Socket
Socket
Sign inDemoInstall

@rjsf/validator-ajv8

Package Overview
Dependencies
4
Maintainers
2
Versions
69
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rjsf/validator-ajv8


Version published
Maintainers
2
Created

Package description

What is @rjsf/validator-ajv8?

@rjsf/validator-ajv8 is a validation package for React JSON Schema Form (RJSF) that uses AJV version 8 for JSON schema validation. It allows you to validate form data against a JSON schema, ensuring that the data conforms to the specified structure and constraints.

What are @rjsf/validator-ajv8's main functionalities?

Schema Validation

This feature allows you to validate data against a JSON schema. The code sample demonstrates how to compile a schema and validate data using AJV.

const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'integer' }
  },
  required: ['name', 'age']
};
const validate = ajv.compile(schema);
const data = { name: 'John', age: 30 };
const valid = validate(data);
if (!valid) console.log(validate.errors);

Custom Keywords

This feature allows you to add custom validation keywords to your schema. The code sample shows how to add a custom keyword 'isPositive' and use it in a schema.

const Ajv = require('ajv');
const ajv = new Ajv();
ajv.addKeyword('isPositive', {
  type: 'number',
  validate: (schema, data) => data > 0
});
const schema = {
  type: 'object',
  properties: {
    positiveNumber: { type: 'number', isPositive: true }
  }
};
const validate = ajv.compile(schema);
const data = { positiveNumber: 10 };
const valid = validate(data);
if (!valid) console.log(validate.errors);

Error Messages

This feature provides detailed error messages when validation fails. The code sample demonstrates how to enable all error messages and validate data that does not meet the schema requirements.

const Ajv = require('ajv');
const ajv = new Ajv({ allErrors: true });
const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'integer', minimum: 18 }
  },
  required: ['name', 'age']
};
const validate = ajv.compile(schema);
const data = { name: 'John', age: 16 };
const valid = validate(data);
if (!valid) console.log(validate.errors);

Other packages similar to @rjsf/validator-ajv8

Changelog

Source

5.0.0-beta.16

@rjsf/antd

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072
  • Updated the use of the deprecated withConfigConsumer with the ConfigConsumer component instead, fixing #3336

@rjsf/bootstrap-4

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated CheckboxWidget to get the required state of the checkbox from the schemaRequiresTrueValue() utility function rather than the required prop, fixing #3317
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/chakra-ui

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated CheckboxWidget to get the required state of the checkbox from the schemaRequiresTrueValue() utility function rather than the required prop, fixing #3317
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/core

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
    • Also, passed registry into the SubmitButton inside of the Form as part of this fix
  • Updated ArrayField to pass the new totalItems and canAdd props to the ArrayFieldItemTemplate instances, fixing #3315
    • Also refactored the near duplicate logic for onAddClick and onAddIndexClick into a new _handleAddClick() function, fixing #3316
  • Fix passing of generic types to a few helper methods, partially fixing #3072
  • Updated the types for ValidatorType, CustomValidator and ErrorTransformer to add the new generics, as well as passing uiSchema to the validateFormData() call, partially fixing #3170

@rjsf/fluent-ui

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/material-ui

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/mui

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/semantic-ui

  • Updated the usage of the ButtonTemplates to pass the new required registry prop, filtering it out in the actual implementations before spreading props, fixing - #3314
  • Updated CheckboxWidget to get the required state of the checkbox from the schemaRequiresTrueValue() utility function rather than the required prop, fixing #3317
    • Also fixed the CheckboxWidget missing label issue #3302
  • Updated the test for the CheckboxWidget validating that the schema.title is passed as the label, fixing #3302
  • Updated the theme to accept generic types, exporting generateXXX functions for Form, Theme, Templates and Widgets to support using the theme with user-specified type generics, partially fixing #3072

@rjsf/utils

  • Updated the SubmitButtonProps and IconButtonProps to add required registry prop, fixing - #3314
  • Updated the ArrayFieldTemplateItemType to add the new totalItems and canAdd props, fixing #3315
  • Updated the CustomValidator and ErrorTransformer types to take the full set of T, S, F generics in order to accept a new optional uiSchema property, partially fixing #3170
  • Updated the ValidatorType to add the F generic to allow the validateFormData() function to take a new optional uiSchema parameter, partially fixing #3170
    • Updated many of the schema-based utility functions to take the additional generics as well to fulfill the ValidatorType interface change

@rjsf/validator-ajv6

  • Updated the customizeValidator and AJV6Validator implementations to add the S and F generics, so that validateFormData() can accept a new optional uiSchema parameter that is passed to transformErrors() and customValidate(), partially fixing #3170

@rjsf/validator-ajv8

  • Updated the customizeValidator and AJV8Validator implementations to add the F generic, so that validateFormData() can accept a new optional uiSchema parameter that is passed to transformErrors() and customValidate(), partially fixing #3170

Dev / docs / playground

  • Fixed the documentation for ArrayFieldItemTemplate, SubmitButtonProps and IconButtonProps as part of the fix for #3314 and #3315
  • Updated the documentation in form-props.md for children, fixing #3322
  • Added new typescript.md documentation to Advanced Customization describing how to use custom generics as part of the fix for #3072
  • Updated the documentation in utilty-functions.md to add the new F generic to all the places which needed them

Readme

Source

Build Status npm npm downloads Contributors Apache 2.0 License


Logo

@rjsf/validator-ajv8

AJV-8 based validator plugin for react-jsonschema-form.
Explore the docs »

View Playground · Report Bug · Request Feature

Table of Contents

About The Project

Exports validator-ajv8 plugin for react-jsonschema-form.

Built With

Getting Started

Prerequisites

React JsonSchema Form Utils
  • @rjsf/utils >= 5.0.0
yarn add @rjsf/core

Installation

yarn add @rjsf/validator-ajv8

Usage

import {RJSFSchema} from "packages/utils/dist/index";
import Form from 'packages/core/dist/index';
import validator from '@rjsf/validator-ajv8';

const schema: RJSFSchema = {
    type: 'string',
};

<Form schema={schema} validator={validator}/>

or, using a more complex example using custom validator with custom formats

import { RJSFSchema } from "@rjsf/utils";
import Form from '@rjsf/core';
import { customizeValidator } from '@rjsf/validator-ajv8';

const customFormats = {
  'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/
};

const validator = customizeValidator({
  customFormats,
});

const schema: RJSFSchema = {
  type: 'string',
  format: 'phone-us'
};

<Form schema={schema} validator={validator} />

or, using a more complex example using a custom with additional meta schema

import { RJSFSchema } from "@rjsf/utils";
import Form from '@rjsf/core';
import { customizeValidator } from '@rjsf/validator-ajv8';

const metaSchemaDraft06 = require("ajv/lib/refs/json-schema-draft-06.json");

const validator = customizeValidator({
  additionalMetaSchemas: [metaSchemaDraft06],
});

const schema: RJSFSchema = {
  "$schema": "http://json-schema.org/draft-06/schema#",
  type: 'string',
};

<Form schema={schema} validator={validator} />

or, using a more complex example using custom validator config override options

import { RJSFSchema } from "@rjsf/utils";
import Form from '@rjsf/core';
import { customizeValidator } from '@rjsf/validator-ajv8';

const validator = customizeValidator({
  ajvOptionsOverrides: {
    $data: true,
    verbose: true,
  }
});

const schema: RJSFSchema = {
  type: 'string',
};

<Form schema={schema} validator={validator} />

or, using a more complex example using ajv-formats custom format options.

import { RJSFSchema } from "@rjsf/utils";
import Form from '@rjsf/core';
import { customizeValidator } from '@rjsf/validator-ajv8';

const validator = customizeValidator({
  ajvFormatOptions: {
    keywords: true,
    formats: ["date", "time"]
  }
});

const schema: RJSFSchema = {
  type: 'string',
};

<Form schema={schema} validator={validator} />

Finally, you can combine both additional meta schemas, custom formats, custom validator config override options and ajv-formats custom format options.

import { RJSFSchema } from "@rjsf/utils";
import Form from '@rjsf/core';
import { customizeValidator } from '@rjsf/validator-ajv8';

const metaSchemaDraft06 = require("ajv/lib/refs/json-schema-draft-06.json");

const customFormats = {
  'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/
};

const validator = customizeValidator({
  additionalMetaSchemas: [metaSchemaDraft06],
  customFormats,
  ajvOptionsOverrides: {
    $data: true,
    verbose: true,
  },
  ajvFormatOptions: {
    keywords: true,
    formats: ["date", "time"]
  }
});

const schema: RJSFSchema = {
  "$schema": "http://json-schema.org/draft-06/schema#",
  type: 'string',
  format: 'phone-us'
};

<Form schema={schema} validator={validator} />

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Read our contributors' guide to get started.

Contact

rjsf team: https://github.com/orgs/rjsf-team/people

GitHub repository: https://github.com/rjsf-team/react-jsonschema-form

Keywords

FAQs

Last updated on 09 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc