schema-shield 0.0.3 → 0.0.4
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/README.md +68 -45
- package/dist/formats.d.ts +1 -1
- package/dist/formats.d.ts.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +197 -176
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +197 -189
- package/dist/keywords/string-keywords.d.ts.map +1 -1
- package/lib/formats.ts +191 -49
- package/lib/index.ts +49 -29
- package/lib/keywords/array-keywords.ts +1 -1
- package/lib/keywords/string-keywords.ts +6 -12
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -8,33 +8,36 @@ Despite its feature-rich and easy extendable nature, SchemaShield is designed to
|
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
10
10
|
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
- [
|
|
16
|
-
- [
|
|
17
|
-
|
|
18
|
-
- [
|
|
19
|
-
- [
|
|
20
|
-
|
|
21
|
-
- [Adding Custom
|
|
22
|
-
- [
|
|
23
|
-
|
|
24
|
-
- [Adding Custom
|
|
25
|
-
- [
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
- [
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
- [
|
|
34
|
-
- [
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
11
|
+
- [SchemaShield](#schemashield)
|
|
12
|
+
- [Table of Contents](#table-of-contents)
|
|
13
|
+
- [Features](#features)
|
|
14
|
+
- [Usage](#usage)
|
|
15
|
+
- [No Code Generation](#no-code-generation)
|
|
16
|
+
- [Error Handling](#error-handling)
|
|
17
|
+
- [ValidationError Properties](#validationerror-properties)
|
|
18
|
+
- [Get the cause of the error](#get-the-cause-of-the-error)
|
|
19
|
+
- [Adding Custom Types](#adding-custom-types)
|
|
20
|
+
- [Method Signature](#method-signature)
|
|
21
|
+
- [Example: Adding a Custom Type](#example-adding-a-custom-type)
|
|
22
|
+
- [Adding Custom Formats](#adding-custom-formats)
|
|
23
|
+
- [Method Signature](#method-signature-1)
|
|
24
|
+
- [Example: Adding a Custom Format](#example-adding-a-custom-format)
|
|
25
|
+
- [Adding Custom Keywords](#adding-custom-keywords)
|
|
26
|
+
- [Method Signature](#method-signature-2)
|
|
27
|
+
- [About the `defineError` Function](#about-the-defineerror-function)
|
|
28
|
+
- [About the `instance` Argument](#about-the-instance-argument)
|
|
29
|
+
- [Example: Adding a Custom Keyword](#example-adding-a-custom-keyword)
|
|
30
|
+
- [Complex example: Adding a Custom Keyword that uses the instance](#complex-example-adding-a-custom-keyword-that-uses-the-instance)
|
|
31
|
+
- [No Code Generation Opened Possibilities](#no-code-generation-opened-possibilities)
|
|
32
|
+
- [Immutable Mode](#immutable-mode)
|
|
33
|
+
- [TypeScript Support](#typescript-support)
|
|
34
|
+
- [Known Limitations](#known-limitations)
|
|
35
|
+
- [Schema References and Schema Definitions](#schema-references-and-schema-definitions)
|
|
36
|
+
- [Unsupported Formats](#unsupported-formats)
|
|
37
|
+
- [Internationalized Formats](#internationalized-formats)
|
|
38
|
+
- [Testing](#testing)
|
|
39
|
+
- [Contribute](#contribute)
|
|
40
|
+
- [Legal](#legal)
|
|
38
41
|
|
|
39
42
|
## Features
|
|
40
43
|
|
|
@@ -218,11 +221,12 @@ interface TypeFunction {
|
|
|
218
221
|
(data: any): boolean;
|
|
219
222
|
}
|
|
220
223
|
|
|
221
|
-
addType(name: string, validator: TypeFunction): void;
|
|
224
|
+
addType(name: string, validator: TypeFunction, overwrite?: boolean): void;
|
|
222
225
|
```
|
|
223
226
|
|
|
224
227
|
- `name`: The name of the custom type. This should be a unique string that does not conflict with existing types.
|
|
225
228
|
- `validator`: A `TypeFunction` that takes a single argument `data` and returns a boolean value. The function should return `true` if the provided data is valid for the custom type, and `false` otherwise.
|
|
229
|
+
- `overwrite` (optional): Set to `true` to overwrite an existing type with the same name. Default is `false`. If set to `false` and a type with the same name already exists, an error will be thrown.
|
|
226
230
|
|
|
227
231
|
### Example: Adding a Custom Type
|
|
228
232
|
|
|
@@ -276,11 +280,12 @@ interface FormatFunction {
|
|
|
276
280
|
(data: any): boolean;
|
|
277
281
|
}
|
|
278
282
|
|
|
279
|
-
addFormat(name: string, validator: FormatFunction): void;
|
|
283
|
+
addFormat(name: string, validator: FormatFunction, overwrite?: boolean): void;
|
|
280
284
|
```
|
|
281
285
|
|
|
282
286
|
- `name`: The name of the custom format. This should be a unique string that does not conflict with existing formats.
|
|
283
287
|
- `validator`: A FormatFunction that takes a single argument data and returns a boolean value. The function should return true if the provided data is valid for the custom format, and false otherwise.
|
|
288
|
+
- `overwrite` (optional): Set to true to overwrite an existing format with the same name. Default is false. If set to false and a format with the same name already exists, an error will be thrown.
|
|
284
289
|
|
|
285
290
|
### Example: Adding a Custom Format
|
|
286
291
|
|
|
@@ -333,7 +338,7 @@ SchemaShield allows you to add custom keywords for validation using the addKeywo
|
|
|
333
338
|
```javascript
|
|
334
339
|
type Result = void | ValidationError;
|
|
335
340
|
|
|
336
|
-
|
|
341
|
+
interface DefineErrorOptions {
|
|
337
342
|
item?: any; // Final item in the path
|
|
338
343
|
cause?: ValidationError; // Cause of the error
|
|
339
344
|
data?: any; // Data that caused the error
|
|
@@ -352,6 +357,28 @@ interface CompiledSchema {
|
|
|
352
357
|
[key: string]: any;
|
|
353
358
|
}
|
|
354
359
|
|
|
360
|
+
interface FormatFunction {
|
|
361
|
+
(data: any): boolean;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
interface TypeFunction {
|
|
365
|
+
(data: any): boolean;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
declare class SchemaShield {
|
|
369
|
+
constructor({ immutable }?: {
|
|
370
|
+
immutable?: boolean;
|
|
371
|
+
});
|
|
372
|
+
compile(schema: any): Validator;
|
|
373
|
+
addType(name: string, validator: TypeFunction, overwrite?: boolean): void;
|
|
374
|
+
addFormat(name: string, validator: FormatFunction, overwrite?: boolean): void;
|
|
375
|
+
addKeyword(name: string, validator: KeywordFunction, overwrite?: boolean): void;
|
|
376
|
+
getType(type: string): TypeFunction | false;
|
|
377
|
+
getFormat(format: string): FormatFunction | false;
|
|
378
|
+
getKeyword(keyword: string): KeywordFunction | false;
|
|
379
|
+
isSchemaLike(subSchema: any): boolean;
|
|
380
|
+
}
|
|
381
|
+
|
|
355
382
|
interface KeywordFunction {
|
|
356
383
|
(
|
|
357
384
|
schema: CompiledSchema,
|
|
@@ -361,11 +388,14 @@ interface KeywordFunction {
|
|
|
361
388
|
): Result;
|
|
362
389
|
}
|
|
363
390
|
|
|
364
|
-
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
addKeyword(name: string, validator: KeywordFunction, overwrite?: boolean): void;
|
|
365
394
|
```
|
|
366
395
|
|
|
367
396
|
- `name`: The name of the custom keyword. This should be a unique string that does not conflict with existing keywords.
|
|
368
397
|
- `validator`: A `KeywordFunction` that takes four arguments: `schema`, `data`, `defineError`, and `instance` (The SchemaShield instance that is currently running the validation). The function should not return anything if the data is valid for the custom keyword, and should return a `ValidationError` instance if the data is invalid.
|
|
398
|
+
- `overwrite` (optional): Set to true to overwrite an existing keyword with the same name. Default is false. If set to false and a keyword with the same name already exists, an error will be thrown.
|
|
369
399
|
|
|
370
400
|
#### About the `defineError` Function
|
|
371
401
|
|
|
@@ -465,9 +495,9 @@ const prefixedUsername = (schema, data, defineError, instance) => {
|
|
|
465
495
|
|
|
466
496
|
// Get the validators for the specified types and formats from the instance
|
|
467
497
|
// (if they exist)
|
|
468
|
-
const typeValidator = instance.
|
|
469
|
-
const prefixValidator = instance.
|
|
470
|
-
const formatValidator = instance.
|
|
498
|
+
const typeValidator = instance.getType(validType);
|
|
499
|
+
const prefixValidator = instance.getKeyword(prefixValidator);
|
|
500
|
+
const formatValidator = instance.getFormat(validFormat);
|
|
471
501
|
|
|
472
502
|
for (let i = 0; i < data.length; i++) {
|
|
473
503
|
const item = data[i];
|
|
@@ -572,9 +602,9 @@ schemaShield.addKeyword(
|
|
|
572
602
|
(schema, data, defineError, instance) => {
|
|
573
603
|
const { assignment, project, employee } = data;
|
|
574
604
|
|
|
575
|
-
const stringTypeValidator = instance.
|
|
576
|
-
const projectTypeValidator = instance.
|
|
577
|
-
const employeeTypeValidator = instance.
|
|
605
|
+
const stringTypeValidator = instance.getType("string");
|
|
606
|
+
const projectTypeValidator = instance.getType("project");
|
|
607
|
+
const employeeTypeValidator = instance.getType("employee");
|
|
578
608
|
|
|
579
609
|
if (!stringTypeValidator(assignment)) {
|
|
580
610
|
return defineError("Assignment must be a string", {
|
|
@@ -685,14 +715,7 @@ For now, consider using custom implementations using the `addKeyword` method or
|
|
|
685
715
|
|
|
686
716
|
### Unsupported Formats
|
|
687
717
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
- `duration`
|
|
691
|
-
- `uuid`
|
|
692
|
-
- `uri-reference`
|
|
693
|
-
- `uri-template`
|
|
694
|
-
|
|
695
|
-
### Internationalized Formats
|
|
718
|
+
#### Internationalized Formats
|
|
696
719
|
|
|
697
720
|
There is no plan to support the following formats in SchemaShield, as they are not relevant to the majority of use cases. If you need to use these formats, consider using custom implementations using the `addFormat` method to handle them.
|
|
698
721
|
|
package/dist/formats.d.ts
CHANGED
package/dist/formats.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../lib/formats.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../lib/formats.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,GAAG,KAAK,CA+O1D,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -25,16 +25,19 @@ export interface Validator {
|
|
|
25
25
|
compiledSchema: CompiledSchema;
|
|
26
26
|
}
|
|
27
27
|
export declare class SchemaShield {
|
|
28
|
-
types
|
|
29
|
-
formats
|
|
30
|
-
keywords
|
|
31
|
-
immutable
|
|
28
|
+
private types;
|
|
29
|
+
private formats;
|
|
30
|
+
private keywords;
|
|
31
|
+
private immutable;
|
|
32
32
|
constructor({ immutable }?: {
|
|
33
33
|
immutable?: boolean;
|
|
34
34
|
});
|
|
35
|
-
addType(name: string, validator: TypeFunction): void;
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
addType(name: string, validator: TypeFunction, overwrite?: boolean): void;
|
|
36
|
+
getType(type: string): TypeFunction | false;
|
|
37
|
+
addFormat(name: string, validator: FormatFunction, overwrite?: boolean): void;
|
|
38
|
+
getFormat(format: string): FormatFunction | false;
|
|
39
|
+
addKeyword(name: string, validator: KeywordFunction, overwrite?: boolean): void;
|
|
40
|
+
getKeyword(keyword: string): KeywordFunction | false;
|
|
38
41
|
compile(schema: any): Validator;
|
|
39
42
|
private compileSchema;
|
|
40
43
|
isSchemaLike(subSchema: any): boolean;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,eAAe,EAKhB,MAAM,SAAS,CAAC;AAMjB,MAAM,MAAM,MAAM,GAAG,IAAI,GAAG,eAAe,CAAC;AAE5C,MAAM,WAAW,eAAe;IAC9B,CACE,MAAM,EAAE,cAAc,EACtB,IAAI,EAAE,GAAG,EACT,WAAW,EAAE,mBAAmB,EAChC,QAAQ,EAAE,YAAY,GACrB,MAAM,CAAC;CACX;AAED,MAAM,WAAW,YAAY;IAC3B,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,CAA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