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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"husky": "8.0.1",
"invariant": "^2.0.0",
"jest": "27.0.6",
"json-schema-traverse": "1.0.0",
"lerna": "6.0.1",
"lint-staged": "13.0.3",
"lodash": "^4.0.0",
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 @@ -889,7 +909,9 @@ describe('JSONSchemaBridge', () => {
'complexNames',
'password',
'passwordNumeric',
'recursive',
'recursiveObject',
'recursiveArray',
'bloodline',
'arrayWithAllOf',
'nonObjectAnyOf',
'nonObjectAnyOfRequired',
Expand All @@ -912,13 +934,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 @@ -34,6 +34,7 @@
],
"dependencies": {
"invariant": "^2.0.0",
"json-schema-traverse": "^1.0.0",
"lodash": "^4.0.0",
"tslib": "^2.2.0",
"uniforms": "^4.0.0-alpha.5"
Expand Down
48 changes: 46 additions & 2 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import memoize from 'lodash/memoize';
import upperFirst from 'lodash/upperFirst';
import { Bridge, UnknownObject, joinName } from 'uniforms';
import traverse from "json-schema-traverse";


function fieldInvariant(name: string, condition: boolean): asserts condition {
invariant(condition, 'Field not found in schema: "%s"', name);
Expand Down Expand Up @@ -273,13 +275,15 @@
}

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 Expand Up @@ -408,4 +412,44 @@
getValidator() {
return this.validator;
}

detectCycle() {
const schema = this.schema;
const visited = new Set();
const detectCycle = (name: string) => {
if (visited.has(name)) {
return true;
}
visited.add(name);
const field = this.getField(name);
if (field.type === Object) {
return Object.keys(field.properties).some(detectCycle);
}
if (field.type === Array) {
return field.items.some(detectCycle);
}
return false;
};
return (name: string) => detectCycle(name);
}

private buildGraph() {
const schema;

Check failure on line 437 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

'const' declarations must be initialized.

Check failure on line 437 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Variable 'schema' implicitly has an 'any' type.
const graph = new Map();

function pre(schema, jsonPointer, rootSchema, parentJSON, parentKeyword, parentSchema, indexOrProperty) {

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'schema' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'jsonPointer' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'rootSchema' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'parentJSON' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'parentKeyword' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'parentSchema' implicitly has an 'any' type.

Check failure on line 440 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Parameter 'indexOrProperty' implicitly has an 'any' type.
if (schema && schema.$ref) {
const ref = schema.$ref;
const parentPointer = jsonPointer.split('/').slice(0, -2).join('/') || '#';
if (!graph.has(parentPointer)) {
graph.set(parentPointer, []);
}
graph.get(parentPointer).push(removeHashPrefix(ref));

Check failure on line 447 in packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts

View workflow job for this annotation

GitHub Actions / CI (14.x)

Cannot find name 'removeHashPrefix'.
}
}

traverse(schema, { cb: { pre } });

return graph;
}
}
Loading