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 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
- - [Table of Contents](#table-of-contents)
12
- - [Features](#features)
13
- - [Usage](#usage)
14
- - [No Code Generation](#no-code-generation)
15
- - [Error Handling](#error-handling)
16
- - [ValidationError Properties](#validationerror-properties)
17
- - [Get the cause of the error](#get-the-cause-of-the-error)
18
- - [Adding Custom Types](#adding-custom-types)
19
- - [Method Signature](#method-signature)
20
- - [Example: Adding a Custom Type](#example-adding-a-custom-type)
21
- - [Adding Custom Formats](#adding-custom-formats)
22
- - [Method Signature](#method-signature-1)
23
- - [Example: Adding a Custom Format](#example-adding-a-custom-format)
24
- - [Adding Custom Keywords](#adding-custom-keywords)
25
- - [Method Signature](#method-signature-2)
26
- - [Example: Adding a Custom Keyword](#example-adding-a-custom-keyword)
27
- - [Complex example: Adding a Custom Keyword that uses the instance](#complex-example-adding-a-custom-keyword-that-uses-the-instance)
28
- - [No Code Generation Opened Possibilities](#no-code-generation-opened-possibilities)
29
- - [Immutable Mode](#immutable-mode)
30
- - [TypeScript Support](#typescript-support)
31
- - [Known Limitations](#known-limitations)
32
- - [Schema References and Schema Definitions](#schema-references-and-schema-definitions)
33
- - [Unsupported Formats](#unsupported-formats)
34
- - [Internationalized Formats](#internationalized-formats)
35
- - [Testing](#testing)
36
- - [Contribute](#contribute)
37
- - [Legal](#legal)
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
- export interface DefineErrorOptions {
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
- addKeyword(name: string, validator: KeywordFunction): void;
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.types.get(validType);
469
- const prefixValidator = instance.keywords.get(prefixValidator);
470
- const formatValidator = instance.formats.get(validFormat);
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.types.get("string");
576
- const projectTypeValidator = instance.types.get("project");
577
- const employeeTypeValidator = instance.types.get("employee");
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
- SchemaShield currently does not support the following formats, but they are planned to be addressed in future updates of SchemaShield. For now, consider using custom implementations using the `addFormat` method to handle these formats.
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
@@ -1,3 +1,3 @@
1
- import { FormatFunction } from './index';
1
+ import { FormatFunction } from "./index";
2
2
  export declare const Formats: Record<string, FormatFunction | false>;
3
3
  //# sourceMappingURL=formats.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../lib/formats.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAgBzC,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,GAAG,KAAK,CAkF1D,CAAC"}
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: Map<string, false | TypeFunction>;
29
- formats: Map<string, false | FormatFunction>;
30
- keywords: Map<string, false | KeywordFunction>;
31
- immutable: boolean;
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
- addFormat(name: string, validator: FormatFunction): void;
37
- addKeyword(name: string, validator: KeywordFunction): void;
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;
@@ -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,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC7B,SAAS,CAAC,EAAE,gBAAgB,CAAC;IAC7B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACxB,CAAC,IAAI,EAAE,GAAG,GAAG;QAAE,IAAI,EAAE,GAAG,CAAC;QAAC,KAAK,EAAE,eAAe,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC;IAC1E,cAAc,EAAE,cAAc,CAAC;CAChC;AAED,qBAAa,YAAY;IACvB,KAAK,oCAA2C;IAChD,OAAO,sCAA6C;IACpD,QAAQ,uCAA8C;IACtD,SAAS,UAAS;gBAEN,EACV,SAAiB,EAClB,GAAE;QACD,SAAS,CAAC,EAAE,OAAO,CAAC;KAChB;IAoBN,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY;IAI7C,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SA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