@squiz/dx-json-schema-lib 1.12.0-alpha.8 → 1.12.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.npm/_logs/2023-02-27T04_50_27_819Z-debug-0.log +39 -0
- package/jest.config.ts +0 -6
- package/lib/JsonValidationService.d.ts +35 -0
- package/lib/JsonValidationService.js +127 -1
- package/lib/JsonValidationService.js.map +1 -1
- package/lib/JsonValidationService.spec.js +288 -0
- package/lib/JsonValidationService.spec.js.map +1 -1
- package/lib/errors/JsonResolutionError.d.ts +5 -0
- package/lib/errors/JsonResolutionError.js +12 -0
- package/lib/errors/JsonResolutionError.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/jsonTypeResolution/arbitraryTypeResolution.d.ts +61 -0
- package/lib/jsonTypeResolution/arbitraryTypeResolution.js +61 -0
- package/lib/jsonTypeResolution/arbitraryTypeResolution.js.map +1 -0
- package/lib/jsonTypeResolution/arbitraryTypeResolution.spec.d.ts +1 -0
- package/lib/jsonTypeResolution/arbitraryTypeResolution.spec.js +100 -0
- package/lib/jsonTypeResolution/arbitraryTypeResolution.spec.js.map +1 -0
- package/lib/jsonTypeResolution/index.d.ts +76 -0
- package/lib/jsonTypeResolution/index.js +35 -0
- package/lib/jsonTypeResolution/index.js.map +1 -0
- package/lib/jsonTypeResolution/primitiveTypes.d.ts +10 -0
- package/lib/jsonTypeResolution/primitiveTypes.js +27 -0
- package/lib/jsonTypeResolution/primitiveTypes.js.map +1 -0
- package/lib/jsonTypeResolution/resolvableTypes.d.ts +12 -0
- package/lib/jsonTypeResolution/resolvableTypes.js +30 -0
- package/lib/jsonTypeResolution/resolvableTypes.js.map +1 -0
- package/lib/manifest/v1/v1.json +4 -3
- package/lib/manifest/v1/v1.spec.js +58 -0
- package/lib/manifest/v1/v1.spec.js.map +1 -1
- package/package.json +5 -4
- package/src/JsonValidationService.spec.ts +417 -1
- package/src/JsonValidationService.ts +154 -1
- package/src/errors/JsonResolutionError.ts +5 -0
- package/src/index.ts +3 -0
- package/src/jsonTypeResolution/arbitraryTypeResolution.spec.ts +134 -0
- package/src/jsonTypeResolution/arbitraryTypeResolution.ts +100 -0
- package/src/jsonTypeResolution/index.ts +16 -0
- package/src/jsonTypeResolution/primitiveTypes.ts +32 -0
- package/src/jsonTypeResolution/resolvableTypes.ts +37 -0
- package/src/manifest/v1/v1.json +4 -3
- package/src/manifest/v1/v1.spec.ts +96 -0
- package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,39 @@
|
|
1
|
+
0 verbose cli /usr/local/bin/node /usr/local/bin/npm
|
2
|
+
1 info using npm@8.19.3
|
3
|
+
2 info using node@v16.19.0
|
4
|
+
3 timing npm:load:whichnode Completed in 0ms
|
5
|
+
4 timing config:load:defaults Completed in 2ms
|
6
|
+
5 timing config:load:file:/usr/local/lib/node_modules/npm/npmrc Completed in 1ms
|
7
|
+
6 timing config:load:builtin Completed in 1ms
|
8
|
+
7 timing config:load:cli Completed in 2ms
|
9
|
+
8 timing config:load:env Completed in 1ms
|
10
|
+
9 info found workspace root at /builds/developer-experience/cmp
|
11
|
+
10 timing config:load:file:/builds/developer-experience/cmp/.npmrc Completed in 0ms
|
12
|
+
11 timing config:load:project Completed in 23ms
|
13
|
+
12 timing config:load:file:/root/.npmrc Completed in 1ms
|
14
|
+
13 timing config:load:user Completed in 1ms
|
15
|
+
14 timing config:load:file:/usr/local/etc/npmrc Completed in 0ms
|
16
|
+
15 timing config:load:global Completed in 0ms
|
17
|
+
16 timing config:load:validate Completed in 2ms
|
18
|
+
17 timing config:load:credentials Completed in 1ms
|
19
|
+
18 timing config:load:setEnvs Completed in 1ms
|
20
|
+
19 timing config:load Completed in 34ms
|
21
|
+
20 timing npm:load:configload Completed in 34ms
|
22
|
+
21 timing npm:load:mkdirpcache Completed in 1ms
|
23
|
+
22 timing npm:load:mkdirplogs Completed in 1ms
|
24
|
+
23 verbose title npm run compile
|
25
|
+
24 verbose argv "run" "compile" "--"
|
26
|
+
25 timing npm:load:setTitle Completed in 2ms
|
27
|
+
26 timing config:load:flatten Completed in 4ms
|
28
|
+
27 timing npm:load:display Completed in 5ms
|
29
|
+
28 verbose logfile logs-max:10 dir:/builds/developer-experience/cmp/packages/dx-json-schema-lib/.npm/_logs
|
30
|
+
29 verbose logfile /builds/developer-experience/cmp/packages/dx-json-schema-lib/.npm/_logs/2023-02-27T04_50_27_819Z-debug-0.log
|
31
|
+
30 timing npm:load:logFile Completed in 4ms
|
32
|
+
31 timing npm:load:timers Completed in 0ms
|
33
|
+
32 timing npm:load:configScope Completed in 0ms
|
34
|
+
33 timing npm:load Completed in 48ms
|
35
|
+
34 silly logfile done cleaning log files
|
36
|
+
35 timing command:run Completed in 8196ms
|
37
|
+
36 verbose exit 0
|
38
|
+
37 timing npm Completed in 8253ms
|
39
|
+
38 info ok
|
package/jest.config.ts
CHANGED
@@ -1,8 +1,43 @@
|
|
1
|
+
import { Input } from '@sagold/json-query';
|
1
2
|
import { JSONSchema, Draft } from 'json-schema-library';
|
3
|
+
import { AnyPrimitiveType, AnyResolvableType, TypeResolver } from './jsonTypeResolution/arbitraryTypeResolution';
|
2
4
|
export declare const ComponentInputSchema: Draft;
|
5
|
+
export declare const RenderInputSchema: Draft;
|
6
|
+
export declare const ComponentInputMetaSchema: MetaSchemaInput;
|
7
|
+
export declare const RenderInputMetaSchema: MetaSchemaInput;
|
8
|
+
export declare const ManifestV1MetaSchema: MetaSchemaInput;
|
9
|
+
interface MetaSchemaInput {
|
10
|
+
root: JSONSchema;
|
11
|
+
remotes?: Record<string, JSONSchema>;
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* A service that can be used to validate and resolve JSON against a schema.
|
15
|
+
*/
|
16
|
+
export declare class JSONSchemaService<P extends AnyPrimitiveType, R extends AnyResolvableType> {
|
17
|
+
private typeResolver;
|
18
|
+
schema: Draft;
|
19
|
+
constructor(typeResolver: TypeResolver<P, R>, metaSchema: MetaSchemaInput);
|
20
|
+
private doResolveRef;
|
21
|
+
/**
|
22
|
+
* Validate an input value against a specified schema
|
23
|
+
* @throws {SchemaValidationError} if the input is invalid
|
24
|
+
* @returns true if the input is valid
|
25
|
+
*/
|
26
|
+
validateInput(input: unknown, inputSchema?: JSONSchema): true | never;
|
27
|
+
private processValidationResult;
|
28
|
+
/**
|
29
|
+
* Resolve an input object by replacing all resolvable shapes with their resolved values
|
30
|
+
* @param input any input object which matches the input schema
|
31
|
+
* @param inputSchema a JSONSchema which provides type information about the input object
|
32
|
+
* @returns the input object with all resolvable shapes resolved
|
33
|
+
*/
|
34
|
+
resolveInput(input: Input, inputSchema: JSONSchema): Promise<Input>;
|
35
|
+
}
|
3
36
|
export declare class JsonValidationService {
|
4
37
|
validateManifest(manifest: unknown, version: 'v1'): true;
|
5
38
|
validateContentSchema(contentSchema: JSONSchema): true;
|
6
39
|
validateComponentInput(functionInputSchema: JSONSchema, inputValue: unknown): true;
|
40
|
+
validateRenderInput(functionInputSchema: JSONSchema, inputValue: unknown): true;
|
7
41
|
private processValidationResult;
|
8
42
|
}
|
43
|
+
export {};
|
@@ -3,7 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.JsonValidationService = exports.ComponentInputSchema = void 0;
|
6
|
+
exports.JsonValidationService = exports.JSONSchemaService = exports.ManifestV1MetaSchema = exports.RenderInputMetaSchema = exports.ComponentInputMetaSchema = exports.RenderInputSchema = exports.ComponentInputSchema = void 0;
|
7
|
+
const json_query_1 = __importDefault(require("@sagold/json-query"));
|
7
8
|
const DxComponentInputSchema_json_1 = __importDefault(require("./manifest/v1/DxComponentInputSchema.json"));
|
8
9
|
const DxComponentIcons_json_1 = __importDefault(require("./manifest/v1/DxComponentIcons.json"));
|
9
10
|
const DxContentMetaSchema_json_1 = __importDefault(require("./manifest/v1/DxContentMetaSchema.json"));
|
@@ -13,6 +14,7 @@ const v1_json_1 = __importDefault(require("./manifest/v1/v1.json"));
|
|
13
14
|
const SchemaValidationError_1 = require("./errors/SchemaValidationError");
|
14
15
|
const json_schema_library_1 = require("json-schema-library");
|
15
16
|
const json_schema_library_2 = require("json-schema-library");
|
17
|
+
const JsonResolutionError_1 = require("./errors/JsonResolutionError");
|
16
18
|
const defaultConfig = {
|
17
19
|
...json_schema_library_2.draft07Config,
|
18
20
|
errors: {
|
@@ -78,12 +80,131 @@ exports.ComponentInputSchema = new json_schema_library_1.Draft({
|
|
78
80
|
},
|
79
81
|
}, DxComponentInputSchema_json_1.default);
|
80
82
|
exports.ComponentInputSchema.addRemoteSchema('DxComponentInputSchema.json/DxContentMetaSchema.json', DxContentMetaSchema_json_1.default);
|
83
|
+
exports.RenderInputSchema = new json_schema_library_1.Draft({
|
84
|
+
...defaultConfig,
|
85
|
+
resolveRef(schema, rootSchema) {
|
86
|
+
const resolvedSchema = json_schema_library_2.draft07Config.resolveRef(schema, rootSchema);
|
87
|
+
if (!resolvedSchema) {
|
88
|
+
return resolvedSchema;
|
89
|
+
}
|
90
|
+
if (resolvedSchema.type === 'FormattedText') {
|
91
|
+
return { type: 'string' };
|
92
|
+
}
|
93
|
+
else if (Array.isArray(resolvedSchema.type) && resolvedSchema.type.includes('FormattedText')) {
|
94
|
+
return {
|
95
|
+
...schema,
|
96
|
+
type: resolvedSchema.type.filter((t) => t !== 'FormattedText').concat('string'),
|
97
|
+
};
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
return resolvedSchema;
|
101
|
+
}
|
102
|
+
},
|
103
|
+
});
|
81
104
|
const v1Schema = new json_schema_library_1.Draft07(v1_json_1.default, defaultConfig);
|
82
105
|
v1Schema.addRemoteSchema('DxComponentInputSchema.json/DxContentMetaSchema.json', DxContentMetaSchema_json_1.default);
|
83
106
|
v1Schema.addRemoteSchema('/DxComponentInputSchema.json', exports.ComponentInputSchema.getSchema());
|
84
107
|
v1Schema.addRemoteSchema('/DxComponentIcons.json', DxComponentIcons_json_1.default);
|
85
108
|
v1Schema.addRemoteSchema('http://json-schema.org/draft-07/schema', Draft_07_json_1.default);
|
86
109
|
v1Schema.addRemoteSchema('http://json-schema.org/draft-07/schema#', Draft_07_json_1.default);
|
110
|
+
exports.ComponentInputMetaSchema = {
|
111
|
+
root: DxComponentInputSchema_json_1.default,
|
112
|
+
remotes: {
|
113
|
+
'DxComponentInputSchema.json/DxContentMetaSchema.json': DxContentMetaSchema_json_1.default,
|
114
|
+
},
|
115
|
+
};
|
116
|
+
exports.RenderInputMetaSchema = {
|
117
|
+
root: Draft_07_json_1.default,
|
118
|
+
};
|
119
|
+
exports.ManifestV1MetaSchema = {
|
120
|
+
root: v1_json_1.default,
|
121
|
+
remotes: {
|
122
|
+
'DxComponentInputSchema.json/DxContentMetaSchema.json': DxContentMetaSchema_json_1.default,
|
123
|
+
'/DxComponentInputSchema.json': DxComponentInputSchema_json_1.default,
|
124
|
+
'/DxComponentIcons.json': DxComponentIcons_json_1.default,
|
125
|
+
'http://json-schema.org/draft-07/schema': Draft_07_json_1.default,
|
126
|
+
'http://json-schema.org/draft-07/schema#': Draft_07_json_1.default,
|
127
|
+
},
|
128
|
+
};
|
129
|
+
/**
|
130
|
+
* A service that can be used to validate and resolve JSON against a schema.
|
131
|
+
*/
|
132
|
+
class JSONSchemaService {
|
133
|
+
constructor(typeResolver, metaSchema) {
|
134
|
+
this.typeResolver = typeResolver;
|
135
|
+
this.schema = new json_schema_library_1.Draft({
|
136
|
+
...defaultConfig,
|
137
|
+
resolveRef: (schema, rootSchema) => this.doResolveRef(schema, rootSchema),
|
138
|
+
}, metaSchema.root);
|
139
|
+
for (const [key, value] of Object.entries(metaSchema.remotes || {})) {
|
140
|
+
this.schema.addRemoteSchema(key, value);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
doResolveRef(schema, rootSchema) {
|
144
|
+
const initialRef = json_schema_library_2.draft07Config.resolveRef(schema, rootSchema);
|
145
|
+
if (!initialRef)
|
146
|
+
return initialRef;
|
147
|
+
if (!this.typeResolver.isPrimitiveType(initialRef.type))
|
148
|
+
return initialRef;
|
149
|
+
return this.typeResolver.getValidationSchemaForPrimitive(initialRef.type);
|
150
|
+
}
|
151
|
+
/**
|
152
|
+
* Validate an input value against a specified schema
|
153
|
+
* @throws {SchemaValidationError} if the input is invalid
|
154
|
+
* @returns true if the input is valid
|
155
|
+
*/
|
156
|
+
validateInput(input, inputSchema = this.schema.rootSchema) {
|
157
|
+
inputSchema = this.schema.compileSchema(inputSchema);
|
158
|
+
const errors = this.schema.validate(input, inputSchema);
|
159
|
+
return this.processValidationResult(errors);
|
160
|
+
}
|
161
|
+
processValidationResult(errors) {
|
162
|
+
if (errors.length > 0) {
|
163
|
+
throw new SchemaValidationError_1.SchemaValidationError(errors.map((a) => a.message).join(',\n'));
|
164
|
+
}
|
165
|
+
return true;
|
166
|
+
}
|
167
|
+
/**
|
168
|
+
* Resolve an input object by replacing all resolvable shapes with their resolved values
|
169
|
+
* @param input any input object which matches the input schema
|
170
|
+
* @param inputSchema a JSONSchema which provides type information about the input object
|
171
|
+
* @returns the input object with all resolvable shapes resolved
|
172
|
+
*/
|
173
|
+
async resolveInput(input, inputSchema) {
|
174
|
+
const setters = [];
|
175
|
+
this.schema.each(input, (schema, value, pointer) => {
|
176
|
+
// First we check for if value is a resolvable shape
|
177
|
+
if (!this.typeResolver.isResolvableSchema(schema))
|
178
|
+
return;
|
179
|
+
// If its a resolvable schema, it should exist in a oneOf array with other schemas
|
180
|
+
// Including a primitive schema
|
181
|
+
const allPossibleSchemas = schema.oneOfSchema.oneOf;
|
182
|
+
if ((0, json_schema_library_1.isJSONError)(allPossibleSchemas))
|
183
|
+
return;
|
184
|
+
const primitiveSchema = allPossibleSchemas.find((schema) => this.typeResolver.isPrimitiveSchema(schema));
|
185
|
+
if (!primitiveSchema)
|
186
|
+
return;
|
187
|
+
const resolver = this.typeResolver.tryGetResolver(primitiveSchema, schema);
|
188
|
+
if (!resolver)
|
189
|
+
return;
|
190
|
+
const setResolvedData = Promise.resolve()
|
191
|
+
.then(() => resolver(value))
|
192
|
+
.then((resolvedData) => (item) => json_query_1.default.set(item, pointer, resolvedData))
|
193
|
+
.catch((e) => Promise.reject(new JsonResolutionError_1.JsonResolutionError(e, pointer, value)));
|
194
|
+
setters.push(setResolvedData);
|
195
|
+
}, inputSchema);
|
196
|
+
const potentialResolutionErrors = [];
|
197
|
+
for (const resolveResult of await Promise.allSettled(setters)) {
|
198
|
+
if (resolveResult.status === 'rejected') {
|
199
|
+
potentialResolutionErrors.push(resolveResult.reason);
|
200
|
+
continue;
|
201
|
+
}
|
202
|
+
input = resolveResult.value(input);
|
203
|
+
}
|
204
|
+
return input;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
exports.JSONSchemaService = JSONSchemaService;
|
87
208
|
class JsonValidationService {
|
88
209
|
validateManifest(manifest, version) {
|
89
210
|
switch (version) {
|
@@ -103,6 +224,11 @@ class JsonValidationService {
|
|
103
224
|
const errors = exports.ComponentInputSchema.validate(inputValue, inputSchema);
|
104
225
|
return this.processValidationResult(errors);
|
105
226
|
}
|
227
|
+
validateRenderInput(functionInputSchema, inputValue) {
|
228
|
+
const inputSchema = exports.RenderInputSchema.compileSchema(functionInputSchema);
|
229
|
+
const errors = exports.RenderInputSchema.validate(inputValue, inputSchema);
|
230
|
+
return this.processValidationResult(errors);
|
231
|
+
}
|
106
232
|
processValidationResult(errors) {
|
107
233
|
if (errors.length > 0) {
|
108
234
|
throw new SchemaValidationError_1.SchemaValidationError(errors.map((a) => a.message).join(',\n'));
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"JsonValidationService.js","sourceRoot":"","sources":["../src/JsonValidationService.ts"],"names":[],"mappings":";;;;;;AAAA,4GAA+E;AAC/E,gGAAmE;AACnE,sGAAyE;AACzE,gFAAwD;AAExD,gGAAmE;AAEnE,oEAAuC;AACvC,0EAAuE;A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gB,CAAC,CAAC;aACvD;YAED;gBACE,MAAM,IAAI,6CAAqB,CAAC,0BAA0B,CAAC,CAAC;SAC/D;IACH,CAAC;IACD,qBAAqB,CAAC,aAAyB;QAC7C,OAAO,IAAI,CAAC,uBAAuB,CAAC,4BAAoB,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC;IACpF,CAAC;IAED,sBAAsB,CAAC,mBAA+B,EAAE,UAAmB;QACzE,MAAM,WAAW,GAAG,4BAAoB,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;QAC5E,MAAM,MAAM,GAAG,4BAAoB,CAAC,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QAEtE,OAAO,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IAED,mBAAmB,CAAC,mBAA+B,EAAE,UAAmB;QACtE,MAAM,WAAW,GAAG,yBAAiB,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;QACzE,MAAM,MAAM,GAAG,yBAAiB,CAAC,QAAQ,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QACnE,OAAO,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;IAC9C,CAAC;IAEO,uBAAuB,CAAC,MAAmB;QACjD,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACrB,MAAM,IAAI,6CAAqB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;SAC3E;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AApCD,sDAoCC"}
|
@@ -7,6 +7,7 @@ const JsonValidationService_1 = require("./JsonValidationService");
|
|
7
7
|
const SchemaValidationError_1 = require("./errors/SchemaValidationError");
|
8
8
|
const validComponent_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/validComponent.json"));
|
9
9
|
const validComponentJson_json_1 = __importDefault(require("./manifest/v1/__test__/schemas/validComponentJson.json"));
|
10
|
+
const arbitraryTypeResolution_1 = require("./jsonTypeResolution/arbitraryTypeResolution");
|
10
11
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
11
12
|
function expectToThrowErrorMatchingTypeAndMessage(received, errorType, message) {
|
12
13
|
let error = null;
|
@@ -344,6 +345,54 @@ describe('JsonValidationService', () => {
|
|
344
345
|
jsonValidationService.validateComponentInput(schema, value);
|
345
346
|
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `[object Object]` (object) in `#/my-input` to be of type `array`');
|
346
347
|
});
|
348
|
+
it('should validate a FormattedText value when the schema is a $ref to a $ref', async () => {
|
349
|
+
const formattedText = [
|
350
|
+
{
|
351
|
+
tag: 'p',
|
352
|
+
type: 'tag',
|
353
|
+
children: [{ type: 'text', value: 'hello' }],
|
354
|
+
},
|
355
|
+
];
|
356
|
+
const schema = {
|
357
|
+
type: 'object',
|
358
|
+
properties: {
|
359
|
+
'my-input': { $ref: '#/definitions/FormattedText' },
|
360
|
+
},
|
361
|
+
definitions: {
|
362
|
+
FormattedText: { $ref: '#/definitions/FormattedText2' },
|
363
|
+
FormattedText2: { type: 'FormattedText' },
|
364
|
+
},
|
365
|
+
required: ['my-input'],
|
366
|
+
};
|
367
|
+
const value = {
|
368
|
+
'my-input': formattedText,
|
369
|
+
};
|
370
|
+
expect(jsonValidationService.validateComponentInput(schema, value)).toEqual(true);
|
371
|
+
});
|
372
|
+
it('should validate a FormattedText value when the schema is in an if/then/else', async () => {
|
373
|
+
const formattedText = [
|
374
|
+
{
|
375
|
+
tag: 'p',
|
376
|
+
type: 'tag',
|
377
|
+
children: [{ type: 'text', value: 'hello' }],
|
378
|
+
},
|
379
|
+
];
|
380
|
+
const schema = {
|
381
|
+
type: 'object',
|
382
|
+
properties: {
|
383
|
+
'my-input': {
|
384
|
+
if: { type: 'string' },
|
385
|
+
then: { type: 'string' },
|
386
|
+
else: { type: 'FormattedText' },
|
387
|
+
},
|
388
|
+
},
|
389
|
+
required: ['my-input'],
|
390
|
+
};
|
391
|
+
const value = {
|
392
|
+
'my-input': formattedText,
|
393
|
+
};
|
394
|
+
expect(jsonValidationService.validateComponentInput(schema, value)).toEqual(true);
|
395
|
+
});
|
347
396
|
});
|
348
397
|
describe('standard inputs', () => {
|
349
398
|
const functionInputSchema = {
|
@@ -391,5 +440,244 @@ describe('JsonValidationService', () => {
|
|
391
440
|
});
|
392
441
|
});
|
393
442
|
});
|
443
|
+
describe('validateRenderInput', () => {
|
444
|
+
it('should validate a property with type FormattedText as string', () => {
|
445
|
+
const schema = {
|
446
|
+
type: 'object',
|
447
|
+
properties: {
|
448
|
+
'my-input': { type: 'FormattedText' },
|
449
|
+
},
|
450
|
+
required: ['my-input'],
|
451
|
+
};
|
452
|
+
const value = {
|
453
|
+
'my-input': 'hello',
|
454
|
+
};
|
455
|
+
expect(jsonValidationService.validateRenderInput(schema, value)).toEqual(true);
|
456
|
+
});
|
457
|
+
it('should error when a property with type FormattedText is provided a valid FormattedText value', () => {
|
458
|
+
const schema = {
|
459
|
+
type: 'object',
|
460
|
+
properties: {
|
461
|
+
'my-input': { type: 'FormattedText' },
|
462
|
+
},
|
463
|
+
required: ['my-input'],
|
464
|
+
};
|
465
|
+
const value = {
|
466
|
+
'my-input': [{ type: 'text', value: 'hello' }],
|
467
|
+
};
|
468
|
+
expectToThrowErrorMatchingTypeAndMessage(() => {
|
469
|
+
jsonValidationService.validateRenderInput(schema, value);
|
470
|
+
}, SchemaValidationError_1.SchemaValidationError, 'failed validation: Expected `[object Object]` (array) in `#/my-input` to be of type `string`');
|
471
|
+
});
|
472
|
+
it('should validate a property with type FormattedText within an if/then/else as string', () => {
|
473
|
+
const schema = {
|
474
|
+
type: 'object',
|
475
|
+
properties: {
|
476
|
+
'my-input': {
|
477
|
+
type: 'object',
|
478
|
+
properties: {
|
479
|
+
prop: { type: 'string' },
|
480
|
+
},
|
481
|
+
required: ['prop'],
|
482
|
+
if: { properties: { prop: { const: 'a' } } },
|
483
|
+
then: {
|
484
|
+
properties: {
|
485
|
+
text: { type: 'FormattedText' },
|
486
|
+
},
|
487
|
+
required: ['text'],
|
488
|
+
},
|
489
|
+
else: {
|
490
|
+
properties: {
|
491
|
+
text: { type: 'array', items: { type: 'FormattedText' } },
|
492
|
+
},
|
493
|
+
required: ['text'],
|
494
|
+
},
|
495
|
+
},
|
496
|
+
},
|
497
|
+
required: ['my-input'],
|
498
|
+
};
|
499
|
+
const value = {
|
500
|
+
'my-input': {
|
501
|
+
prop: 'a',
|
502
|
+
text: 'hello',
|
503
|
+
},
|
504
|
+
};
|
505
|
+
expect(jsonValidationService.validateRenderInput(schema, value)).toEqual(true);
|
506
|
+
const otherValue = {
|
507
|
+
'my-input': {
|
508
|
+
prop: 'b',
|
509
|
+
text: ['my', 'formatted', 'text'],
|
510
|
+
},
|
511
|
+
};
|
512
|
+
expect(jsonValidationService.validateRenderInput(schema, otherValue)).toEqual(true);
|
513
|
+
});
|
514
|
+
});
|
515
|
+
});
|
516
|
+
const defaultSchema = {
|
517
|
+
type: 'object',
|
518
|
+
properties: {
|
519
|
+
myProperty: {
|
520
|
+
type: 'string',
|
521
|
+
},
|
522
|
+
},
|
523
|
+
required: ['myProperty'],
|
524
|
+
};
|
525
|
+
function primitiveTypeFixture(title, schema = defaultSchema) {
|
526
|
+
return (0, arbitraryTypeResolution_1.PrimitiveType)({
|
527
|
+
...schema,
|
528
|
+
title,
|
529
|
+
});
|
530
|
+
}
|
531
|
+
function resolvableTypeFixture(title, schema = defaultSchema) {
|
532
|
+
return (0, arbitraryTypeResolution_1.ResolvableType)({
|
533
|
+
...schema,
|
534
|
+
title,
|
535
|
+
});
|
536
|
+
}
|
537
|
+
describe('JsonSchemaService', () => {
|
538
|
+
describe('validateInput', () => {
|
539
|
+
it.each([String('123'), Number(123), [123]])('should validate any primitive type with its resolvable type %s', (propertyValue) => {
|
540
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
541
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
542
|
+
MyPrimitive: primitiveSchema,
|
543
|
+
}, {
|
544
|
+
MyResolvableNumber: resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
545
|
+
MyResolvableArray: resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
546
|
+
}, {
|
547
|
+
MyPrimitive: {
|
548
|
+
MyResolvableNumber: (value) => value.toString(),
|
549
|
+
MyResolvableArray: (value) => value.join(''),
|
550
|
+
},
|
551
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
552
|
+
expect(jsonSchemaService.validateInput({ myProperty: propertyValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
553
|
+
});
|
554
|
+
it('should error when a primitive type is provided a value that cannot be resolved by its resolvable types', () => {
|
555
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
556
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
557
|
+
MyPrimitive: primitiveSchema,
|
558
|
+
}, {
|
559
|
+
MyResolvableNumber: resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
560
|
+
MyResolvableArray: resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
561
|
+
}, {
|
562
|
+
MyPrimitive: {
|
563
|
+
MyResolvableNumber: (value) => value.toString(),
|
564
|
+
MyResolvableArray: (value) => value.join(''),
|
565
|
+
},
|
566
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
567
|
+
expect(() => {
|
568
|
+
jsonSchemaService.validateInput({ myProperty: true }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } });
|
569
|
+
}).toThrowError();
|
570
|
+
});
|
571
|
+
it.each([String('123'), Number(123), [123]])('should validate a primitive type when defined as a ref with resolvable value %s', (propertyValue) => {
|
572
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
573
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
574
|
+
MyPrimitive: primitiveSchema,
|
575
|
+
}, {
|
576
|
+
MyResolvableNumber: resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
577
|
+
MyResolvableArray: resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
578
|
+
}, {
|
579
|
+
MyPrimitive: {
|
580
|
+
MyResolvableNumber: (value) => value.toString(),
|
581
|
+
MyResolvableArray: (value) => value.join(''),
|
582
|
+
},
|
583
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
584
|
+
expect(jsonSchemaService.validateInput({ myProperty: propertyValue }, {
|
585
|
+
type: 'object',
|
586
|
+
properties: { myProperty: { $ref: '#/definitions/Ref' } },
|
587
|
+
definitions: { Ref: { type: 'MyPrimitive' } },
|
588
|
+
})).toEqual(true);
|
589
|
+
});
|
590
|
+
it('should not validate on a primitive type against a resolvable type when a resolver is not defined', () => {
|
591
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
592
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
593
|
+
MyPrimitive: primitiveSchema,
|
594
|
+
}, {
|
595
|
+
MyResolvableNumber: resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
596
|
+
MyResolvableArray: resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
597
|
+
}, {
|
598
|
+
MyPrimitive: {
|
599
|
+
MyResolvableNumber: (value) => value.toString(),
|
600
|
+
},
|
601
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
602
|
+
expect(() => {
|
603
|
+
jsonSchemaService.validateInput({ myProperty: [123] }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } });
|
604
|
+
}).toThrowError();
|
605
|
+
});
|
606
|
+
it('should validate a primitive type against similar but different resolvable types', () => {
|
607
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
608
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
609
|
+
MyPrimitive: primitiveSchema,
|
610
|
+
}, {
|
611
|
+
MyResolvableSrcNumber: resolvableTypeFixture('MyResolvableSrcNumber', {
|
612
|
+
type: 'object',
|
613
|
+
properties: {
|
614
|
+
src: { type: 'number' },
|
615
|
+
},
|
616
|
+
}),
|
617
|
+
MyResolvableSrcString: resolvableTypeFixture('MyResolvableSrcString', {
|
618
|
+
type: 'object',
|
619
|
+
properties: {
|
620
|
+
src: { type: 'string' },
|
621
|
+
},
|
622
|
+
}),
|
623
|
+
}, {
|
624
|
+
MyPrimitive: {
|
625
|
+
MyResolvableSrcNumber: (value) => value.src.toString(),
|
626
|
+
MyResolvableSrcString: (value) => value.src,
|
627
|
+
},
|
628
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
629
|
+
expect(jsonSchemaService.validateInput({
|
630
|
+
myProperty: {
|
631
|
+
src: 123,
|
632
|
+
},
|
633
|
+
}, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
634
|
+
expect(jsonSchemaService.validateInput({
|
635
|
+
myProperty: {
|
636
|
+
src: '123',
|
637
|
+
},
|
638
|
+
}, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).toEqual(true);
|
639
|
+
});
|
640
|
+
});
|
641
|
+
describe('resolveInput', () => {
|
642
|
+
it.each([String('123'), Number(123), [123]])('should resolve a primitive type from its resolvable type %s', async (resolvableValue) => {
|
643
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
644
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
645
|
+
MyPrimitive: primitiveSchema,
|
646
|
+
}, {
|
647
|
+
MyResolvableNumber: resolvableTypeFixture('MyResolvableNumber', { type: 'number' }),
|
648
|
+
MyResolvableArray: resolvableTypeFixture('MyResolvableArray', { type: 'array' }),
|
649
|
+
}, {
|
650
|
+
MyPrimitive: {
|
651
|
+
MyResolvableNumber: (value) => value.toString(),
|
652
|
+
MyResolvableArray: (value) => value.join(''),
|
653
|
+
},
|
654
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
655
|
+
await expect(jsonSchemaService.resolveInput({ myProperty: resolvableValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).resolves.toEqual({ myProperty: '123' });
|
656
|
+
});
|
657
|
+
it.each([
|
658
|
+
[{ src: 'MyString' }, 'MyString'],
|
659
|
+
[{ src: 1132 }, '1132'],
|
660
|
+
])('should resolve a resolvable type %s against the correct resolver to %s', async (resolvableValue, output) => {
|
661
|
+
const primitiveSchema = primitiveTypeFixture('MyPrimitive', { type: 'string' });
|
662
|
+
const jsonSchemaService = new JsonValidationService_1.JSONSchemaService(new arbitraryTypeResolution_1.TypeResolver({
|
663
|
+
MyPrimitive: primitiveSchema,
|
664
|
+
}, {
|
665
|
+
MyResolvableSrcString: resolvableTypeFixture('MyResolvableSrcString', {
|
666
|
+
type: 'object',
|
667
|
+
properties: { src: { type: 'string' } },
|
668
|
+
}),
|
669
|
+
MyResolvableSrcNumber: resolvableTypeFixture('MyResolvableSrcNumber', {
|
670
|
+
type: 'object',
|
671
|
+
properties: { src: { type: 'number' } },
|
672
|
+
}),
|
673
|
+
}, {
|
674
|
+
MyPrimitive: {
|
675
|
+
MyResolvableSrcNumber: (value) => value.src.toString(),
|
676
|
+
MyResolvableSrcString: (value) => value.src,
|
677
|
+
},
|
678
|
+
}), JsonValidationService_1.ComponentInputMetaSchema);
|
679
|
+
await expect(jsonSchemaService.resolveInput({ myProperty: resolvableValue }, { type: 'object', properties: { myProperty: { type: 'MyPrimitive' } } })).resolves.toEqual({ myProperty: output });
|
680
|
+
});
|
681
|
+
});
|
394
682
|
});
|
395
683
|
//# sourceMappingURL=JsonValidationService.spec.js.map
|