Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ajv dependency #50

Merged
merged 3 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"testRegex": "(test/.*\\.test.js)$"
},
"dependencies": {
"ajv": "^6.10.2",
"ajv": "^8.6.2",
"lodash": "^4.17.20"
},
"devDependencies": {
Expand Down
21 changes: 6 additions & 15 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type ValidationOptions = {
*/

type ValidationError = {|
dataPath: string,
instancePath: string,
keyword: string,
params: Object
|};
Expand Down Expand Up @@ -60,10 +60,10 @@ export function getErrorPath(error: ValidationError): string {
let key;

// TODO: Handle nested properties.
if (error.dataPath.startsWith('.')) {
key = error.dataPath.substring(1);
if (error.instancePath.startsWith('/')) {
key = error.instancePath.substring(1);
} else {
key = error.dataPath.substring(2, error.dataPath.length - 2);
key = error.instancePath.substring(2, error.instancePath.length - 2);
}

switch (error.keyword) {
Expand Down Expand Up @@ -131,17 +131,8 @@ export type Validate = (schema: Object, values: Object, options?: ValidationOpti
* Export `validate`.
*/

export default function validate(schema: Object, values: Object, options?: ValidationOptions): FieldErrors {
const { keywords, ...restOptions } = options ?? {};
const ajv = new Ajv(merge({}, restOptions, { $data: true, allErrors: true }));

// TODO: Remove this when ajv adds support for passing keywords in the contructor.
// https://github.com/epoberezkin/ajv/issues/1136
if (keywords) {
for (const [keyword, config] of Object.entries(keywords)) {
ajv.addKeyword(keyword, config);
}
}
export default function validate(schema: Object, values: Object, options?: ValidationOptions = {}): FieldErrors {
const ajv = new Ajv(merge({}, options, { $data: true, allErrors: true }));

if (ajv.validate(schema, values)) {
return {};
Expand Down
24 changes: 13 additions & 11 deletions test/src/hooks/use-form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ describe('useForm hook', () => {
it('should validate the initial values', () => {
const { result } = renderHook(() => useForm({
initialValues: {},
jsonSchema: { required: ['foo'] },
jsonSchema: {
required: ['foo'],
type: 'object'
},
onSubmit: () => {}
}));

Expand Down Expand Up @@ -604,16 +607,15 @@ describe('useForm hook', () => {
},
onSubmit: () => {},
validationOptions: {
keywords: {
isBar: {
type: 'string',
validate: () => true
},
isFoo: {
type: 'string',
validate: () => false
}
}
keywords: [{
keyword: 'isBar',
type: 'string',
validate: () => true
}, {
keyword: 'isFoo',
type: 'string',
validate: () => false
}]
}
}));

Expand Down
39 changes: 22 additions & 17 deletions test/src/utils/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ describe('validate', () => {
maxItems: 1,
type: 'array'
}
}
},
type: 'object'
}, {
foo: [1, 2]
});
Expand All @@ -141,7 +142,8 @@ describe('validate', () => {
minItems: 2,
type: 'array'
}
}
},
type: 'object'
}, {
foo: [1]
});
Expand All @@ -161,7 +163,8 @@ describe('validate', () => {
maxProperties: 1,
type: 'object'
}
}
},
type: 'object'
}, {
foo: {
bar: 'qux',
Expand All @@ -182,9 +185,10 @@ describe('validate', () => {
properties: {
foo: {
minProperties: 2,
type: 'string'
type: 'object'
}
}
},
type: 'object'
}, {
foo: {
bar: 'qux'
Expand All @@ -206,7 +210,8 @@ describe('validate', () => {
maximum: 1,
type: 'number'
}
}
},
type: 'object'
}, {
foo: 2
});
Expand All @@ -226,7 +231,8 @@ describe('validate', () => {
minimum: 2,
type: 'number'
}
}
},
type: 'object'
}, {
foo: 1
});
Expand Down Expand Up @@ -286,16 +292,15 @@ describe('validate', () => {
bar: '123',
foo: '123'
}, {
keywords: {
isBar: {
type: 'string',
validate: () => true
},
isFoo: {
type: 'string',
validate: () => false
}
}
keywords: [{
keyword: 'isBar',
type: 'string',
validate: () => true
}, {
keyword: 'isFoo',
type: 'string',
validate: () => false
}]
});

expect(result).toEqual({
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,16 @@ ajv@^6.10.2, ajv@^6.5.5, ajv@^6.9.1:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"

ajv@^8.6.2:
version "8.6.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-8.6.2.tgz#2fb45e0e5fcbc0813326c1c3da535d1881bb0571"
integrity sha512-9807RlWAgT564wT+DjeyU5OFMPjmzxVobvDFmNAhY+5zD6A2ly3jDp6sgnfyDtlIQ+7H97oc/DGCzzfu9rjw9w==
dependencies:
fast-deep-equal "^3.1.1"
json-schema-traverse "^1.0.0"
require-from-string "^2.0.2"
uri-js "^4.2.2"

ansi-escapes@^3.0.0, ansi-escapes@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.2.0.tgz#8780b98ff9dbf5638152d1f1fe5c1d7b4442976b"
Expand Down Expand Up @@ -2853,6 +2863,11 @@ fast-deep-equal@^2.0.1:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz#7b05218ddf9667bf7f370bf7fdb2cb15fdd0aa49"
integrity sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=

fast-deep-equal@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-glob@^3.0.3:
version "3.0.4"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.0.4.tgz#d484a41005cb6faeb399b951fd1bd70ddaebb602"
Expand Down Expand Up @@ -4305,6 +4320,11 @@ json-schema-traverse@^0.4.1:
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"
integrity sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==

json-schema-traverse@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==

json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
Expand Down Expand Up @@ -5715,6 +5735,11 @@ require-directory@^2.1.1:
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha1-jGStX9MNqxyXbiNE/+f3kqam30I=

require-from-string@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==

require-main-filename@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"
Expand Down