From 150285b0f7c8d0d6527e7bb23e419d0cd514e3c3 Mon Sep 17 00:00:00 2001 From: janrywang Date: Sat, 30 Jan 2021 16:28:10 +0800 Subject: [PATCH] test(json-schema): add some tests --- package.json | 2 + .../__snapshots__/schema.spec.ts.snap | 1239 +++++++++++++++++ .../json-schema/src/__tests__/compat.spec.ts | 0 .../src/__tests__/complier.spec.ts | 146 ++ .../json-schema/src/__tests__/patches.spec.ts | 156 +++ .../json-schema/src/__tests__/schema.spec.ts | 251 ++++ packages/json-schema/src/complier.ts | 21 +- packages/json-schema/src/index.ts | 2 +- .../SPECIFICATION_1_0.ts | 8 +- .../src/{polyfills => patches}/index.ts | 0 packages/json-schema/src/schema.ts | 18 +- packages/json-schema/src/types.ts | 6 +- 12 files changed, 1815 insertions(+), 34 deletions(-) create mode 100644 packages/json-schema/src/__tests__/__snapshots__/schema.spec.ts.snap delete mode 100644 packages/json-schema/src/__tests__/compat.spec.ts create mode 100644 packages/json-schema/src/__tests__/patches.spec.ts rename packages/json-schema/src/{polyfills => patches}/SPECIFICATION_1_0.ts (93%) rename packages/json-schema/src/{polyfills => patches}/index.ts (100%) diff --git a/package.json b/package.json index 2d32f623476..6d405c31de1 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ "test:sc:watch": "npm run test:sc -- --watch", "test:core": "jest packages/core/src/__tests__/*.spec.ts", "test:core:watch": "npm run test:core --- --watch", + "test:schema": "jest packages/json-schema/src/__tests__/*.spec.ts", + "test:schema:watch": "npm run test:schema --- --watch", "test:react": "jest packages/react/src/__tests__/*.spec.tsx", "test:react:watch": "npm run test:react --- --watch", "test:react-schema": "jest packages/react-schema-field/src/__tests__/*.spec.tsx", diff --git a/packages/json-schema/src/__tests__/__snapshots__/schema.spec.ts.snap b/packages/json-schema/src/__tests__/__snapshots__/schema.spec.ts.snap new file mode 100644 index 00000000000..59fd82049b7 --- /dev/null +++ b/packages/json-schema/src/__tests__/__snapshots__/schema.spec.ts.snap @@ -0,0 +1,1239 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`all methods 1`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalProperties": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "patternProperties": Object { + "^[a-zA-Z0-9]*$": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "properties": Object { + "made": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "made", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 1, + }, + "model": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "model", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 2, + }, + "year": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "year", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 0, + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "version": "2.0", + "x-decorator": "FormItem", + }, + }, + "properties": Object { + "array": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "array", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "string": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "description": null, + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "string", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": Array [ + Object { + "fullfill": Object { + "schema": Object {}, + }, + "target": "xxx", + "when": true, + }, + ], + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": null, +} +`; + +exports[`all methods 2`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalProperties": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "patternProperties": Object { + "^[a-zA-Z0-9]*$": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "properties": Object { + "made": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "made", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 1, + }, + "model": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "model", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 2, + }, + "year": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "year", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-index": 0, + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "version": "2.0", + "x-decorator": "FormItem", + }, + }, + "properties": Object { + "array": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "array", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "string": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "description": null, + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "string", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": Array [ + Object { + "fullfill": Object { + "schema": Object {}, + }, + "target": "xxx", + "when": true, + }, + ], + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": null, +} +`; + +exports[`all methods 3`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalProperties": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "patternProperties": Object {}, + "properties": Object { + "array": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "integer", + "version": "2.0", + "x-decorator": "FormItem", + }, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "array", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": null, +} +`; + +exports[`all methods 4`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "description": null, + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "string", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "string", + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + "x-reactions": Array [ + Object { + "fullfill": Object { + "schema": Object {}, + }, + "target": "xxx", + "when": true, + }, + ], +} +`; + +exports[`all methods 5`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": undefined, + "additionalProperties": undefined, + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + undefined, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "properties": Object { + "xxx": undefined, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", +} +`; + +exports[`all methods 6`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": undefined, + "additionalProperties": undefined, + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + undefined, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "properties": Object { + "xxx": undefined, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", +} +`; + +exports[`all props 1`] = ` +Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalProperties": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "description": "description", + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "patternProperties": Object { + "^[a-zA-Z0-9]*$": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "properties": Object { + "made": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "made", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "model": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "model", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "year": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "year", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "version": "2.0", + "x-decorator": "FormItem", + }, + }, + "properties": Object { + "array": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "additionalItems": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "number", + "version": "2.0", + "x-decorator": "FormItem", + }, + "complie": [Function], + "fromJSON": [Function], + "items": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "array", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "array", + "version": "2.0", + "x-decorator": "FormItem", + }, + "array2": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "items": Array [ + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-decorator": "FormItem", + }, + Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", + }, + ], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "array2", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "array", + "version": "2.0", + "x-decorator": "FormItem", + }, + "boolean": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "default": false, + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "boolean", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "boolean", + "version": "2.0", + "x-decorator": "FormItem", + }, + "date": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "default": "2020-12-23", + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "date", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "date", + "version": "2.0", + "x-decorator": "FormItem", + }, + "datetime": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "default": "2020-12-23 23:00:00", + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "datetime", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "datetime", + "version": "2.0", + "x-decorator": "FormItem", + }, + "number": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "default": 100, + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "number", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "number", + "version": "2.0", + "x-decorator": "FormItem", + }, + "string": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "default": "default", + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "string", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "required": true, + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "string", + "version": "2.0", + "x-component": "Input", + "x-component-props": Object { + "placeholder": "placeholder", + }, + "x-decorator": "FormItem", + "x-decorator-props": Object { + "labelCol": 3, + }, + "x-disabled": true, + "x-display": "visible", + "x-editable": false, + "x-hidden": false, + "x-pattern": "readPretty", + "x-reactions": Array [ + Object { + "target": "xxx", + "when": "{{aa > bb}}", + }, + ], + "x-read-only": true, + "x-validator": Array [ + "phone", + ], + }, + "void": Object { + "_isJSONSchemaObject": true, + "addPatternProperty": [Function], + "addProperty": [Function], + "complie": [Function], + "fromJSON": [Function], + "mapPatternProperties": [Function], + "mapProperties": [Function], + "name": "void", + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "toJSON": [Function], + "type": "void", + "version": "2.0", + }, + }, + "reducePatternProperties": [Function], + "reduceProperties": [Function], + "removePatternProperty": [Function], + "removeProperty": [Function], + "setAdditionalItems": [Function], + "setAdditionalProperties": [Function], + "setItems": [Function], + "setPatternProperties": [Function], + "setProperties": [Function], + "title": "title", + "toJSON": [Function], + "type": "object", + "version": "2.0", + "x-decorator": "FormItem", +} +`; diff --git a/packages/json-schema/src/__tests__/compat.spec.ts b/packages/json-schema/src/__tests__/compat.spec.ts deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/packages/json-schema/src/__tests__/complier.spec.ts b/packages/json-schema/src/__tests__/complier.spec.ts index e69de29bb2d..3f8a0f1f972 100644 --- a/packages/json-schema/src/__tests__/complier.spec.ts +++ b/packages/json-schema/src/__tests__/complier.spec.ts @@ -0,0 +1,146 @@ +import { complie, registerComplier, shallowComplie } from '../complier' +import { makeAutoObservable } from 'mobx' +import { Schema } from '../schema' + +test('complie', () => { + expect(complie('{{123}}xx')).toEqual('{{123}}xx') + expect(complie('{{123}} ')).toEqual(123) + expect(complie('{{123}}')).toEqual(123) + expect( + complie({ + hello: '{{123}}', + }) + ).toEqual({ + hello: 123, + }) + expect( + complie({ + array: ['{{123}}'], + }) + ).toEqual({ + array: [123], + }) + const date = new Date() + date['expression'] = '{{123}}' + const compliedDate = complie(date) + expect(compliedDate).toEqual(date) + expect(compliedDate['expression']).toEqual('{{123}}') + const moment = { _isAMomentObject: true, expression: '{{123}}' } + const compliedMoment = complie(moment) + expect(compliedMoment).toEqual(moment) + expect(compliedMoment['expression']).toEqual('{{123}}') + const react = { _owner: true, $$typeof: true, expression: '{{123}}' } + const compliedReact = complie(react) + expect(compliedReact).toEqual(react) + expect(compliedReact['expression']).toEqual('{{123}}') + const actions = { + [Symbol.for('__REVA_ACTIONS')]: true, + expression: '{{123}}', + } + const compliedActions = complie(actions) + expect(compliedActions).toEqual(actions) + expect(compliedActions['expression']).toEqual('{{123}}') + const observable = makeAutoObservable({ + aa: 123, + bb: 321, + expression: '{{123}}', + }) + const compliedObservable = complie(observable) + expect(compliedObservable).toEqual(observable) + expect(compliedObservable['expression']).toEqual('{{123}}') + + const schema = new Schema({ + type: 'object', + properties: { + aa: { + type: 'string', + 'x-component': 'Input', + 'x-component-props': '{{123}}', + }, + }, + }) + const compliedSchema = complie(schema) + expect(compliedSchema).toEqual(schema) + expect(compliedSchema.properties['aa']['x-component-props']).toEqual( + '{{123}}' + ) + const toJSable = { + toJS() { + return { + aa: 123, + } + }, + expression: '{{123}}', + } + + const compliedToJSable = complie(toJSable) + expect(compliedToJSable).toEqual(toJSable) + expect(compliedToJSable['expression']).toEqual('{{123}}') + + const toJSONable = { + toJSON() { + return { + aa: 123, + } + }, + expression: '{{123}}', + } + + const compliedToJSONable = complie(toJSONable) + expect(compliedToJSONable).toEqual(toJSONable) + expect(compliedToJSONable['expression']).toEqual('{{123}}') + + const circularRef = { + expression: '{{123}}', + } + circularRef['self'] = circularRef + + const compliedCircularRef = complie(circularRef) + expect(compliedCircularRef['expression']).toEqual(123) +}) + +test('shallowComplie', () => { + expect(shallowComplie('{{123}}xx')).toEqual('{{123}}xx') + expect(shallowComplie('{{123}} ')).toEqual(123) + expect(shallowComplie('{{123}}')).toEqual(123) + expect( + shallowComplie({ + hello: '{{123}}', + }) + ).toEqual({ + hello: '{{123}}', + }) + expect( + shallowComplie({ + array: ['{{123}}'], + }) + ).toEqual({ + array: ['{{123}}'], + }) + expect(shallowComplie(['{{123}}'])).toEqual([123]) + expect(shallowComplie([{ kk: '{{123}}' }])).toEqual([{ kk: '{{123}}' }]) +}) + +test('registerComplier', () => { + registerComplier(() => { + return 'complied' + }) + expect(complie('{{123}}xx')).toEqual('{{123}}xx') + expect(complie('{{123}} ')).toEqual('complied') + expect(complie('{{123}}')).toEqual('complied') + expect( + complie({ + hello: '{{123}}', + }) + ).toEqual({ + hello: 'complied', + }) + expect( + complie({ + array: ['{{123}}'], + }) + ).toEqual({ + array: ['complied'], + }) + registerComplier(null) +}) diff --git a/packages/json-schema/src/__tests__/patches.spec.ts b/packages/json-schema/src/__tests__/patches.spec.ts new file mode 100644 index 00000000000..48996df8523 --- /dev/null +++ b/packages/json-schema/src/__tests__/patches.spec.ts @@ -0,0 +1,156 @@ +import { Schema } from '../schema' +import { + registerTypeDefaultComponents, + registerVoidComponents, +} from '../patches' + +registerVoidComponents(['MyCard']) +registerTypeDefaultComponents({ + string: 'Input', +}) + +test('v1 polyfill', () => { + const schema = new Schema({ + type: 'string', + editable: true, + } as any) + expect(schema['x-editable']).toEqual(true) + const schema1 = new Schema({ + type: 'string', + visible: true, + } as any) + expect(schema1['x-visible']).toEqual(true) + const schema2 = new Schema({ + type: 'string', + display: false, + } as any) + expect(schema2['x-display']).toEqual('hidden') + expect(schema2['x-display']).toEqual('hidden') + const schema3 = new Schema({ + type: 'string', + 'x-linkages': [ + { + type: 'value:visible', + condition: '{{$value == 123}}', + }, + ], + } as any) + expect(schema3['x-reactions']).toEqual([ + { + when: '{{$self.value == 123}}', + fullfill: { + state: { + visible: true, + }, + }, + otherwise: { + state: { + visible: false, + }, + }, + }, + ]) + const schema4 = new Schema({ + type: 'string', + 'x-linkages': [ + { + type: 'value:schema', + target: 'xxx', + condition: '{{$value == 123}}', + schema: { + title: 'xxx', + }, + otherwise: { + title: '123', + }, + }, + ], + } as any) + expect(schema4['x-reactions']).toEqual([ + { + when: '{{$self.value == 123}}', + target: 'xxx', + fullfill: { + schema: { + version: '1.0', + title: 'xxx', + 'x-decorator': 'FormItem', + }, + }, + otherwise: { + schema: { + version: '1.0', + title: '123', + 'x-decorator': 'FormItem', + }, + }, + }, + ]) + const schema5 = new Schema({ + type: 'string', + 'x-linkages': [ + { + type: 'value:state', + target: 'xxx', + condition: '{{$value == 123}}', + state: { + title: 'xxx', + }, + otherwise: { + title: '123', + }, + }, + ], + } as any) + expect(schema5['x-reactions']).toEqual([ + { + when: '{{$self.value == 123}}', + target: 'xxx', + fullfill: { + state: { + title: 'xxx', + }, + }, + otherwise: { + state: { + title: '123', + }, + }, + }, + ]) + const schema6 = new Schema({ + type: 'string', + 'x-props': { + labelCol: 3, + wrapperCol: 4, + }, + 'x-linkages': [ + { + type: 'value:visible', + condition: null, + }, + ], + } as any) + expect(schema6['x-component']).toEqual('Input') + expect(schema6['x-decorator']).toEqual('FormItem') + expect(schema6['x-decorator-props']).toEqual({ + labelCol: 3, + wrapperCol: 4, + }) + const schema7 = new Schema({ + type: 'object', + 'x-component': 'MyCard', + 'x-linkages': {}, + } as any) + expect(schema7.type === 'void').toBeTruthy() + new Schema({ + type: 'object', + 'x-component': 'MyCard', + 'x-linkages': [null], + } as any) + new Schema({ + type: 'object', + 'x-component': 'MyCard', + 'x-linkages': [{}], + } as any) +}) diff --git a/packages/json-schema/src/__tests__/schema.spec.ts b/packages/json-schema/src/__tests__/schema.spec.ts index e69de29bb2d..eeecf0d9d82 100644 --- a/packages/json-schema/src/__tests__/schema.spec.ts +++ b/packages/json-schema/src/__tests__/schema.spec.ts @@ -0,0 +1,251 @@ +import { Schema } from '../' +import { isFn } from '@formily/shared' + +test('has methods', () => { + const schema = new Schema({ + type: 'object', + properties: { + aa: { + type: 'string', + }, + }, + }) + expect(isFn(schema.setAdditionalItems)).toBeTruthy() + expect(isFn(schema.setAdditionalProperties)).toBeTruthy() + expect(isFn(schema.setItems)).toBeTruthy() + expect(isFn(schema.setPatternProperties)).toBeTruthy() + expect(isFn(schema.setProperties)).toBeTruthy() + expect(isFn(schema.addPatternProperty)).toBeTruthy() + expect(isFn(schema.addProperty)).toBeTruthy() + expect(isFn(schema.fromJSON)).toBeTruthy() + expect(isFn(schema.toJSON)).toBeTruthy() + expect(isFn(schema.reducePatternProperties)).toBeTruthy() + expect(isFn(schema.reduceProperties)).toBeTruthy() + expect(isFn(schema.removeProperty)).toBeTruthy() + expect(isFn(schema.removePatternProperty)).toBeTruthy() + expect(isFn(schema.mapPatternProperties)).toBeTruthy() + expect(isFn(schema.mapProperties)).toBeTruthy() + + expect(isFn(Schema.isSchemaInstance)).toBeTruthy() + expect(isFn(Schema.registerComplier)).toBeTruthy() + expect(isFn(Schema.registerPatches)).toBeTruthy() + expect(isFn(Schema.shallowComplie)).toBeTruthy() + expect(isFn(Schema.complie)).toBeTruthy() + expect(isFn(Schema.getOrderProperties)).toBeTruthy() +}) + +test('all props', () => { + const schema = new Schema({ + type: 'object', + title: 'title', + description: 'description', + patternProperties: { + '^[a-zA-Z0-9]*$': { + properties: { + model: { type: 'string' }, + made: { type: 'string' }, + year: { type: 'string' }, + }, + }, + }, + additionalProperties: { + type: 'string', + }, + properties: { + string: { + type: 'string', + default: 'default', + required: true, + 'x-component': 'Input', + 'x-component-props': { + placeholder: 'placeholder', + }, + 'x-decorator': 'FormItem', + 'x-decorator-props': { + labelCol: 3, + }, + 'x-disabled': true, + 'x-display': 'visible', + 'x-editable': false, + 'x-hidden': false, + 'x-pattern': 'readPretty', + 'x-read-only': true, + 'x-validator': ['phone'], + 'x-reactions': [ + { + target: 'xxx', + when: '{{aa > bb}}', + }, + ], + }, + boolean: { + type: 'boolean', + default: false, + }, + number: { + type: 'number', + default: 100, + }, + date: { + type: 'date', + default: '2020-12-23', + }, + datetime: { + type: 'datetime', + default: '2020-12-23 23:00:00', + }, + array: { + type: 'array', + items: { + type: 'string', + }, + additionalItems: { + type: 'number', + }, + }, + array2: { + type: 'array', + items: [ + { + type: 'string', + }, + { + type: 'object', + }, + ], + }, + void: { + type: 'void', + }, + }, + }) + expect(schema).toMatchSnapshot() +}) + +test('all methods', () => { + const schema = new Schema({ + type: 'object', + 'x-reactions': null, + }) + const schema2 = new Schema({ + type: 'object', + fn: () => {}, + } as any) + const schema3 = new Schema({ + type: 'object', + additionalItems: null, + additionalProperties: null, + properties: null, + }) + schema3.additionalItems = null + schema3.additionalProperties = null + schema3.properties = { + xxx: null, + } + schema3.items = [null] + const schema4 = new Schema({ + type: 'object', + additionalItems: {}, + additionalProperties: {}, + properties: {}, + }) + schema4.additionalItems = {} as any + schema4.additionalProperties = {} as any + schema4.properties = { + xxx: {} as any, + } + schema4.items = [{}] as any + const schema5 = new Schema({ + type: 'array', + }) + schema5.items = null + const schema6 = new Schema({ + type: 'array', + }) + schema6.items = {} as any + const schema7 = new Schema({ + type: 'array', + items: { + type: 'string', + }, + }) + const string = schema.addProperty('string', { + type: 'string', + title: 'string', + description: null, + 'x-reactions': [ + { + target: 'xxx', + when: true, + fullfill: { + schema: {}, + }, + }, + ], + }) + + const array = schema.addProperty('array', { + type: 'string', + title: 'string', + items: [{ type: 'integer' }, { type: 'integer' }], + }) + const pattern = schema.addPatternProperty('^[a-zA-Z0-9]*$', { + properties: { + model: { type: 'string', 'x-index': 2 }, + made: { type: 'string', 'x-index': 1 }, + year: { type: 'string', 'x-index': 0 }, + }, + }) + schema.addPatternProperty('xxx', null) + schema.setAdditionalProperties({ + type: 'string', + }) + schema.setAdditionalProperties(null) + array.setItems(null) + array.setAdditionalItems({ + type: 'string', + }) + array.setAdditionalItems(null) + schema.setPatternProperties(null) + schema.fromJSON(null) + expect(schema2['fn']).toBeUndefined() + expect(schema.properties.string).not.toBeUndefined() + expect(schema.patternProperties['^[a-zA-Z0-9]*$']).not.toBeUndefined() + expect(schema).toMatchSnapshot() + expect(schema.toJSON()).toMatchSnapshot() + expect(pattern.mapProperties((schema, key) => key)).toEqual([ + 'year', + 'made', + 'model', + ]) + expect( + pattern.reduceProperties((buf, schema, key) => buf.concat('_' + key), []) + ).toEqual(['_year', '_made', '_model']) + expect(schema.mapPatternProperties((schema, key) => key)).toEqual([ + '^[a-zA-Z0-9]*$', + ]) + expect( + schema.reducePatternProperties( + (buf, schema, key) => buf.concat('_' + key), + [] + ) + ).toEqual(['_^[a-zA-Z0-9]*$']) + schema5.toJSON() + schema6.toJSON() + schema7.toJSON() + schema.removeProperty('string') + expect(schema.properties.string).toBeUndefined() + schema.removePatternProperty('^[a-zA-Z0-9]*$') + expect(schema.patternProperties['^[a-zA-Z0-9]*$']).toBeUndefined() + expect(schema.complie()).toMatchSnapshot() + expect(string.complie()).toMatchSnapshot() + expect(schema3.toJSON()).toMatchSnapshot() + expect(schema4.toJSON()).toMatchSnapshot() +}) + +describe('all static methods', () => { + expect(Schema.complie({ aa: '{{123}}' })).toEqual({ aa: 123 }) + expect(Schema.shallowComplie('{{123}}')).toEqual(123) + expect(Schema.getOrderProperties()).toEqual([]) + Schema.registerPatches(null) +}) \ No newline at end of file diff --git a/packages/json-schema/src/complier.ts b/packages/json-schema/src/complier.ts index 08cffeeea54..044137b955a 100644 --- a/packages/json-schema/src/complier.ts +++ b/packages/json-schema/src/complier.ts @@ -1,4 +1,4 @@ -import { isArr, isFn, isStr, reduce } from '@formily/shared' +import { isArr, isFn, isPlainObj, isStr, reduce } from '@formily/shared' import { isObservable } from 'mobx' import { Schema } from './schema' @@ -36,8 +36,7 @@ export const shallowComplie = ( export const complie = ( source: Source, - scope?: Scope, - exclude?: (key: string, value: any) => boolean + scope?: Scope ): any => { const seenObjects = new WeakMap() const complie = (source: any) => { @@ -45,7 +44,7 @@ export const complie = ( return shallowComplie(source, scope) } else if (isArr(source)) { return source.map((value: any) => complie(value)) - } else if (typeof source === 'object') { + } else if (isPlainObj(source)) { if ('$$typeof' in source && '_owner' in source) { return source } @@ -74,20 +73,6 @@ export const complie = ( return reduce( source, (buf, value, key) => { - if (isFn(exclude)) { - if (exclude(key, value)) { - buf[key] = value - return buf - } - } - if (key === 'x-linkages' || key === 'x-reactions') { - buf[key] = value - return buf - } - if (value && value['_owner'] && value['$$typeof']) { - buf[key] = value - return buf - } buf[key] = complie(value) return buf }, diff --git a/packages/json-schema/src/index.ts b/packages/json-schema/src/index.ts index d27bacc77e3..72e16ac0a53 100644 --- a/packages/json-schema/src/index.ts +++ b/packages/json-schema/src/index.ts @@ -1,3 +1,3 @@ export * from './schema' -export * from './polyfills' +export * from './patches' export * from './types' diff --git a/packages/json-schema/src/polyfills/SPECIFICATION_1_0.ts b/packages/json-schema/src/patches/SPECIFICATION_1_0.ts similarity index 93% rename from packages/json-schema/src/polyfills/SPECIFICATION_1_0.ts rename to packages/json-schema/src/patches/SPECIFICATION_1_0.ts index 7a10b6e2c12..b88d6a49496 100644 --- a/packages/json-schema/src/polyfills/SPECIFICATION_1_0.ts +++ b/packages/json-schema/src/patches/SPECIFICATION_1_0.ts @@ -66,10 +66,10 @@ const transformXLinkage = (linkages: any[]) => { } }, []) } + return [] } const transformSchema = (schema: ISchema) => { - if (!schema) return schema if (isValid(schema['editable'])) { schema['x-editable'] = schema['x-editable'] || schema['editable'] delete schema['editable'] @@ -79,11 +79,13 @@ const transformSchema = (schema: ISchema) => { delete schema['visible'] } if (isValid(schema['display'])) { - schema['x-display'] = schema['x-display'] || schema['display'] ? 'visible' : 'hidden' + schema['x-display'] = + schema['x-display'] || schema['display'] ? 'visible' : 'hidden' delete schema['display'] } if (isValid(schema['x-props'])) { - schema['x-decorator-props'] = schema['x-decorator-props'] || schema['x-props'] + schema['x-decorator-props'] = + schema['x-decorator-props'] || schema['x-props'] delete schema['display'] } if (schema['x-linkages']) { diff --git a/packages/json-schema/src/polyfills/index.ts b/packages/json-schema/src/patches/index.ts similarity index 100% rename from packages/json-schema/src/polyfills/index.ts rename to packages/json-schema/src/patches/index.ts diff --git a/packages/json-schema/src/schema.ts b/packages/json-schema/src/schema.ts index a833328008f..78907f710f7 100644 --- a/packages/json-schema/src/schema.ts +++ b/packages/json-schema/src/schema.ts @@ -417,7 +417,7 @@ export class Schema< return results } - complie = (scope: any) => { + complie = (scope?: any) => { const shallows = [ 'properties', 'patternProperties', @@ -492,14 +492,14 @@ export class Schema< const results = {} each(this, (value: any, key) => { if (key === 'properties' || key === 'patternProperties') { - results[key] = map(value, (item) => item?.toJSON()) + results[key] = map(value, (item) => item?.toJSON?.()) } else if (key === 'additionalProperties' || key === 'additionalItems') { - results[key] = value?.toJSON() + results[key] = value?.toJSON?.() } else if (key === 'items') { if (Array.isArray(value)) { - results[key] = value.map((item) => item?.toJSON()) + results[key] = value.map((item) => item?.toJSON?.()) } else { - results[key] = value?.toJSON() + results[key] = value?.toJSON?.() } } else { results[key] = value @@ -526,12 +526,12 @@ export class Schema< return orderProperties.concat(unorderProperties).filter((item) => !!item) } - static complie = (data: any, scope: any) => { - return complie(data, scope) + static complie = (expression: any, scope?: any) => { + return complie(expression, scope) } - static shallowComplie = (data: any, scope: any) => { - return shallowComplie(data, scope) + static shallowComplie = (expression: any, scope?: any) => { + return shallowComplie(expression, scope) } static isSchemaInstance = (value: any): value is Schema => { diff --git a/packages/json-schema/src/types.ts b/packages/json-schema/src/types.ts index 8203ff34db8..6cb2f3208e3 100644 --- a/packages/json-schema/src/types.ts +++ b/packages/json-schema/src/types.ts @@ -46,16 +46,16 @@ export type SchemaKey = string | number export type SchemaExtendReaction = | { dependencies?: string[] - when?: string + when?: string | boolean target?: string fullfill?: { state?: any - schema?: any + schema?: ISchema run?: string } otherwise?: { state?: any - schema?: any + schema?: ISchema run?: string } }