Skip to content

Commit

Permalink
detect cycle wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Bosak committed Aug 9, 2024
1 parent 124e714 commit 4891bd9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
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
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
42 changes: 42 additions & 0 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 lowerCase from 'lodash/lowerCase';
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 @@ -410,4 +412,44 @@ export default class JSONSchemaBridge extends Bridge {
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;
}
}

0 comments on commit 4891bd9

Please sign in to comment.