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

Recursive schema evaluation - stop evaluating initial value for empty array #1282

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
"@testing-library/user-event": "14.4.3",
"@types/jest": "26.0.20",
"@types/node": "18.19.50",
"buffer": "^5.5.0||^6.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two different versions? It is dev depenendency, not a peer dependency. Why is this needed? Where is this used?

"eslint-config-vazco": "7.4.0",
"eslint-import-resolver-alias": "1.1.2",
"eslint-import-resolver-typescript": "2.3.0",
"husky": "8.0.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"json-schema-traverse": "1.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it added here? It was already added to the json bridge package.

"lint-staged": "13.0.3",
"process": "^0.11.10",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

"rimraf": "3.0.2",
"stylelint": "13.8.0",
"stylelint-config-prettier": "8.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ describe('JSONSchemaBridge', () => {
firstName: { type: 'string', default: 'John' },
middleName: { $ref: '#/definitions/lastName' },
lastName: { type: 'string' },
recursive: {
recursiveObject: {
type: 'object',
properties: {
field: { type: 'string' },
recursive: { $ref: '#/definitions/recursive' },
recursiveObject: { $ref: '#/definitions/recursiveObject' },
},
},
recursiveArray: {
type: 'array',
items: { $ref: '#/definitions/recursiveArray' },
},
bloodlineNode: {
type: 'object',
properties: {
name: { type: 'string' },
children: {
type: 'array',
items: { $ref: '#/definitions/bloodlineNode' },
},
},
},
},
Expand Down Expand Up @@ -135,7 +149,13 @@ describe('JSONSchemaBridge', () => {
minimum: 1000,
multipleOf: 3,
},
recursive: { $ref: '#/definitions/recursive' },
recursiveObject: { $ref: '#/definitions/recursiveObject' },
recursiveArray: {
$ref: '#/definitions/recursiveArray',
},
bloodline: {
$ref: '#/definitions/bloodlineNode',
},
arrayWithAllOf: {
type: 'array',
items: {
Expand Down Expand Up @@ -888,7 +908,9 @@ describe('JSONSchemaBridge', () => {
'complexNames',
'password',
'passwordNumeric',
'recursive',
'recursiveObject',
'recursiveArray',
'bloodline',
'arrayWithAllOf',
'nonObjectAnyOf',
'nonObjectAnyOfRequired',
Expand All @@ -911,13 +933,24 @@ describe('JSONSchemaBridge', () => {
]);
});

it('works with recursive types', () => {
expect(bridge.getSubfields('recursive')).toEqual(['field', 'recursive']);
expect(bridge.getSubfields('recursive.recursive')).toEqual([
it('works with recursive types - object', () => {
expect(bridge.getSubfields('recursiveObject')).toEqual([
'field',
'recursiveObject',
]);
expect(bridge.getSubfields('recursiveObject.recursiveObject')).toEqual([
'field',
'recursive',
'recursiveObject',
]);
});
it('works with recursive types - array', () => {
expect(bridge.getSubfields('recursiveArray')).toEqual([]);
});

it('works with recursive types - mixed', () => {
expect(bridge.getSubfields('bloodline')).toEqual(['name', 'children']);
expect(bridge.getSubfields('bloodline.children')).toEqual([]);
});

it('works with primitives', () => {
expect(bridge.getSubfields('personalData.firstName')).toEqual([]);
Expand Down
1 change: 1 addition & 0 deletions packages/uniforms-bridge-json-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"invariant": "^2.0.0",
"json-schema-traverse": "^1.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you use it?

"lodash": "^4.0.0",
"tslib": "2.8.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ export default class JSONSchemaBridge extends Bridge {
}

if (type === 'array') {
if (!field.minItems) {
return [];
}
const item = this.getInitialValue(joinName(name, '$'));
if (item === undefined) {
return [];
}

const length = field.minItems || 0;
return Array.from({ length }, () => item);
return Array.from({ length: field.minItems }, () => item);
}

if (type === 'object') {
Expand Down
20 changes: 17 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading