Skip to content

Commit

Permalink
fix: ajv resolver to work with unlimited layers of nesting (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoRKin authored Jun 10, 2022
1 parent 0cad904 commit 692fe65
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
14 changes: 12 additions & 2 deletions ajv/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Field, InternalFieldName } from 'react-hook-form';
interface Data {
username: string;
password: string;
deepObject: { data: string };
deepObject: { data: string; twoLayersDeep: { name: string } };
}

export const schema: JSONSchemaType<Data> = {
Expand All @@ -26,8 +26,14 @@ export const schema: JSONSchemaType<Data> = {
nullable: true,
properties: {
data: { type: 'string' },
twoLayersDeep: {
type: 'object',
properties: { name: { type: 'string' } },
additionalProperties: false,
required: ['name'],
},
},
required: ['data'],
required: ['data', 'twoLayersDeep'],
},
},
required: ['username', 'password', 'deepObject'],
Expand All @@ -38,6 +44,9 @@ export const validData: Data = {
username: 'jsun969',
password: 'validPassword',
deepObject: {
twoLayersDeep: {
name: 'deeper',
},
data: 'data',
},
};
Expand All @@ -47,6 +56,7 @@ export const invalidData = {
password: 'invalid-password',
deepObject: {
data: 233,
twoLayersDeep: { name: 123 }
},
};

Expand Down
34 changes: 34 additions & 0 deletions ajv/src/__tests__/__snapshots__/ajv.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ Object {
"type": "must be string",
},
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
"types": Object {
"type": "must be string",
},
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -104,6 +114,16 @@ Object {
"type": "must be string",
},
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
"types": Object {
"type": "must be string",
},
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -139,6 +159,13 @@ Object {
"ref": undefined,
"type": "type",
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down Expand Up @@ -168,6 +195,13 @@ Object {
"ref": undefined,
"type": "type",
},
"twoLayersDeep": Object {
"name": Object {
"message": "must be string",
"ref": undefined,
"type": "type",
},
},
},
"password": Object {
"message": "One uppercase character",
Expand Down
2 changes: 1 addition & 1 deletion ajv/src/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const parseErrorSchema = (

return ajvErrors.reduce<Record<string, FieldError>>((previous, error) => {
// `/deepObject/data` -> `deepObject.data`
const path = error.instancePath.substring(1).replace('/', '.');
const path = error.instancePath.substring(1).replace(/\//g, '.');

if (!previous[path]) {
previous[path] = {
Expand Down

0 comments on commit 692fe65

Please sign in to comment.