diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 69421fe89d..e18914eae5 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -29,7 +29,6 @@ body: - react-bootstrap - semantic-ui - utils - - validator-ajv6 - validator-ajv8 - other validations: diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 967e0ba807..81448b0b39 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -23,7 +23,6 @@ body: - react-bootstrap - semantic-ui - utils - - validator-ajv6 - validator-ajv8 - other validations: diff --git a/.github/ISSUE_TEMPLATE/question_issue.yml b/.github/ISSUE_TEMPLATE/question_issue.yml index c6cd69198f..6571d1ae05 100644 --- a/.github/ISSUE_TEMPLATE/question_issue.yml +++ b/.github/ISSUE_TEMPLATE/question_issue.yml @@ -23,7 +23,6 @@ body: - react-bootstrap - semantic-ui - utils - - validator-ajv6 - validator-ajv8 - other validations: diff --git a/package-lock.json b/package-lock.json index c5a2b53613..0d7dd40883 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "packages/react-bootstrap", "packages/semantic-ui", "packages/utils", - "packages/validator-ajv6", "packages/validator-ajv8", "packages/snapshot-tests" ], @@ -9232,10 +9231,6 @@ "resolved": "packages/utils", "link": true }, - "node_modules/@rjsf/validator-ajv6": { - "resolved": "packages/validator-ajv6", - "link": true - }, "node_modules/@rjsf/validator-ajv8": { "resolved": "packages/validator-ajv8", "link": true @@ -34239,7 +34234,6 @@ "@babel/preset-typescript": "^7.23.3", "@rjsf/snapshot-tests": "^6.0.0-alpha.0", "@rjsf/utils": "^6.0.0-alpha.0", - "@rjsf/validator-ajv6": "^6.0.0-alpha.0", "@rjsf/validator-ajv8": "^6.0.0-alpha.0", "@types/jest": "^29.5.12", "@types/lodash": "^4.14.202", @@ -34490,7 +34484,6 @@ "@rjsf/react-bootstrap": "^6.0.0-alpha.0", "@rjsf/semantic-ui": "^6.0.0-alpha.0", "@rjsf/utils": "^6.0.0-alpha.0", - "@rjsf/validator-ajv6": "^6.0.0-alpha.0", "@rjsf/validator-ajv8": "^6.0.0-alpha.0", "ajv": "^8.12.0", "ajv-formats": "^2.1.1", @@ -34799,41 +34792,6 @@ "react": "^16.14.0 || >=17" } }, - "packages/validator-ajv6": { - "name": "@rjsf/validator-ajv6", - "version": "6.0.0-alpha.0", - "license": "Apache-2.0", - "dependencies": { - "ajv": "^6.12.6", - "lodash": "^4.17.21", - "lodash-es": "^4.17.21" - }, - "devDependencies": { - "@babel/core": "^7.23.9", - "@babel/plugin-proposal-class-properties": "^7.18.6", - "@babel/plugin-proposal-optional-chaining": "^7.21.0", - "@babel/preset-env": "^7.23.9", - "@babel/preset-react": "^7.23.3", - "@babel/preset-typescript": "^7.23.3", - "@rjsf/utils": "^6.0.0-alpha.0", - "@types/jest": "^29.5.12", - "@types/json-schema": "^7.0.15", - "@types/lodash": "^4.14.202", - "babel-jest": "^29.7.0", - "eslint": "^8.56.0", - "jest": "^29.7.0", - "jest-environment-jsdom": "^29.7.0", - "rimraf": "^5.0.5", - "rollup": "^3.29.4", - "typescript": "^4.9.5" - }, - "engines": { - "node": ">=20" - }, - "peerDependencies": { - "@rjsf/utils": "^6.x" - } - }, "packages/validator-ajv8": { "name": "@rjsf/validator-ajv8", "version": "6.0.0-alpha.0", diff --git a/package.json b/package.json index df57fb59f3..0f51885349 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,6 @@ "packages/react-bootstrap", "packages/semantic-ui", "packages/utils", - "packages/validator-ajv6", "packages/validator-ajv8", "packages/snapshot-tests" ] diff --git a/packages/core/package.json b/packages/core/package.json index a4709a6d83..4db01b6888 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -56,7 +56,6 @@ "@babel/preset-typescript": "^7.23.3", "@rjsf/snapshot-tests": "^6.0.0-alpha.0", "@rjsf/utils": "^6.0.0-alpha.0", - "@rjsf/validator-ajv6": "^6.0.0-alpha.0", "@rjsf/validator-ajv8": "^6.0.0-alpha.0", "@types/jest": "^29.5.12", "@types/lodash": "^4.14.202", diff --git a/packages/core/src/tsconfig.json b/packages/core/src/tsconfig.json index d2e6a35741..c10f841019 100644 --- a/packages/core/src/tsconfig.json +++ b/packages/core/src/tsconfig.json @@ -13,9 +13,6 @@ { "path": "../../utils" }, - { - "path": "../../validator-ajv6" - }, { "path": "../../validator-ajv8" } diff --git a/packages/core/test/validate.test.js b/packages/core/test/validate.test.js index 67916d538d..f57bb312f1 100644 --- a/packages/core/test/validate.test.js +++ b/packages/core/test/validate.test.js @@ -3,554 +3,9 @@ import sinon from 'sinon'; import { fireEvent, act } from '@testing-library/react'; import { createFormComponent, submitForm } from './test_utils'; -import v6Validator, { customizeValidator as customizeV6Validator } from '@rjsf/validator-ajv6'; import { customizeValidator as customizeV8Validator } from '@rjsf/validator-ajv8'; describe('Validation', () => { - describe('Form integration, v6 validator', () => { - let sandbox; - - beforeEach(() => { - sandbox = sinon.createSandbox(); - }); - - afterEach(() => { - sandbox.restore(); - }); - - describe('JSONSchema validation', () => { - describe('ShowErrorList prop top', () => { - const schema = { - type: 'object', - required: ['foo'], - properties: { - foo: { type: 'string' }, - bar: { type: 'string' }, - }, - }; - - let node; - const compInfo = createFormComponent({ - schema, - formData: { - foo: undefined, - }, - }); - node = compInfo.node; - submitForm(node); - - it('should render errors at the top', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(1); - expect(node.querySelector('.errors li').textContent).eql("must have required property 'foo'"); - expect(node.childNodes[0].className).to.eql('panel panel-danger errors'); - }); - }); - - describe('ShowErrorList prop bottom', () => { - const schema = { - type: 'object', - required: ['foo'], - properties: { - foo: { type: 'string' }, - bar: { type: 'string' }, - }, - }; - - let node; - const compInfo = createFormComponent({ - showErrorList: 'bottom', - schema, - formData: { - foo: undefined, - }, - }); - node = compInfo.node; - submitForm(node); - - it('should render errors at the bottom', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(1); - expect(node.querySelector('.errors li').textContent).eql("must have required property 'foo'"); - - // The last child node is the submit button so the one before it will be the error list - expect(node.childNodes[2].className).to.eql('panel panel-danger errors'); - }); - }); - - describe('Required fields', () => { - const schema = { - type: 'object', - required: ['foo'], - properties: { - foo: { type: 'string' }, - bar: { type: 'string' }, - }, - }; - - let onError, node; - beforeEach(() => { - const compInfo = createFormComponent({ - schema, - formData: { - foo: undefined, - }, - validator: v6Validator, - }); - onError = compInfo.onError; - node = compInfo.node; - submitForm(node); - }); - - it('should trigger onError call', () => { - sinon.assert.calledWithMatch(onError.lastCall, [ - { - message: 'is a required property', - name: 'required', - params: { missingProperty: 'foo' }, - property: '.foo', - schemaPath: '#/required', - stack: '.foo is a required property', - }, - ]); - }); - - it('should render errors', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(1); - expect(node.querySelector('.errors li').textContent).eql('.foo is a required property'); - expect(node.childNodes[0].className).to.eql('panel panel-danger errors'); - }); - }); - - describe('Min length', () => { - const schema = { - type: 'object', - required: ['foo'], - properties: { - foo: { - type: 'string', - minLength: 10, - }, - }, - }; - - let node, onError; - - beforeEach(() => { - onError = sandbox.spy(); - const compInfo = createFormComponent({ - schema, - formData: { - foo: '123456789', - }, - onError, - validator: v6Validator, - }); - node = compInfo.node; - - submitForm(node); - }); - - it('should render errors', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(1); - expect(node.querySelector('.errors li').textContent).eql('.foo should NOT be shorter than 10 characters'); - }); - - it('should trigger the onError handler', () => { - sinon.assert.calledWithMatch(onError.lastCall, [ - { - message: 'should NOT be shorter than 10 characters', - name: 'minLength', - params: { limit: 10 }, - property: '.foo', - schemaPath: '#/properties/foo/minLength', - stack: '.foo should NOT be shorter than 10 characters', - }, - ]); - }); - }); - }); - - describe('Custom Form validation', () => { - it('should validate a simple string value', () => { - const schema = { type: 'string' }; - const formData = 'a'; - - function customValidate(formData, errors) { - if (formData !== 'hello') { - errors.addError('Invalid'); - } - return errors; - } - - const { onError, node } = createFormComponent({ - schema, - customValidate, - formData, - validator: v6Validator, - }); - - submitForm(node); - sinon.assert.calledWithMatch(onError.lastCall, [{ property: '.', message: 'Invalid', stack: '. Invalid' }]); - }); - - it('should live validate a simple string value when liveValidate is set to true', () => { - const schema = { type: 'string' }; - const formData = 'a'; - - function customValidate(formData, errors) { - if (formData !== 'hello') { - errors.addError('Invalid'); - } - return errors; - } - - const { onChange, node } = createFormComponent({ - schema, - customValidate, - formData, - liveValidate: true, - validator: v6Validator, - }); - - act(() => { - fireEvent.change(node.querySelector('input'), { - target: { value: '1234' }, - }); - }); - - sinon.assert.calledWithMatch( - onChange.lastCall, - { - errorSchema: { __errors: ['Invalid'] }, - errors: [{ property: '.', message: 'Invalid', stack: '. Invalid' }], - formData: '1234', - }, - 'root' - ); - }); - - it('should submit form on valid data', () => { - const schema = { type: 'string' }; - const formData = 'hello'; - const onSubmit = sandbox.spy(); - - function customValidate(formData, errors) { - if (formData !== 'hello') { - errors.addError('Invalid'); - } - return errors; - } - - const { node } = createFormComponent({ - schema, - formData, - customValidate, - onSubmit, - validator: v6Validator, - }); - - submitForm(node); - - sinon.assert.called(onSubmit); - }); - - it('should prevent form submission on invalid data', () => { - const schema = { type: 'string' }; - const formData = 'a'; - const onSubmit = sandbox.spy(); - const onError = sandbox.spy(); - - function customValidate(formData, errors) { - if (formData !== 'hello') { - errors.addError('Invalid'); - } - return errors; - } - - const { node } = createFormComponent({ - schema, - formData, - customValidate, - onSubmit, - onError, - validator: v6Validator, - }); - - submitForm(node); - - sinon.assert.notCalled(onSubmit); - sinon.assert.called(onError); - }); - - it('should validate a simple object', () => { - const schema = { - type: 'object', - properties: { - pass1: { type: 'string', minLength: 3 }, - pass2: { type: 'string', minLength: 3 }, - }, - }; - - const formData = { pass1: 'aaa', pass2: 'b' }; - - function customValidate(formData, errors) { - const { pass1, pass2 } = formData; - if (pass1 !== pass2) { - errors.pass2.addError("Passwords don't match"); - } - return errors; - } - - const { node, onError } = createFormComponent({ - schema, - customValidate, - formData, - validator: v6Validator, - }); - submitForm(node); - sinon.assert.calledWithMatch(onError.lastCall, [ - { - message: 'should NOT be shorter than 3 characters', - name: 'minLength', - params: { limit: 3 }, - property: '.pass2', - schemaPath: '#/properties/pass2/minLength', - stack: '.pass2 should NOT be shorter than 3 characters', - }, - { - property: '.pass2', - message: "Passwords don't match", - stack: ".pass2 Passwords don't match", - }, - ]); - }); - - it('should validate an array of object', () => { - const schema = { - type: 'array', - items: { - type: 'object', - properties: { - pass1: { type: 'string' }, - pass2: { type: 'string' }, - }, - }, - }; - - const formData = [ - { pass1: 'a', pass2: 'b' }, - { pass1: 'a', pass2: 'a' }, - ]; - - function customValidate(formData, errors) { - formData.forEach(({ pass1, pass2 }, i) => { - if (pass1 !== pass2) { - errors[i].pass2.addError("Passwords don't match"); - } - }); - return errors; - } - - const { node, onError } = createFormComponent({ - schema, - customValidate, - formData, - validator: v6Validator, - }); - - submitForm(node); - sinon.assert.calledWithMatch(onError.lastCall, [ - { - property: '.0.pass2', - message: "Passwords don't match", - stack: ".0.pass2 Passwords don't match", - }, - ]); - }); - - it('should validate a simple array', () => { - const schema = { - type: 'array', - items: { - type: 'string', - }, - }; - - const formData = ['aaa', 'bbb', 'ccc']; - - function customValidate(formData, errors) { - if (formData.indexOf('bbb') !== -1) { - errors.addError('Forbidden value: bbb'); - } - return errors; - } - - const { node, onError } = createFormComponent({ - schema, - customValidate, - formData, - validator: v6Validator, - }); - submitForm(node); - sinon.assert.calledWithMatch(onError.lastCall, [ - { - property: '.', - message: 'Forbidden value: bbb', - stack: '. Forbidden value: bbb', - }, - ]); - }); - }); - - describe('showErrorList prop validation', () => { - describe('Required fields', () => { - const schema = { - type: 'object', - required: ['foo'], - properties: { - foo: { type: 'string' }, - bar: { type: 'string' }, - }, - }; - - let node, onError; - beforeEach(() => { - const compInfo = createFormComponent({ - schema, - formData: { - foo: undefined, - }, - showErrorList: false, - validator: v6Validator, - }); - node = compInfo.node; - onError = compInfo.onError; - - submitForm(node); - }); - - it('should not render error list if showErrorList prop true', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(0); - }); - - it('should trigger onError call', () => { - sinon.assert.calledWithMatch(onError.lastCall, [ - { - message: 'is a required property', - name: 'required', - params: { missingProperty: 'foo' }, - property: '.foo', - schemaPath: '#/required', - stack: '.foo is a required property', - }, - ]); - }); - }); - }); - - describe('Custom ErrorList', () => { - const schema = { - type: 'string', - minLength: 1, - }; - - const uiSchema = { - foo: 'bar', - }; - - const formData = 0; - - const CustomErrorList = ({ errors, errorSchema, schema, uiSchema, formContext: { className } }) => ( -
-
{errors.length} custom
-
{errorSchema.__errors[0]}
-
{schema.type}
-
{uiSchema.foo}
-
-
- ); - - it('should use CustomErrorList', () => { - const { node } = createFormComponent({ - schema, - uiSchema, - liveValidate: true, - formData, - templates: { ErrorListTemplate: CustomErrorList }, - formContext: { className: 'foo' }, - validator: v6Validator, - }); - expect(node.querySelectorAll('.CustomErrorList')).to.have.length.of(1); - expect(node.querySelector('.CustomErrorList').textContent).eql('1 custom'); - expect(node.querySelectorAll('.ErrorSchema')).to.have.length.of(1); - expect(node.querySelector('.ErrorSchema').textContent).eql('should be string'); - expect(node.querySelectorAll('.Schema')).to.have.length.of(1); - expect(node.querySelector('.Schema').textContent).eql('string'); - expect(node.querySelectorAll('.UiSchema')).to.have.length.of(1); - expect(node.querySelector('.UiSchema').textContent).eql('bar'); - expect(node.querySelectorAll('.foo')).to.have.length.of(1); - }); - }); - describe('Custom meta schema', () => { - let onError, node; - const formData = { - datasetId: 'no err', - }; - - const schema = { - $ref: '#/definitions/Dataset', - $schema: 'http://json-schema.org/draft-06/schema#', - definitions: { - Dataset: { - properties: { - datasetId: { - pattern: '\\d+', - type: 'string', - }, - }, - required: ['datasetId'], - type: 'object', - }, - }, - }; - - beforeEach(() => { - const validator = customizeV6Validator({ - additionalMetaSchemas: [require('ajv/lib/refs/json-schema-draft-06.json')], - }); - const withMetaSchema = createFormComponent({ - schema, - formData, - liveValidate: true, - validator, - }); - node = withMetaSchema.node; - onError = withMetaSchema.onError; - submitForm(node); - }); - it('should be used to validate schema', () => { - expect(node.querySelectorAll('.errors li')).to.have.length.of(1); - sinon.assert.calledWithMatch(onError.lastCall, [ - { - message: 'should match pattern "\\d+"', - name: 'pattern', - params: { pattern: '\\d+' }, - property: '.datasetId', - schemaPath: '#/properties/datasetId/pattern', - stack: '.datasetId should match pattern "\\d+"', - }, - ]); - onError.resetHistory(); - - act(() => { - fireEvent.change(node.querySelector('input'), { - target: { value: '1234' }, - }); - }); - expect(node.querySelectorAll('.errors li')).to.have.length.of(0); - sinon.assert.notCalled(onError); - }); - }); - }); describe('Form integration, v8 validator', () => { let sandbox; diff --git a/packages/docs/docs/api-reference/validator-ajv8.md b/packages/docs/docs/api-reference/validator-ajv8.md index 2e9cedffed..17443f4604 100644 --- a/packages/docs/docs/api-reference/validator-ajv8.md +++ b/packages/docs/docs/api-reference/validator-ajv8.md @@ -1,7 +1,7 @@ # @rjsf/validator-ajv8 APIs -In RJSF version 5, the original, embedded AJV 6 validator implementation from earlier versions was extracted into its own package, `@rjsf/validator-ajv6`, which was immediately deprecated since AJV 6 is no longer getting maintenance updates. -A new `@rjsf/validator-ajv8` package was added that uses the AJV 8 package, including adding support for using precompiled validators. +In RJSF version 6, the original, embedded AJV 6 validator implementation from earlier versions was removed in favor of the `@rjsf/validator-ajv8` package. +`@rjsf/validator-ajv8` uses the AJV 8 package, including adding support for using precompiled validators. Below are the exported API functions that are provided by this package. See the [Validation documentation](../usage/validation.md) for examples of using these APIs. diff --git a/packages/docs/docs/contributing.md b/packages/docs/docs/contributing.md index 99fcb0145c..e1015de6ee 100644 --- a/packages/docs/docs/contributing.md +++ b/packages/docs/docs/contributing.md @@ -125,7 +125,7 @@ The full report can be seen by opening `./coverage/lcov-report/index.html`. #### Utils and validator-ajvX code coverage -100% code coverage is required by the `@rjsf/utils` and `@rjsf/validator-ajv6` and `@rjsf/validator-ajv8` tests. +100% code coverage is required by the `@rjsf/utils` and `@rjsf/validator-ajv8` tests. If you make changes to those libraries, you will have to maintain that coverage, otherwise the tests will fail. > NOTE: All three of these directories share the same tests for verifying `validator` based APIs. See the documentation in the `getTestValidator()` functions for more information. diff --git a/packages/docs/docs/usage/validation.md b/packages/docs/docs/usage/validation.md index 00674a7e43..432561f24b 100644 --- a/packages/docs/docs/usage/validation.md +++ b/packages/docs/docs/usage/validation.md @@ -3,11 +3,7 @@ When the form is submitted, the form data is validated to conform to the given JSON schema; As of version 5, as a result of decoupling the validation implementation from the `Form`, a `validator` implementation is required to be passed to all `Form`s. -React Json Schema Form provides a default `@rjsf/validator-ajv6` implementation that uses version 6 of the [ajv](https://github.com/ajv-validator/ajv) validator. - -> NOTE: The ajv6 validator has been deprecated from the outset in favor of the `@rjsf/validator-ajv8` and is provided primarily to assist in code migrations - -It also provides a new `@rjsf/validator-ajv8` implementation that uses version 8 of the [ajv](https://github.com/ajv-validator/ajv) validator. +React Json Schema Form provides a default `@rjsf/validator-ajv8` implementation that uses version 8 of the [ajv](https://github.com/ajv-validator/ajv) validator. The error messages generated by this new validator differ from those provided by the original validator due to it using a newer version. If you depend on having specifically formatted messages, then using this validator would constitute a breaking change for you. @@ -466,25 +462,6 @@ const metaSchemaDraft04 = require('ajv/lib/refs/json-schema-draft-04.json'); In this example `schema` passed as props to `Form` component can be validated against draft-07 (default) and by draft-04 (added), depending on the value of `$schema` attribute. -```tsx -import { Form } from '@rjsf/core'; -import { RJSFSchema } from '@rjsf/utils'; -import { customizeValidator } from '@rjsf/validator-ajv6'; - -const validator = customizeValidator({ - additionalMetaSchemas: [metaSchemaDraft04], -}); - -const schema: RJSFSchema = { - $schema: 'http://json-schema.org/draft-04/schema#', - type: 'string', -}; - -return
; -``` - -NOTE: This syntax works only for the `@rjsf/validator-ajv6` validator; if you only use the `draft-04` schema, and you want to use the `@rjsf/validator-ajv8` you can do the following: - ```tsx import { Form } from '@rjsf/core'; import { RJSFSchema } from '@rjsf/utils'; @@ -598,11 +575,11 @@ render(, document.getElementById(' ## Using the raw Ajv instance -The `customizeValidator()` function returns the `AJV8Validator` (or `AJV6Validator` depending on the library you use) implementation class, which has an internal raw `ajv` instance within it. +The `customizeValidator()` function returns the `AJV8Validator` implementation class, which has an internal raw `ajv` instance within it. If you need to do some deep customization of the instance using any of the `ajv` libraries (like `ajv-keywords`), you can do so using this raw instance as follows: ```ts -import { customizeValidator } from '@rjsf/validator-ajv6'; +import { customizeValidator } from '@rjsf/validator-ajv8'; import ajvKeywords from 'ajv-keywords'; const validator = customizeValidator(); @@ -611,17 +588,11 @@ ajvKeywords(validator.ajv, ['your-keyword']); // use your update validator with a `Form` ``` -## Ajv8 validator differences - -There are a few differences in configuring the Ajv 8 validator. -First, there are many things to be aware of related to internal migration from Ajv 6 to 8; see the [migration guide](https://ajv.js.org/v6-to-v8-migration.html) for more information. - -One big difference is that Ajv 8 dropped support for any JSON Schema version before draft-06. -So if your schema is using an older format, you have to either upgrade it or stick with the `@rjsf/validator-ajv6`. +## Ajv8 validator -Our implementation of `@rjsf/validator-ajv8` also utilizes Ajv's internal cache to avoid unnecessarily re-compiling schemas, which can be an expensive operation. The cache key is the schema `$id`. +Our implementation of `@rjsf/validator-ajv8` utilizes Ajv's internal cache to avoid unnecessarily re-compiling schemas, which can be an expensive operation. The cache key is the schema `$id`. -The `ajvOptionsOverrides` for the Ajv 8 validator are the ones supported by that version and not the Ajv 6 validator. +The `ajvOptionsOverrides` for the Ajv 8 validator are the ones supported by that version. Second, the data formats previously provided in Ajv 6 now need to be added explicitly using the `ajv-formats` package. A new `ajvFormatOptions` option is available on the `customizeValidator()` API to be able to configure this. Additionally, a new `AjvClass` option is available on the `customizeValidator()` API to support using one of the other [JSON schema versions](https://ajv.js.org/json-schema.html#json-schema-versions) provided by Ajv 8 besides the `draft-07` default. diff --git a/packages/fluentui-rc/README.md b/packages/fluentui-rc/README.md index 37198bc3f9..d767a56986 100644 --- a/packages/fluentui-rc/README.md +++ b/packages/fluentui-rc/README.md @@ -63,7 +63,7 @@ Fluent UI React Components (v9) theme, fields and widgets for `react-jsonschema- - `@rjsf/core >= 2.0.0` ```bash -yarn add @fluentui/react-components @rjsf/core @rjsf/utils @rjsf/validator-ajv6 +yarn add @fluentui/react-components @rjsf/core @rjsf/utils @rjsf/validator-ajv8 ``` ### Installation diff --git a/packages/mui/README.md b/packages/mui/README.md index 0251339302..31acff882a 100644 --- a/packages/mui/README.md +++ b/packages/mui/README.md @@ -71,10 +71,10 @@ NOTE: Material UI 5 requires React 17, so you will need to upgrade - `@emotion/styled` - `@rjsf/core >= 5.0.0` - `@rjsf/utils >= 5.0.0` -- `@rjsf/validator-ajv6 >= 5.0.0` +- `@rjsf/validator-ajv8 >= 5.0.0` ```bash -yarn add @mui/material @mui/icons-material @emotion/react @emotion/styled @rjsf/core @rjsf/utils @rjsf/validator-ajv6 +yarn add @mui/material @mui/icons-material @emotion/react @emotion/styled @rjsf/core @rjsf/utils @rjsf/validator-ajv8 ``` ### Installation diff --git a/packages/playground/package.json b/packages/playground/package.json index 05cf277dcd..881a4c48ac 100644 --- a/packages/playground/package.json +++ b/packages/playground/package.json @@ -47,7 +47,6 @@ "@rjsf/react-bootstrap": "^6.0.0-alpha.0", "@rjsf/semantic-ui": "^6.0.0-alpha.0", "@rjsf/utils": "^6.0.0-alpha.0", - "@rjsf/validator-ajv6": "^6.0.0-alpha.0", "@rjsf/validator-ajv8": "^6.0.0-alpha.0", "ajv": "^8.12.0", "ajv-formats": "^2.1.1", diff --git a/packages/playground/src/app.tsx b/packages/playground/src/app.tsx index cac5be629d..fb1753dc49 100644 --- a/packages/playground/src/app.tsx +++ b/packages/playground/src/app.tsx @@ -5,7 +5,6 @@ import { Theme as AntdTheme } from '@rjsf/antd'; import { Theme as BootstrapTheme } from '@rjsf/react-bootstrap'; import { Theme as ChakraUITheme } from '@rjsf/chakra-ui'; import v8Validator, { customizeValidator } from '@rjsf/validator-ajv8'; -import v6Validator from '@rjsf/validator-ajv6'; import localize_es from 'ajv-i18n/localize/es'; import Ajv2019 from 'ajv/dist/2019.js'; import Ajv2020 from 'ajv/dist/2020.js'; diff --git a/packages/playground/tsconfig.json b/packages/playground/tsconfig.json index 26e665a186..32d1966633 100644 --- a/packages/playground/tsconfig.json +++ b/packages/playground/tsconfig.json @@ -30,7 +30,6 @@ { "path": "../react-bootstrap"}, { "path": "../semantic-ui" }, { "path": "../utils" }, - { "path": "../validator-ajv6" }, { "path": "../validator-ajv8" } ] } diff --git a/packages/playground/vite.config.ts b/packages/playground/vite.config.ts index 80848da723..5c0345e0e8 100644 --- a/packages/playground/vite.config.ts +++ b/packages/playground/vite.config.ts @@ -24,11 +24,6 @@ export default defineConfig({ '@rjsf/semantic-ui': path.resolve(__dirname, '../semantic-ui/src'), '@rjsf/utils': path.resolve(__dirname, '../utils/src'), '@rjsf/validator-ajv8': path.resolve(__dirname, '../validator-ajv8/src'), - // validator-ajv6 can not be mapped directly to the sources, because that causes wrong ajv version resolution - // which looks related to: https://github.com/vitejs/vite/issues/12618 - // - // the difference when mapping directly vs mapping to src folder - @vitejs/plugin-react can not be applied in the 2nd case - '@rjsf/validator-ajv6': '@rjsf/validator-ajv6/src', }, }, }); diff --git a/packages/snapshot-tests/tsconfig.json b/packages/snapshot-tests/tsconfig.json index 6d01a14322..317c541ebb 100644 --- a/packages/snapshot-tests/tsconfig.json +++ b/packages/snapshot-tests/tsconfig.json @@ -14,9 +14,6 @@ { "path": "../utils" }, - { - "path": "../validator-ajv6" - }, { "path": "../validator-ajv8" } diff --git a/packages/validator-ajv6/.eslintrc b/packages/validator-ajv6/.eslintrc deleted file mode 100644 index 731b36bae0..0000000000 --- a/packages/validator-ajv6/.eslintrc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "extends": ["../../.eslintrc"], - "plugins": ["@typescript-eslint", "jsx-a11y", "react", "import"] -} diff --git a/packages/validator-ajv6/README.md b/packages/validator-ajv6/README.md deleted file mode 100644 index ebbdb7ecd1..0000000000 --- a/packages/validator-ajv6/README.md +++ /dev/null @@ -1,223 +0,0 @@ -[![Build Status][build-shield]][build-url] -[![npm][npm-shield]][npm-url] -[![npm downloads][npm-dl-shield]][npm-dl-url] -[![Contributors][contributors-shield]][contributors-url] -[![Apache 2.0 License][license-shield]][license-url] - - -
-

- - Logo - - -

@rjsf/validator-ajv6

- -

- AJV-6 based validator plugin for react-jsonschema-form. -
- Explore the docs » -
-
- View Playground - · - Report Bug - · - Request Feature -

-

- - - -## Table of Contents - -- [Table of Contents](#table-of-contents) -- [About The Project](#about-the-project) - - [Built With](#built-with) -- [Getting Started](#getting-started) - - [Prerequisites](#prerequisites) - - [Installation](#installation) -- [Usage](#usage) -- [Roadmap](#roadmap) -- [Contributing](#contributing) -- [Contact](#contact) - - - -## About The Project - -Exports `validator-ajv6` plugin for `react-jsonschema-form`. - -### Built With - -- [react-jsonschema-form](https://github.com/rjsf-team/react-jsonschema-form/) -- [AJV](https://github.com/ajv-validator/ajv/) -- [TypeScript](https://www.typescriptlang.org/) - - - -## Getting Started - -> NOTE: This package is deprecated in favor of the `@rjsf/validator-ajv8` and is provided primarily for people upgrading to version 5 who initially want to minimize the validator differences. - -### Prerequisites - -#### React JsonSchema Form Utils - -- `@rjsf/utils >= 5.0.0` - -```bash -yarn add @rjsf/core -``` - -### Installation - -```bash -yarn add @rjsf/validator-ajv6 -``` - - - -## Usage - -```tsx -import { RJSFSchema } from '@rjsf/utils'; -import Form from '@rjsf/core'; -import validator from '@rjsf/validator-ajv6'; - -const schema: RJSFSchema = { - type: 'string', -}; - -; -``` - -or, using a more complex example using custom validator with custom formats - -```tsx -import { RJSFSchema } from '@rjsf/utils'; -import Form from '@rjsf/core'; -import { customizeValidator } from '@rjsf/validator-ajv6'; - -const customFormats = { - 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, -}; - -const validator = customizeValidator({ - customFormats, -}); - -const schema: RJSFSchema = { - type: 'string', - format: 'phone-us', -}; - -; -``` - -or, using a more complex example using a custom with additional meta schema - -```tsx -import { RJSFSchema } from '@rjsf/utils'; -import Form from '@rjsf/core'; -import { customizeValidator } from '@rjsf/validator-ajv6'; - -const metaSchemaDraft04 = require('ajv/lib/refs/json-schema-draft-04.json'); - -const validator = customizeValidator({ - additionalMetaSchemas: [metaSchemaDraft04], -}); - -const schema: RJSFSchema = { - $schema: 'http://json-schema.org/draft-04/schema#', - type: 'string', -}; - -; -``` - -or, using a more complex example using custom validator config override options - -```tsx -import { RJSFSchema } from '@rjsf/utils'; -import Form from '@rjsf/core'; -import { customizeValidator } from '@rjsf/validator-ajv6'; - -const validator = customizeValidator({ - ajvOptionsOverrides: { - $data: true, - verbose: true, - }, -}); - -const schema: RJSFSchema = { - type: 'string', -}; - -; -``` - -Finally, you can combine both additional meta schemas, custom formats and custom validator config override options. - -```tsx -import { RJSFSchema } from '@rjsf/utils'; -import Form from '@rjsf/core'; -import { customizeValidator } from '@rjsf/validator-ajv6'; - -const metaSchemaDraft04 = require('ajv/lib/refs/json-schema-draft-04.json'); - -const customFormats = { - 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, -}; - -const validator = customizeValidator({ - additionalMetaSchemas: [metaSchemaDraft04], - customFormats, - ajvOptionsOverrides: { - $data: true, - verbose: true, - }, -}); - -const schema: RJSFSchema = { - $schema: 'http://json-schema.org/draft-04/schema#', - type: 'string', - format: 'phone-us', -}; - -; -``` - - - -## Roadmap - -See the [open issues](https://github.com/rjsf-team/react-jsonschema-form/issues) for a list of proposed features (and known issues). - - - -## Contributing - -Read our [contributors' guide](https://rjsf-team.github.io/react-jsonschema-form/docs/contributing/) to get started. - - - -## Contact - -rjsf team: [https://github.com/orgs/rjsf-team/people](https://github.com/orgs/rjsf-team/people) - -GitHub repository: [https://github.com/rjsf-team/react-jsonschema-form](https://github.com/rjsf-team/react-jsonschema-form) - - - - -[build-shield]: https://github.com/rjsf-team/react-jsonschema-form/workflows/CI/badge.svg -[build-url]: https://github.com/rjsf-team/react-jsonschema-form/actions -[contributors-shield]: https://img.shields.io/github/contributors/rjsf-team/react-jsonschema-form.svg -[contributors-url]: https://github.com/rjsf-team/react-jsonschema-form/graphs/contributors -[license-shield]: https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=flat-square -[license-url]: https://choosealicense.com/licenses/apache-2.0/ -[npm-shield]: https://img.shields.io/npm/v/@rjsf/validator-ajv6/latest.svg?style=flat-square -[npm-url]: https://www.npmjs.com/package/@rjsf/validator-ajv6 -[npm-dl-shield]: https://img.shields.io/npm/dm/@rjsf/validator-ajv6.svg?style=flat-square -[npm-dl-url]: https://www.npmjs.com/package/@rjsf/validator-ajv6 diff --git a/packages/validator-ajv6/babel.config.js b/packages/validator-ajv6/babel.config.js deleted file mode 100644 index 5f772a56c4..0000000000 --- a/packages/validator-ajv6/babel.config.js +++ /dev/null @@ -1,3 +0,0 @@ -const defaultConfig = require('../../babel.config'); - -module.exports = defaultConfig; diff --git a/packages/validator-ajv6/jest.config.js b/packages/validator-ajv6/jest.config.js deleted file mode 100644 index ba654509b0..0000000000 --- a/packages/validator-ajv6/jest.config.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = { - verbose: true, - testEnvironment: 'jsdom', - testEnvironmentOptions: { - browsers: ['chrome', 'firefox', 'safari'], - }, - testMatch: ['**/test/**/*.test.ts?(x)'], - coverageDirectory: '/coverage/', - collectCoverage: true, - coveragePathIgnorePatterns: ['/node_modules/', '/test'], - coverageThreshold: { - global: { - branches: 100, - functions: 100, - lines: 100, - statements: 100, - }, - }, -}; diff --git a/packages/validator-ajv6/logo.png b/packages/validator-ajv6/logo.png deleted file mode 100644 index 0db8558c7a..0000000000 Binary files a/packages/validator-ajv6/logo.png and /dev/null differ diff --git a/packages/validator-ajv6/package.json b/packages/validator-ajv6/package.json deleted file mode 100644 index a06bbf86f5..0000000000 --- a/packages/validator-ajv6/package.json +++ /dev/null @@ -1,82 +0,0 @@ -{ - "name": "@rjsf/validator-ajv6", - "version": "6.0.0-alpha.0", - "main": "dist/index.js", - "module": "lib/index.js", - "typings": "lib/index.d.ts", - "description": "The ajv-6 based validator for @rjsf/core", - "files": [ - "dist", - "lib", - "src" - ], - "engineStrict": false, - "engines": { - "node": ">=20" - }, - "scripts": { - "build:ts": "tsc -b", - "build:cjs": "esbuild ./src/index.ts --bundle --outfile=dist/index.js --sourcemap --packages=external --format=cjs", - "build:esm": "esbuild ./src/index.ts --bundle --outfile=dist/validator-ajv6.esm.js --sourcemap --packages=external --format=esm", - "build:umd": "rollup dist/validator-ajv6.esm.js --format=umd --file=dist/validator-ajv6.umd.js --name=@rjsf/validator-ajv6", - "build": "npm run build:ts && npm run build:cjs && npm run build:esm && npm run build:umd", - "cs-check": "prettier -l \"{src,test}/**/*.ts?(x)\"", - "cs-format": "prettier \"{src,test}/**/*.ts?(x)\" --write", - "lint": "eslint src test", - "precommit": "lint-staged", - "test": "jest" - }, - "lint-staged": { - "{src,test}/**/*.ts?(x)": [ - "eslint --fix" - ] - }, - "dependencies": { - "ajv": "^6.12.6", - "lodash": "^4.17.21", - "lodash-es": "^4.17.21" - }, - "peerDependencies": { - "@rjsf/utils": "^6.x" - }, - "devDependencies": { - "@babel/core": "^7.23.9", - "@babel/plugin-proposal-class-properties": "^7.18.6", - "@babel/plugin-proposal-optional-chaining": "^7.21.0", - "@babel/preset-env": "^7.23.9", - "@babel/preset-react": "^7.23.3", - "@babel/preset-typescript": "^7.23.3", - "@rjsf/utils": "^6.0.0-alpha.0", - "@types/jest": "^29.5.12", - "@types/json-schema": "^7.0.15", - "@types/lodash": "^4.14.202", - "babel-jest": "^29.7.0", - "eslint": "^8.56.0", - "jest": "^29.7.0", - "jest-environment-jsdom": "^29.7.0", - "rimraf": "^5.0.5", - "rollup": "^3.29.4", - "typescript": "^4.9.5" - }, - "publishConfig": { - "access": "public" - }, - "author": "Heath Chiavettone { - ajv.addFormat(formatName, customFormats[formatName]); - }); - } - - return ajv; -} diff --git a/packages/validator-ajv6/src/customizeValidator.ts b/packages/validator-ajv6/src/customizeValidator.ts deleted file mode 100644 index 3706b268ba..0000000000 --- a/packages/validator-ajv6/src/customizeValidator.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { FormContextType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils'; - -import { CustomValidatorOptionsType } from './types'; -import AJV6Validator from './validator'; - -/** Creates and returns a customized implementation of the `ValidatorType` with the given customization `options` if - * provided. - * - * @param [options={}] - The `CustomValidatorOptionsType` options that are used to create the `ValidatorType` instance - * @deprecated in favor of the `@rjsf/validator-ajv8 - */ -export default function customizeValidator< - T = any, - S extends StrictRJSFSchema = RJSFSchema, - F extends FormContextType = any ->(options: CustomValidatorOptionsType = {}) { - return new AJV6Validator(options); -} diff --git a/packages/validator-ajv6/src/index.ts b/packages/validator-ajv6/src/index.ts deleted file mode 100644 index 8b4da43615..0000000000 --- a/packages/validator-ajv6/src/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -import customizeValidator from './customizeValidator'; - -export { customizeValidator }; -export * from './types'; - -/** @deprecated in favor of the `@rjsf/validator-ajv8 - */ -export default customizeValidator(); diff --git a/packages/validator-ajv6/src/tsconfig.json b/packages/validator-ajv6/src/tsconfig.json deleted file mode 100644 index 2701b42ad7..0000000000 --- a/packages/validator-ajv6/src/tsconfig.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "extends": "../../../tsconfig.base.json", - "include": [ - "./" - ], - "compilerOptions": { - "rootDir": "./", - "outDir": "../lib", - "baseUrl": "../", - "jsx": "react-jsx" - }, - "references": [ - { - "path": "../../utils" - } - ] -} diff --git a/packages/validator-ajv6/src/types.ts b/packages/validator-ajv6/src/types.ts deleted file mode 100644 index a5b6aaa009..0000000000 --- a/packages/validator-ajv6/src/types.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Options } from 'ajv'; - -/** The type describing how to customize the AJV6 validator - */ -export interface CustomValidatorOptionsType { - /** The list of additional meta schemas that the validator can access */ - additionalMetaSchemas?: ReadonlyArray; - /** The set of additional custom formats that the validator will support */ - customFormats?: { - [k: string]: string | RegExp | ((data: string) => boolean); - }; - /** The set of config overrides that will be passed to the AJV validator constructor on top of the defaults */ - ajvOptionsOverrides?: Options; -} diff --git a/packages/validator-ajv6/src/validator.ts b/packages/validator-ajv6/src/validator.ts deleted file mode 100644 index 79c221a246..0000000000 --- a/packages/validator-ajv6/src/validator.ts +++ /dev/null @@ -1,199 +0,0 @@ -import { Ajv, ErrorObject } from 'ajv'; -import { - createErrorHandler, - CustomValidator, - deepEquals, - ErrorSchema, - ErrorTransformer, - FormContextType, - getDefaultFormState, - RJSFSchema, - RJSFValidationError, - ROOT_SCHEMA_PREFIX, - StrictRJSFSchema, - toErrorList, - toErrorSchema, - UiSchema, - unwrapErrorHandler, - ValidationData, - validationDataMerge, - ValidatorType, - withIdRefPrefix, -} from '@rjsf/utils'; - -import { CustomValidatorOptionsType } from './types'; -import createAjvInstance from './createAjvInstance'; - -/** `ValidatorType` implementation that uses the AJV 6 validation mechanism. - * - * @deprecated in favor of the `@rjsf/validator-ajv8 - */ -export default class AJV6Validator - implements ValidatorType -{ - /** The AJV instance to use for all validations - */ - ajv: Ajv; - - /** Constructs an `AJV6Validator` instance using the `options` - * - * @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance - */ - constructor(options: CustomValidatorOptionsType) { - const { additionalMetaSchemas, customFormats, ajvOptionsOverrides } = options; - this.ajv = createAjvInstance(additionalMetaSchemas, customFormats, ajvOptionsOverrides); - } - - /** Converts an `errorSchema` into a list of `RJSFValidationErrors` - * - * @param errorSchema - The `ErrorSchema` instance to convert - * @param [fieldPath=[]] - The current field path, defaults to [] if not specified - * @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in - * the next major release. - */ - toErrorList(errorSchema?: ErrorSchema, fieldPath: string[] = []) { - return toErrorList(errorSchema, fieldPath); - } - - /** Transforming the error output from ajv to format used by @rjsf/utils. - * At some point, components should be updated to support ajv. - * - * @param errors - The list of AJV errors to convert to `RJSFValidationErrors` - * @private - */ - private transformRJSFValidationErrors(errors: ErrorObject[] = []): RJSFValidationError[] { - return errors.map((e: ErrorObject) => { - const { dataPath, keyword, message, params, schemaPath } = e; - const property = `${dataPath}`; - - // put data in expected format - return { - name: keyword, - property, - message, - params, // specific to ajv - stack: `${property} ${message}`.trim(), - schemaPath, - }; - }); - } - - /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use - * by the playground. Returns the `errors` from the validation - * - * @param schema - The schema against which to validate the form data * @param schema - * @param formData - The form data to validate - */ - rawValidation(schema: RJSFSchema, formData?: T): { errors?: Result[]; validationError?: Error } { - let validationError: Error | undefined = undefined; - try { - this.ajv.validate(schema, formData); - } catch (err) { - validationError = err as Error; - } - - const errors = this.ajv.errors || undefined; - - // Clear errors to prevent persistent errors, see #1104 - this.ajv.errors = null; - - return { errors: errors as unknown as Result[], validationError }; - } - - /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives - * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also - * supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and - * transform them in what ever way it chooses. - * - * @param formData - The form data to validate - * @param schema - The schema against which to validate the form data - * @param [customValidate] - An optional function that is used to perform custom validation - * @param [transformErrors] - An optional function that is used to transform errors after AJV validation - * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate` - */ - validateFormData( - formData: T | undefined, - schema: S, - customValidate?: CustomValidator, - transformErrors?: ErrorTransformer, - uiSchema?: UiSchema - ): ValidationData { - const rootSchema = schema; - - const rawErrors = this.rawValidation(schema, formData); - const { validationError } = rawErrors; - let errors = this.transformRJSFValidationErrors(rawErrors.errors); - - const noProperMetaSchema = - validationError && validationError.message && validationError.message.includes('no schema with key or ref '); - - if (noProperMetaSchema) { - errors = [...errors, { stack: validationError!.message }]; - } - if (typeof transformErrors === 'function') { - errors = transformErrors(errors, uiSchema); - } - - let errorSchema = toErrorSchema(errors); - - if (noProperMetaSchema) { - errorSchema = { - ...errorSchema, - ...{ - $schema: { - __errors: [validationError!.message], - }, - }, - }; - } - - if (typeof customValidate !== 'function') { - return { errors, errorSchema }; - } - - // Include form data with undefined values, which is required for custom validation. - const newFormData = getDefaultFormState(this, schema, formData, rootSchema, true) as T; - - const errorHandler = customValidate(newFormData, createErrorHandler(newFormData), uiSchema); - const userErrorSchema = unwrapErrorHandler(errorHandler); - return validationDataMerge({ errors, errorSchema }, userErrorSchema); - } - - /** - * This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. - * @param rootSchema - The root schema used to provide $ref resolutions - */ - handleSchemaUpdate(rootSchema: RJSFSchema): void { - const rootSchemaId = ROOT_SCHEMA_PREFIX; - // add the rootSchema ROOT_SCHEMA_PREFIX as id. - // if schema validator instance doesn't exist, add it. - // else 'handleRootSchemaChange' should be called if the root schema changes so we don't have to remove and recompile the schema every run. - if (this.ajv.getSchema(ROOT_SCHEMA_PREFIX) === undefined) { - this.ajv.addSchema(rootSchema, ROOT_SCHEMA_PREFIX); - } else if (!deepEquals(rootSchema, this.ajv.getSchema(ROOT_SCHEMA_PREFIX)?.schema)) { - this.ajv.removeSchema(rootSchemaId); - this.ajv.addSchema(rootSchema, rootSchemaId); - } - } - - /** Validates data against a schema, returning true if the data is valid, or - * false otherwise. If the schema is invalid, then this function will return - * false. - * - * @param schema - The schema against which to validate the form data * @param schema - * @param formData- - The form data to validate - * @param rootSchema - The root schema used to provide $ref resolutions - */ - isValid(schema: RJSFSchema, formData: T | undefined, rootSchema: RJSFSchema) { - try { - this.handleSchemaUpdate(rootSchema); - // then rewrite the schema ref's to point to the rootSchema - // this accounts for the case where schema have references to models - // that lives in the rootSchema but not in the schema in question. - const result = this.ajv.validate(withIdRefPrefix(schema), formData); - return result as boolean; - } catch (e) { - return false; - } - } -} diff --git a/packages/validator-ajv6/test/.eslintrc b/packages/validator-ajv6/test/.eslintrc deleted file mode 100644 index 796f216827..0000000000 --- a/packages/validator-ajv6/test/.eslintrc +++ /dev/null @@ -1,16 +0,0 @@ -{ - "env": { - "jest": true - }, - "globals": { - "expect": true - }, - "rules": { - "no-unused-vars": [ - 2, - { - "varsIgnorePattern": "^d$" - } - ] - } -} diff --git a/packages/validator-ajv6/test/createAjvInstance.test.ts b/packages/validator-ajv6/test/createAjvInstance.test.ts deleted file mode 100644 index 7d6c0f0538..0000000000 --- a/packages/validator-ajv6/test/createAjvInstance.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import Ajv, { Ajv as AjvType } from 'ajv'; - -import createAjvInstance, { AJV_CONFIG, COLOR_FORMAT_REGEX, DATA_URL_FORMAT_REGEX } from '../src/createAjvInstance'; -import { CustomValidatorOptionsType } from '../src'; - -jest.mock('ajv'); - -export const CUSTOM_OPTIONS: CustomValidatorOptionsType = { - additionalMetaSchemas: [require('ajv/lib/refs/json-schema-draft-04.json')], - customFormats: { - 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, - 'area-code': /\d{3}/, - }, - ajvOptionsOverrides: { - $data: true, - verbose: true, - }, -}; - -describe('createAjvInstance()', () => { - describe('no additional meta schemas, custom formats or ajv options overrides', () => { - let ajv: AjvType; - beforeAll(() => { - ajv = createAjvInstance(); - }); - afterAll(() => { - (Ajv as unknown as jest.Mock).mockClear(); - }); - it('expect a new Ajv to be constructed with the AJV_CONFIG', () => { - expect(Ajv).toHaveBeenCalledWith(AJV_CONFIG); - }); - it('addFormat() was called twice', () => { - expect(ajv.addFormat).toHaveBeenCalledTimes(2); - }); - it('the first addFormat() was for data-url', () => { - expect(ajv.addFormat).toHaveBeenNthCalledWith(1, 'data-url', DATA_URL_FORMAT_REGEX); - }); - it('the second addFormat() was for color', () => { - expect(ajv.addFormat).toHaveBeenNthCalledWith(2, 'color', COLOR_FORMAT_REGEX); - }); - it('addMetaSchema was not called', () => { - expect(ajv.addMetaSchema).not.toHaveBeenCalled(); - }); - }); - describe('has additional meta schemas, custom formats and ajv options overrides', () => { - let ajv: AjvType; - beforeAll(() => { - ajv = createAjvInstance( - CUSTOM_OPTIONS.additionalMetaSchemas, - CUSTOM_OPTIONS.customFormats, - CUSTOM_OPTIONS.ajvOptionsOverrides - ); - }); - afterAll(() => { - (Ajv as unknown as jest.Mock).mockClear(); - }); - it('expect a new Ajv to be constructed with the AJV_CONFIG', () => { - expect(Ajv).toHaveBeenCalledWith({ - ...AJV_CONFIG, - ...CUSTOM_OPTIONS.ajvOptionsOverrides, - }); - }); - it('addFormat() was called twice', () => { - expect(ajv.addFormat).toHaveBeenCalledTimes(4); - }); - it('the first addFormat() was for data-url', () => { - expect(ajv.addFormat).toHaveBeenNthCalledWith(1, 'data-url', DATA_URL_FORMAT_REGEX); - }); - it('the second addFormat() was for color', () => { - expect(ajv.addFormat).toHaveBeenNthCalledWith(2, 'color', COLOR_FORMAT_REGEX); - }); - it('the remaining addFormat() calls were for custom formats', () => { - Object.keys(CUSTOM_OPTIONS.customFormats!).forEach((key: string, i: number) => { - expect(ajv.addFormat).toHaveBeenNthCalledWith(3 + i, key, CUSTOM_OPTIONS.customFormats![key]); - }); - }); - it('addMetaSchema was not called', () => { - expect(ajv.addMetaSchema).toHaveBeenCalledWith(CUSTOM_OPTIONS.additionalMetaSchemas); - }); - }); -}); diff --git a/packages/validator-ajv6/test/customizeValidator.test.ts b/packages/validator-ajv6/test/customizeValidator.test.ts deleted file mode 100644 index 550a2590a2..0000000000 --- a/packages/validator-ajv6/test/customizeValidator.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import AJV6Validator from '../src/validator'; -import defaultValidator, { customizeValidator } from '../src'; -import { CUSTOM_OPTIONS } from './createAjvInstance.test'; - -jest.mock('../src/validator'); - -type TestType = { - foo: string; - bar: boolean; -}; - -describe('customizeValidator()', () => { - it('defaultValidator was created', () => { - expect(defaultValidator).toBeInstanceOf(AJV6Validator); - }); - it('defaultValidator was constructed with empty object', () => { - expect(AJV6Validator).toHaveBeenCalledWith({}); - }); - describe('passing options to customizeValidator', () => { - let custom: any; - beforeAll(() => { - (AJV6Validator as unknown as jest.Mock).mockClear(); - custom = customizeValidator(CUSTOM_OPTIONS); - }); - it('custom validator was created', () => { - expect(custom).toBeInstanceOf(AJV6Validator); - }); - it('defaultValidator was constructed with custom options object', () => { - expect(AJV6Validator).toHaveBeenCalledWith(CUSTOM_OPTIONS); - }); - }); -}); diff --git a/packages/validator-ajv6/test/tsconfig.json b/packages/validator-ajv6/test/tsconfig.json deleted file mode 100644 index ff0e009d67..0000000000 --- a/packages/validator-ajv6/test/tsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "extends": "../../../tsconfig.base.json", - "include": ["./", "./**/*.json"], - "compilerOptions": { - "rootDir": "./", - "noEmit": true, - "baseUrl": "../", - "jsx": "react-jsx" - }, - "references": [ - { - "path": "../src" - }, - { - "path": "../../utils/test" - } - ] -} diff --git a/packages/validator-ajv6/test/utilsTests/getTestValidator.ts b/packages/validator-ajv6/test/utilsTests/getTestValidator.ts deleted file mode 100644 index e977a0b45c..0000000000 --- a/packages/validator-ajv6/test/utilsTests/getTestValidator.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { - CustomValidator, - ErrorSchema, - ErrorTransformer, - RJSFSchema, - RJSFValidationError, - ValidationData, -} from '@rjsf/utils'; -// With Lerna active, the test world has access to the test suite via the symlink -import { TestValidatorType } from '@rjsf/utils/test/schema'; -import { customizeValidator, CustomValidatorOptionsType } from '../../src'; - -/** In this environment, a test validator merely creates an internal `AJV6` validator with the custom `options` and - * then forwards all calls into it. It is provided to mirror the test validator in the `utils` directory, except that - * the `setReturnValues()` function does nothing. - * - * @param options - */ -export default function getTestValidator(options: CustomValidatorOptionsType): TestValidatorType { - const validator = customizeValidator(options); - return { - validateFormData( - formData: T, - schema: RJSFSchema, - customValidate?: CustomValidator, - transformErrors?: ErrorTransformer - ): ValidationData { - return validator.validateFormData(formData, schema, customValidate, transformErrors); - }, - toErrorList(errorSchema?: ErrorSchema, fieldPath?: string[]): RJSFValidationError[] { - return validator.toErrorList(errorSchema, fieldPath); - }, - isValid(schema: RJSFSchema, formData: T | undefined, rootSchema: RJSFSchema): boolean { - return validator.isValid(schema, formData, rootSchema); - }, - rawValidation(schema: RJSFSchema, formData?: T): { errors?: Result[]; validationError?: Error } { - return validator.rawValidation(schema, formData); - }, - // This is intentionally a no-op as we are using the real validator here - setReturnValues() {}, - }; -} diff --git a/packages/validator-ajv6/test/utilsTests/schema.test.ts b/packages/validator-ajv6/test/utilsTests/schema.test.ts deleted file mode 100644 index a4d21c534f..0000000000 --- a/packages/validator-ajv6/test/utilsTests/schema.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -// With Lerna active, the test world has access to the test suite via the symlink -import { - getDefaultFormStateTest, - getDisplayLabelTest, - getClosestMatchingOptionTest, - getFirstMatchingOptionTest, - isFilesArrayTest, - isMultiSelectTest, - isSelectTest, - mergeValidationDataTest, - retrieveSchemaTest, - sanitizeDataForNewSchemaTest, - toIdSchemaTest, - toPathSchemaTest, -} from '@rjsf/utils/test/schema'; -import getTestValidator from './getTestValidator'; - -const testValidator = getTestValidator({}); - -// NOTE: to restrict which tests to run, you can temporarily comment out any tests you aren't needing -getDefaultFormStateTest(testValidator); -getDisplayLabelTest(testValidator); -getClosestMatchingOptionTest(testValidator); -getFirstMatchingOptionTest(testValidator); -isFilesArrayTest(testValidator); -isMultiSelectTest(testValidator); -isSelectTest(testValidator); -mergeValidationDataTest(testValidator); -retrieveSchemaTest(testValidator); -sanitizeDataForNewSchemaTest(testValidator); -toIdSchemaTest(testValidator); -toPathSchemaTest(testValidator); diff --git a/packages/validator-ajv6/test/validator.test.ts b/packages/validator-ajv6/test/validator.test.ts deleted file mode 100644 index c73d1936da..0000000000 --- a/packages/validator-ajv6/test/validator.test.ts +++ /dev/null @@ -1,560 +0,0 @@ -import { - ErrorSchema, - ErrorSchemaBuilder, - FormValidation, - RJSFSchema, - RJSFValidationError, - UiSchema, - ValidatorType, -} from '@rjsf/utils'; - -import AJV6Validator from '../src/validator'; - -const illFormedKey = "bar.`'[]()=+*&^%$#@!"; -const metaSchemaDraft4 = require('ajv/lib/refs/json-schema-draft-04.json'); -const metaSchemaDraft6 = require('ajv/lib/refs/json-schema-draft-06.json'); - -describe('AJV6Validator', () => { - let builder: ErrorSchemaBuilder; - beforeAll(() => { - builder = new ErrorSchemaBuilder(); - }); - afterEach(() => { - builder.resetAllErrors(); - }); - describe('default options', () => { - // Use the AJV6Validator to access the `withIdRefPrefix` function - let validator: AJV6Validator; - beforeAll(() => { - validator = new AJV6Validator({}); - }); - describe('validator.isValid()', () => { - it('should return true if the data is valid against the schema', () => { - const schema: RJSFSchema = { - type: 'object', - properties: { - foo: { type: 'string' }, - }, - }; - - expect(validator.isValid(schema, { foo: 'bar' }, schema)).toBe(true); - }); - it('should return false if the data is not valid against the schema', () => { - const schema: RJSFSchema = { - type: 'object', - properties: { - foo: { type: 'string' }, - }, - }; - - expect(validator.isValid(schema, { foo: 12345 }, schema)).toBe(false); - }); - it('should return false if the schema is invalid', () => { - const schema: RJSFSchema = 'foobarbaz' as unknown as RJSFSchema; - - expect(validator.isValid(schema, { foo: 'bar' }, schema)).toBe(false); - }); - it('should return true if the data is valid against the schema including refs to rootSchema', () => { - const schema: RJSFSchema = { - anyOf: [{ $ref: '#/definitions/foo' }], - }; - const rootSchema: RJSFSchema = { - definitions: { - foo: { - properties: { - name: { type: 'string' }, - }, - }, - }, - }; - const formData = { - name: 'John Doe', - }; - - expect(validator.isValid(schema, formData, rootSchema)).toBe(true); - }); - }); - describe('validator.toErrorList()', () => { - it('should return empty list for unspecified errorSchema', () => { - expect(validator.toErrorList()).toEqual([]); - }); - it('should convert an errorSchema into a flat list', () => { - const errorSchema = builder - .addErrors(['err1', 'err2']) - .addErrors(['err3', 'err4'], 'a.b') - .addErrors(['err5'], 'c').ErrorSchema; - expect(validator.toErrorList(errorSchema)).toEqual([ - { property: '.', message: 'err1', stack: '. err1' }, - { property: '.', message: 'err2', stack: '. err2' }, - { property: '.a.b', message: 'err3', stack: '.a.b err3' }, - { property: '.a.b', message: 'err4', stack: '.a.b err4' }, - { property: '.c', message: 'err5', stack: '.c err5' }, - ]); - }); - }); - describe('validator.validateFormData()', () => { - describe('No custom validate function, single value', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - foo: { type: 'string' }, - [illFormedKey]: { type: 'string' }, - }, - }; - const result = validator.validateFormData({ foo: 42, [illFormedKey]: 41 }, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - - it('should return an error list', () => { - expect(errors).toHaveLength(2); - expect(errors[0].message).toEqual('should be string'); - expect(errors[1].message).toEqual('should be string'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.foo!.__errors).toHaveLength(1); - expect(errorSchema.foo!.__errors![0]).toEqual('should be string'); - expect(errorSchema[illFormedKey]!.__errors).toHaveLength(1); - expect(errorSchema[illFormedKey]!.__errors![0]).toEqual('should be string'); - }); - }); - describe('Validating multipleOf with a float', () => { - let errors: RJSFValidationError[]; - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - price: { - title: 'Price per task ($)', - type: 'number', - multipleOf: 0.01, - minimum: 0, - }, - }, - }; - const result = validator.validateFormData({ price: 0.14 }, schema); - errors = result.errors; - }); - it('should not return an error', () => { - expect(errors).toHaveLength(0); - }); - }); - describe('Validating multipleOf with a float, with multiple errors', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - price: { - title: 'Price per task ($)', - type: 'number', - multipleOf: 0.03, - minimum: 1, - }, - }, - }; - const result = validator.validateFormData({ price: 0.14 }, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should have 2 errors', () => { - expect(errors).toHaveLength(2); - }); - it('first error is for minimum', () => { - expect(errors[0].message).toEqual('should be >= 1'); - }); - it('first error is for multipleOf', () => { - expect(errors[1].message).toEqual('should be multiple of 0.03'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.price!.__errors).toHaveLength(2); - expect(errorSchema.price!.__errors).toEqual(['should be >= 1', 'should be multiple of 0.03']); - }); - }); - describe('Validating required fields', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - describe('formData is not provided at top level', () => { - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - required: ['pass1', 'pass2'], - properties: { - pass1: { type: 'string' }, - pass2: { type: 'string' }, - }, - }; - - const formData = { pass1: 'a' }; - const result = validator.validateFormData(formData, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].stack).toEqual('.pass2 is a required property'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.pass2!.__errors).toHaveLength(1); - expect(errorSchema.pass2!.__errors![0]).toEqual('is a required property'); - }); - }); - describe('formData is not provided for nested child', () => { - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - nested: { - type: 'object', - required: ['pass1', 'pass2'], - properties: { - pass1: { type: 'string' }, - pass2: { type: 'string' }, - }, - }, - }, - }; - - const formData = { nested: { pass1: 'a' } }; - const result = validator.validateFormData(formData, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].stack).toEqual('.nested.pass2 is a required property'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.nested!.pass2!.__errors).toHaveLength(1); - expect(errorSchema.nested!.pass2!.__errors![0]).toEqual('is a required property'); - }); - }); - }); - describe('No custom validate function, single additionalProperties value', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - additionalProperties: { - type: 'string', - }, - }; - const result = validator.validateFormData({ foo: 42 }, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].message).toEqual('should be string'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.foo!.__errors).toHaveLength(1); - expect(errorSchema.foo!.__errors![0]).toEqual('should be string'); - }); - }); - describe('TransformErrors', () => { - let errors: RJSFValidationError[]; - let newErrorMessage: string; - let transformErrors: jest.Mock; - let uiSchema: UiSchema; - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - foo: { type: 'string' }, - [illFormedKey]: { type: 'string' }, - }, - }; - uiSchema = { - foo: { 'ui:label': false }, - }; - newErrorMessage = 'Better error message'; - transformErrors = jest.fn((errors: RJSFValidationError[]) => { - return [Object.assign({}, errors[0], { message: newErrorMessage })]; - }); - const result = validator.validateFormData( - { foo: 42, [illFormedKey]: 41 }, - schema, - undefined, - transformErrors, - uiSchema - ); - errors = result.errors; - }); - - it('should use transformErrors function', () => { - expect(errors).not.toHaveLength(0); - expect(errors[0].message).toEqual(newErrorMessage); - }); - it('transformErrors function was called with uiSchema', () => { - expect(transformErrors).toHaveBeenCalledWith(expect.any(Array), uiSchema); - }); - }); - describe('Custom validate function', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - let validate: jest.Mock; - let uiSchema: UiSchema; - beforeAll(() => { - uiSchema = { - foo: { 'ui:label': false }, - }; - - validate = jest.fn((formData: any, errors: FormValidation) => { - if (formData.pass1 !== formData.pass2) { - errors.pass2!.addError('passwords don`t match.'); - } - return errors; - }); - }); - describe('formData is provided', () => { - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - required: ['pass1', 'pass2'], - properties: { - pass1: { type: 'string' }, - pass2: { type: 'string' }, - foo: { type: 'array', items: { type: 'string' } }, // Adding an array for test coverage - }, - }; - const formData = { pass1: 'a', pass2: 'b', foo: ['a'] }; - const result = validator.validateFormData(formData, schema, validate, undefined, uiSchema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].stack).toEqual('.pass2 passwords don`t match.'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.pass2!.__errors).toHaveLength(1); - expect(errorSchema.pass2!.__errors![0]).toEqual('passwords don`t match.'); - }); - it('validate function was called with uiSchema', () => { - expect(validate).toHaveBeenCalledWith(expect.any(Object), expect.any(Object), uiSchema); - }); - }); - describe('formData is missing data', () => { - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - pass1: { type: 'string' }, - pass2: { type: 'string' }, - }, - }; - const formData = { pass1: 'a' }; - const result = validator.validateFormData(formData, schema, validate); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].stack).toEqual('.pass2 passwords don`t match.'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.pass2!.__errors).toHaveLength(1); - expect(errorSchema.pass2!.__errors![0]).toEqual('passwords don`t match.'); - }); - it('validate function was called with undefined uiSchema', () => { - expect(validate).toHaveBeenCalledWith(expect.any(Object), expect.any(Object), undefined); - }); - }); - }); - describe('Data-Url validation', () => { - let schema: RJSFSchema; - beforeAll(() => { - schema = { - type: 'object', - properties: { - dataUrlWithName: { type: 'string', format: 'data-url' }, - dataUrlWithoutName: { type: 'string', format: 'data-url' }, - }, - }; - }); - it('Data-Url with name is accepted', () => { - const formData = { - dataUrlWithName: 'data:text/plain;name=file1.txt;base64,x=', - }; - const result = validator.validateFormData(formData, schema); - expect(result.errors).toHaveLength(0); - }); - it('Data-Url without name is accepted', () => { - const formData = { - dataUrlWithoutName: 'data:text/plain;base64,x=', - }; - const result = validator.validateFormData(formData, schema); - expect(result.errors).toHaveLength(0); - }); - }); - describe('Invalid schema', () => { - let errors: RJSFValidationError[]; - let errorSchema: ErrorSchema; - - beforeAll(() => { - const schema: RJSFSchema = { - type: 'object', - properties: { - foo: { - type: 'string', - required: 'invalid_type_non_array' as unknown as string[], - }, - }, - }; - const result = validator.validateFormData({ foo: 42 }, schema); - errors = result.errors; - errorSchema = result.errorSchema; - }); - it('should return an error list', () => { - expect(errors).toHaveLength(1); - expect(errors[0].name).toEqual('type'); - expect(errors[0].property).toEqual(".properties['foo'].required"); - // TODO: This schema path is wrong due to a bug in ajv; change this test when https://github.com/ajv-validator/ajv/issues/512 is fixed. - expect(errors[0].schemaPath).toEqual('#/definitions/stringArray/type'); - expect(errors[0].message).toEqual('should be array'); - }); - it('should return an errorSchema', () => { - expect(errorSchema.properties!.foo!.required!.__errors).toHaveLength(1); - expect(errorSchema.properties!.foo!.required!.__errors![0]).toEqual('should be array'); - }); - }); - }); - }); - describe('validator.validateFormData(), custom options', () => { - let validator: AJV6Validator; - let schema: RJSFSchema; - beforeAll(() => { - validator = new AJV6Validator({}); - schema = { - $ref: '#/definitions/Dataset', - $schema: 'http://json-schema.org/draft-04/schema#', - definitions: { - Dataset: { - properties: { - datasetId: { - pattern: '\\d+', - type: 'string', - }, - }, - required: ['datasetId'], - type: 'object', - }, - }, - }; - }); - it('should return a validation error about meta schema when meta schema is not defined', () => { - const errors = validator.validateFormData({ datasetId: 'some kind of text' }, schema); - const errMessage = 'no schema with key or ref "http://json-schema.org/draft-04/schema#"'; - expect(errors.errors).toEqual([{ stack: errMessage }]); - expect(errors.errorSchema).toEqual({ - $schema: { __errors: [errMessage] }, - }); - }); - describe('validating using single custom meta schema', () => { - let errors: RJSFValidationError[]; - beforeAll(() => { - validator = new AJV6Validator({ - additionalMetaSchemas: [metaSchemaDraft4], - }); - const result = validator.validateFormData({ datasetId: 'some kind of text' }, schema); - errors = result.errors; - }); - it('should return 1 error about formData', () => { - expect(errors).toHaveLength(1); - }); - it('has a pattern match validation error about formData', () => { - expect(errors[0].stack).toEqual('.datasetId should match pattern "\\d+"'); - }); - }); - describe('validating using several custom meta schemas', () => { - let errors: RJSFValidationError[]; - - beforeAll(() => { - validator = new AJV6Validator({ - additionalMetaSchemas: [metaSchemaDraft4, metaSchemaDraft6], - }); - const result = validator.validateFormData({ datasetId: 'some kind of text' }, schema); - errors = result.errors; - }); - it('should return 1 error about formData', () => { - expect(errors).toHaveLength(1); - }); - it('has a pattern match validation error about formData', () => { - expect(errors[0].stack).toEqual('.datasetId should match pattern "\\d+"'); - }); - }); - describe('validating using custom string formats', () => { - let validator: ValidatorType; - let schema: RJSFSchema; - beforeAll(() => { - validator = new AJV6Validator({}); - schema = { - type: 'object', - properties: { - phone: { - type: 'string', - format: 'phone-us', - }, - }, - }; - }); - it('should not return a validation error if unknown string format is used', () => { - const result = validator.validateFormData({ phone: '800.555.2368' }, schema); - expect(result.errors.length).toEqual(0); - }); - describe('validating using a custom formats', () => { - let errors: RJSFValidationError[]; - - beforeAll(() => { - validator = new AJV6Validator({ - customFormats: { - 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/, - 'area-code': /\d{3}/, - }, - }); - const result = validator.validateFormData({ phone: '800.555.2368' }, schema); - errors = result.errors; - }); - it('should return 1 error about formData', () => { - expect(errors).toHaveLength(1); - }); - it('should return a validation error about formData', () => { - expect(errors[0].stack).toEqual('.phone should match format "phone-us"'); - }); - describe('prop updates with new custom formats are accepted', () => { - beforeAll(() => { - const result = validator.validateFormData( - { phone: 'abc' }, - { - type: 'object', - properties: { - phone: { - type: 'string', - format: 'area-code', - }, - }, - } - ); - errors = result.errors; - }); - - it('should return 1 error about formData', () => { - expect(errors).toHaveLength(1); - }); - it('should return a validation error about formData', () => { - expect(errors[0].stack).toEqual('.phone should match format "area-code"'); - }); - }); - }); - }); - }); -}); diff --git a/packages/validator-ajv6/tsconfig.json b/packages/validator-ajv6/tsconfig.json deleted file mode 100644 index 3259ae5965..0000000000 --- a/packages/validator-ajv6/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "files": [], - "references": [ - { - "path": "./src" - }, - { - "path": "./test" - }, - { - "path": "../utils" - } - ] -} diff --git a/packages/validator-ajv8/babel.config.js b/packages/validator-ajv8/babel.config.js index 7a98797215..5f772a56c4 100644 --- a/packages/validator-ajv8/babel.config.js +++ b/packages/validator-ajv8/babel.config.js @@ -1,3 +1,3 @@ -const defaultConfig = require('../validator-ajv6/babel.config'); +const defaultConfig = require('../../babel.config'); module.exports = defaultConfig; diff --git a/tsconfig.json b/tsconfig.json index 16aebdf13d..731d4641dc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -31,9 +31,6 @@ { "path": "./packages/utils" }, - { - "path": "./packages/validator-ajv6" - }, { "path": "./packages/validator-ajv8" },