@takeshape/json-schema 11.70.2 → 11.71.2
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect, test } from 'vitest';
|
|
2
|
-
import { createAjv, createSchemaValidator, fixSchema, isInvalidPropertyRequired, validate } from "../schema-validator.js";
|
|
2
|
+
import { createAjv, createSchemaValidator, createTypedValidator, fixSchema, isInvalidPropertyRequired, validate } from "../schema-validator.js";
|
|
3
3
|
test('createSchemaValidator - missing $id', () => {
|
|
4
4
|
const testSchema = {
|
|
5
5
|
type: 'object',
|
|
@@ -293,3 +293,31 @@ test('validate - ignoreNulls', () => {
|
|
|
293
293
|
expect(valid).toBe(true);
|
|
294
294
|
expect(errors.length).toBe(0);
|
|
295
295
|
});
|
|
296
|
+
test('createTypedValidator - valid data', () => {
|
|
297
|
+
const schema = {
|
|
298
|
+
type: 'object',
|
|
299
|
+
properties: {
|
|
300
|
+
id: { type: 'string' },
|
|
301
|
+
name: { type: 'string' }
|
|
302
|
+
},
|
|
303
|
+
required: ['id', 'name']
|
|
304
|
+
};
|
|
305
|
+
const validate = createTypedValidator(schema);
|
|
306
|
+
const data = { id: '123', name: 'Test' };
|
|
307
|
+
expect(validate(data)).toBe(true);
|
|
308
|
+
});
|
|
309
|
+
test('createTypedValidator - invalid data', () => {
|
|
310
|
+
const schema = {
|
|
311
|
+
type: 'object',
|
|
312
|
+
properties: {
|
|
313
|
+
id: { type: 'string' },
|
|
314
|
+
name: { type: 'string' }
|
|
315
|
+
},
|
|
316
|
+
required: ['id', 'name']
|
|
317
|
+
};
|
|
318
|
+
const validate = createTypedValidator(schema);
|
|
319
|
+
const data = { id: '123' }; // Missing 'name'
|
|
320
|
+
expect(validate(data)).toBe(false);
|
|
321
|
+
expect(validate.errors).toBeDefined();
|
|
322
|
+
expect(validate.errors?.[0].message).toBe("must have required property 'name'");
|
|
323
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ErrorObject, Options, SchemaObject } from 'ajv';
|
|
2
2
|
import { Ajv } from 'ajv';
|
|
3
|
+
import type { JSONSchema7 } from 'json-schema';
|
|
3
4
|
export type Data = any;
|
|
4
5
|
export type ValidateParams = {
|
|
5
6
|
ignoreMissing: boolean;
|
|
@@ -29,3 +30,7 @@ export declare function createAjv(options?: Options): Ajv;
|
|
|
29
30
|
*/
|
|
30
31
|
export declare function fixSchema(schema: SchemaObject): SchemaObject;
|
|
31
32
|
export declare function createSchemaValidator(schema: SchemaObject | SchemaObject[], metaSchemas?: SchemaObject[], options?: Options): Validator;
|
|
33
|
+
/**
|
|
34
|
+
* Returns a compiled validator from a schema which can act as a type guard.
|
|
35
|
+
*/
|
|
36
|
+
export declare function createTypedValidator<T>(schema: JSONSchema7, options?: Options): import("ajv").ValidateFunction<T>;
|
package/dist/schema-validator.js
CHANGED
|
@@ -198,3 +198,10 @@ export function createSchemaValidator(schema, metaSchemas = [], options = {}) {
|
|
|
198
198
|
return validate(ajv, defaultRef, data, options);
|
|
199
199
|
};
|
|
200
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Returns a compiled validator from a schema which can act as a type guard.
|
|
203
|
+
*/
|
|
204
|
+
export function createTypedValidator(schema, options = {}) {
|
|
205
|
+
const ajv = createAjv(options);
|
|
206
|
+
return ajv.compile(schema);
|
|
207
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@takeshape/json-schema",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.71.2",
|
|
4
4
|
"description": "JSON Schema validator",
|
|
5
5
|
"homepage": "https://www.takeshape.io",
|
|
6
6
|
"repository": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"ajv-formats": "3.0.1",
|
|
39
39
|
"lodash": "^4.17.21",
|
|
40
40
|
"minimatch": "^3.0.4",
|
|
41
|
-
"@takeshape/util": "11.
|
|
41
|
+
"@takeshape/util": "11.71.2"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/json-schema": "^7.0.7",
|