@bastani/atomic-schema 0.0.0-placeholder.0 → 0.9.88-alpha.1

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.
Files changed (178) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +22 -0
  3. package/NOTICE.md +13 -0
  4. package/README.md +64 -0
  5. package/THIRD_PARTY_NOTICES/typebox-LICENSE +23 -0
  6. package/dist/annotations.d.ts +44 -0
  7. package/dist/annotations.d.ts.map +1 -0
  8. package/dist/annotations.js +193 -0
  9. package/dist/annotations.js.map +1 -0
  10. package/dist/builders.d.ts +143 -0
  11. package/dist/builders.d.ts.map +1 -0
  12. package/dist/builders.js +126 -0
  13. package/dist/builders.js.map +1 -0
  14. package/dist/checks.d.ts +114 -0
  15. package/dist/checks.d.ts.map +1 -0
  16. package/dist/checks.js +578 -0
  17. package/dist/checks.js.map +1 -0
  18. package/dist/coerce.d.ts +53 -0
  19. package/dist/coerce.d.ts.map +1 -0
  20. package/dist/coerce.js +624 -0
  21. package/dist/coerce.js.map +1 -0
  22. package/dist/compiler.d.ts +53 -0
  23. package/dist/compiler.d.ts.map +1 -0
  24. package/dist/compiler.js +551 -0
  25. package/dist/compiler.js.map +1 -0
  26. package/dist/explain.d.ts +29 -0
  27. package/dist/explain.d.ts.map +1 -0
  28. package/dist/explain.js +392 -0
  29. package/dist/explain.js.map +1 -0
  30. package/dist/formats/index.d.ts +7 -0
  31. package/dist/formats/index.d.ts.map +1 -0
  32. package/dist/formats/index.js +51 -0
  33. package/dist/formats/index.js.map +1 -0
  34. package/dist/formats/typebox/date.d.ts +7 -0
  35. package/dist/formats/typebox/date.d.ts.map +1 -0
  36. package/dist/formats/typebox/date.js +47 -0
  37. package/dist/formats/typebox/date.js.map +1 -0
  38. package/dist/formats/typebox/date_time.d.ts +7 -0
  39. package/dist/formats/typebox/date_time.d.ts.map +1 -0
  40. package/dist/formats/typebox/date_time.js +39 -0
  41. package/dist/formats/typebox/date_time.js.map +1 -0
  42. package/dist/formats/typebox/duration.d.ts +6 -0
  43. package/dist/formats/typebox/duration.d.ts.map +1 -0
  44. package/dist/formats/typebox/duration.js +36 -0
  45. package/dist/formats/typebox/duration.js.map +1 -0
  46. package/dist/formats/typebox/email.d.ts +6 -0
  47. package/dist/formats/typebox/email.d.ts.map +1 -0
  48. package/dist/formats/typebox/email.js +36 -0
  49. package/dist/formats/typebox/email.js.map +1 -0
  50. package/dist/formats/typebox/hostname.d.ts +8 -0
  51. package/dist/formats/typebox/hostname.d.ts.map +1 -0
  52. package/dist/formats/typebox/hostname.js +38 -0
  53. package/dist/formats/typebox/hostname.js.map +1 -0
  54. package/dist/formats/typebox/idn_email.d.ts +6 -0
  55. package/dist/formats/typebox/idn_email.d.ts.map +1 -0
  56. package/dist/formats/typebox/idn_email.js +36 -0
  57. package/dist/formats/typebox/idn_email.js.map +1 -0
  58. package/dist/formats/typebox/idn_hostname.d.ts +9 -0
  59. package/dist/formats/typebox/idn_hostname.d.ts.map +1 -0
  60. package/dist/formats/typebox/idn_hostname.js +39 -0
  61. package/dist/formats/typebox/idn_hostname.js.map +1 -0
  62. package/dist/formats/typebox/idna/format/bidi.d.ts +4 -0
  63. package/dist/formats/typebox/idna/format/bidi.d.ts.map +1 -0
  64. package/dist/formats/typebox/idna/format/bidi.js +132 -0
  65. package/dist/formats/typebox/idna/format/bidi.js.map +1 -0
  66. package/dist/formats/typebox/idna/format/puny.d.ts +4 -0
  67. package/dist/formats/typebox/idna/format/puny.d.ts.map +1 -0
  68. package/dist/formats/typebox/idna/format/puny.js +172 -0
  69. package/dist/formats/typebox/idna/format/puny.js.map +1 -0
  70. package/dist/formats/typebox/idna/hostname.d.ts +2 -0
  71. package/dist/formats/typebox/idna/hostname.d.ts.map +1 -0
  72. package/dist/formats/typebox/idna/hostname.js +43 -0
  73. package/dist/formats/typebox/idna/hostname.js.map +1 -0
  74. package/dist/formats/typebox/idna/idn-hostname.d.ts +2 -0
  75. package/dist/formats/typebox/idna/idn-hostname.d.ts.map +1 -0
  76. package/dist/formats/typebox/idna/idn-hostname.js +71 -0
  77. package/dist/formats/typebox/idna/idn-hostname.js.map +1 -0
  78. package/dist/formats/typebox/idna/index.d.ts +3 -0
  79. package/dist/formats/typebox/idna/index.d.ts.map +1 -0
  80. package/dist/formats/typebox/idna/index.js +30 -0
  81. package/dist/formats/typebox/idna/index.js.map +1 -0
  82. package/dist/formats/typebox/idna/label/ascii.d.ts +2 -0
  83. package/dist/formats/typebox/idna/label/ascii.d.ts.map +1 -0
  84. package/dist/formats/typebox/idna/label/ascii.js +37 -0
  85. package/dist/formats/typebox/idna/label/ascii.js.map +1 -0
  86. package/dist/formats/typebox/idna/label/puny.d.ts +2 -0
  87. package/dist/formats/typebox/idna/label/puny.d.ts.map +1 -0
  88. package/dist/formats/typebox/idna/label/puny.js +56 -0
  89. package/dist/formats/typebox/idna/label/puny.js.map +1 -0
  90. package/dist/formats/typebox/idna/label/unicode.d.ts +2 -0
  91. package/dist/formats/typebox/idna/label/unicode.d.ts.map +1 -0
  92. package/dist/formats/typebox/idna/label/unicode.js +134 -0
  93. package/dist/formats/typebox/idna/label/unicode.js.map +1 -0
  94. package/dist/formats/typebox/idna/pattern/pattern.d.ts +31 -0
  95. package/dist/formats/typebox/idna/pattern/pattern.d.ts.map +1 -0
  96. package/dist/formats/typebox/idna/pattern/pattern.js +98 -0
  97. package/dist/formats/typebox/idna/pattern/pattern.js.map +1 -0
  98. package/dist/formats/typebox/ipv4.d.ts +6 -0
  99. package/dist/formats/typebox/ipv4.d.ts.map +1 -0
  100. package/dist/formats/typebox/ipv4.js +36 -0
  101. package/dist/formats/typebox/ipv4.js.map +1 -0
  102. package/dist/formats/typebox/ipv6.d.ts +6 -0
  103. package/dist/formats/typebox/ipv6.d.ts.map +1 -0
  104. package/dist/formats/typebox/ipv6.js +36 -0
  105. package/dist/formats/typebox/ipv6.js.map +1 -0
  106. package/dist/formats/typebox/iri.d.ts +6 -0
  107. package/dist/formats/typebox/iri.d.ts.map +1 -0
  108. package/dist/formats/typebox/iri.js +63 -0
  109. package/dist/formats/typebox/iri.js.map +1 -0
  110. package/dist/formats/typebox/iri_reference.d.ts +6 -0
  111. package/dist/formats/typebox/iri_reference.d.ts.map +1 -0
  112. package/dist/formats/typebox/iri_reference.js +40 -0
  113. package/dist/formats/typebox/iri_reference.js.map +1 -0
  114. package/dist/formats/typebox/json_pointer.d.ts +7 -0
  115. package/dist/formats/typebox/json_pointer.d.ts.map +1 -0
  116. package/dist/formats/typebox/json_pointer.js +37 -0
  117. package/dist/formats/typebox/json_pointer.js.map +1 -0
  118. package/dist/formats/typebox/json_pointer_uri_fragment.d.ts +7 -0
  119. package/dist/formats/typebox/json_pointer_uri_fragment.d.ts.map +1 -0
  120. package/dist/formats/typebox/json_pointer_uri_fragment.js +37 -0
  121. package/dist/formats/typebox/json_pointer_uri_fragment.js.map +1 -0
  122. package/dist/formats/typebox/regex.d.ts +6 -0
  123. package/dist/formats/typebox/regex.d.ts.map +1 -0
  124. package/dist/formats/typebox/regex.js +41 -0
  125. package/dist/formats/typebox/regex.js.map +1 -0
  126. package/dist/formats/typebox/relative_json_pointer.d.ts +7 -0
  127. package/dist/formats/typebox/relative_json_pointer.d.ts.map +1 -0
  128. package/dist/formats/typebox/relative_json_pointer.js +37 -0
  129. package/dist/formats/typebox/relative_json_pointer.js.map +1 -0
  130. package/dist/formats/typebox/time.d.ts +6 -0
  131. package/dist/formats/typebox/time.d.ts.map +1 -0
  132. package/dist/formats/typebox/time.js +60 -0
  133. package/dist/formats/typebox/time.js.map +1 -0
  134. package/dist/formats/typebox/uri.d.ts +6 -0
  135. package/dist/formats/typebox/uri.d.ts.map +1 -0
  136. package/dist/formats/typebox/uri.js +36 -0
  137. package/dist/formats/typebox/uri.js.map +1 -0
  138. package/dist/formats/typebox/uri_reference.d.ts +6 -0
  139. package/dist/formats/typebox/uri_reference.d.ts.map +1 -0
  140. package/dist/formats/typebox/uri_reference.js +36 -0
  141. package/dist/formats/typebox/uri_reference.js.map +1 -0
  142. package/dist/formats/typebox/uri_template.d.ts +6 -0
  143. package/dist/formats/typebox/uri_template.d.ts.map +1 -0
  144. package/dist/formats/typebox/uri_template.js +37 -0
  145. package/dist/formats/typebox/uri_template.js.map +1 -0
  146. package/dist/formats/typebox/url.d.ts +7 -0
  147. package/dist/formats/typebox/url.d.ts.map +1 -0
  148. package/dist/formats/typebox/url.js +36 -0
  149. package/dist/formats/typebox/url.js.map +1 -0
  150. package/dist/formats/typebox/uuid.d.ts +7 -0
  151. package/dist/formats/typebox/uuid.d.ts.map +1 -0
  152. package/dist/formats/typebox/uuid.js +37 -0
  153. package/dist/formats/typebox/uuid.js.map +1 -0
  154. package/dist/index.d.ts +5 -0
  155. package/dist/index.d.ts.map +1 -0
  156. package/dist/index.js +6 -0
  157. package/dist/index.js.map +1 -0
  158. package/dist/resolver.d.ts +58 -0
  159. package/dist/resolver.d.ts.map +1 -0
  160. package/dist/resolver.js +209 -0
  161. package/dist/resolver.js.map +1 -0
  162. package/dist/shared.d.ts +9 -0
  163. package/dist/shared.d.ts.map +1 -0
  164. package/dist/shared.js +33 -0
  165. package/dist/shared.js.map +1 -0
  166. package/dist/typebox-static.d.ts +56 -0
  167. package/dist/typebox-static.d.ts.map +1 -0
  168. package/dist/typebox-static.js +7 -0
  169. package/dist/typebox-static.js.map +1 -0
  170. package/dist/types.d.ts +48 -0
  171. package/dist/types.d.ts.map +1 -0
  172. package/dist/types.js +3 -0
  173. package/dist/types.js.map +1 -0
  174. package/dist/validator.d.ts +48 -0
  175. package/dist/validator.d.ts.map +1 -0
  176. package/dist/validator.js +81 -0
  177. package/dist/validator.js.map +1 -0
  178. package/package.json +61 -8
@@ -0,0 +1,48 @@
1
+ import type { Static } from "./builders.ts";
2
+ import type { Check, CompileOptions, JsonSchema, ParseResult, ValidationError } from "./types.ts";
3
+ /**
4
+ * Compiles `schema` into a Validator. Throws if the schema is malformed or a `$ref` names nothing (register other
5
+ * documents with `options.schemas`). The schema is read, never modified. With `coerce: true` the validator also
6
+ * coerces and parses loosely typed input.
7
+ */
8
+ export declare function compile<S extends JsonSchema>(schema: S, options: CompileOptions & {
9
+ readonly coerce: true;
10
+ }): CoercingValidator<Static<S>>;
11
+ export declare function compile<S extends JsonSchema>(schema: S, options?: CompileOptions): Validator<Static<S>>;
12
+ /** A compiled schema whose valid values have type `T` (`unknown` unless the schema was built with the builders). */
13
+ export declare class Validator<T = unknown> {
14
+ /** Whether `value` is valid. A plain function: it may be passed around without its validator. */
15
+ readonly check: (value: unknown) => value is T;
16
+ protected readonly schema: JsonSchema;
17
+ protected readonly options: CompileOptions;
18
+ private explainer;
19
+ constructor(schema: JsonSchema, options: CompileOptions, check: Check);
20
+ /**
21
+ * Why `value` is invalid: one error per failing keyword, `[]` when it is valid. Built on the first failure and
22
+ * kept, so a validator whose values always pass never pays for it.
23
+ */
24
+ errors(value: unknown): ValidationError[];
25
+ /**
26
+ * `value` itself when it is valid, else its errors: check() and errors() in one result, without coercion.
27
+ *
28
+ * Not TypeBox's `Value.Parse`, which also clones, fills defaults, converts and strips undeclared properties before
29
+ * it checks. This only checks: the strict replacement where `Value.Parse` is used to validate (its other steps
30
+ * change nothing for a valid value of a schema without defaults or extra properties). Use `parse()` with
31
+ * `coerce: true` for conversion.
32
+ */
33
+ validate(value: unknown): ParseResult<T>;
34
+ }
35
+ /** A validator compiled with `coerce: true`: it also coerces loosely typed input towards the schema. */
36
+ export declare class CoercingValidator<T = unknown> extends Validator<T> {
37
+ private coercer;
38
+ /**
39
+ * `value` coerced towards the schema: numeric strings to numbers, `"true"` to `true`, a single value to a one-item
40
+ * array, an optional `null` dropped, the first union branch the value converts to (the README lists every rule).
41
+ * Only the changed paths are copied; `value` comes back itself when nothing changes, and is never modified. Values
42
+ * that cannot be coerced stay as they are. The rules are built on the first call.
43
+ */
44
+ coerce(value: unknown): unknown;
45
+ /** Coerces `value`, then checks it: the coerced value when it is valid, else its errors. */
46
+ parse(value: unknown): ParseResult<T>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AAa,iBAAiB,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,SAAS,CAAC,CAAC,CAAC;IAC/D,OAAO,CAAC,OAAO,CAAkC;IAEjD;;;;;OAKG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAG9B;IAED,4FAA4F;IAC5F,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,CAEpC;CACD"}
@@ -0,0 +1,81 @@
1
+ // compile(): a schema to a Validator, whose `check` is the fast boolean path and whose `errors` explains failures.
2
+ import { Coercer } from "./coerce.js";
3
+ import { Compiler, RESTART } from "./compiler.js";
4
+ import { Explainer } from "./explain.js";
5
+ export function compile(schema, options = NO_OPTIONS) {
6
+ const check = compileCheck(schema, options);
7
+ return options.coerce === true
8
+ ? new CoercingValidator(schema, options, check)
9
+ : new Validator(schema, options, check);
10
+ }
11
+ const NO_OPTIONS = Object.freeze({});
12
+ /** A compiled schema whose valid values have type `T` (`unknown` unless the schema was built with the builders). */
13
+ export class Validator {
14
+ /** Whether `value` is valid. A plain function: it may be passed around without its validator. */
15
+ check;
16
+ schema;
17
+ options;
18
+ explainer = undefined;
19
+ constructor(schema, options, check) {
20
+ // A compiled check is exactly the test that a value has the schema's static type.
21
+ this.check = check;
22
+ this.schema = schema;
23
+ this.options = options;
24
+ }
25
+ /**
26
+ * Why `value` is invalid: one error per failing keyword, `[]` when it is valid. Built on the first failure and
27
+ * kept, so a validator whose values always pass never pays for it.
28
+ */
29
+ errors(value) {
30
+ if (this.check(value))
31
+ return [];
32
+ this.explainer ??= new Explainer(compiler(this.schema, this.options), this.schema);
33
+ return this.explainer.errors(value);
34
+ }
35
+ /**
36
+ * `value` itself when it is valid, else its errors: check() and errors() in one result, without coercion.
37
+ *
38
+ * Not TypeBox's `Value.Parse`, which also clones, fills defaults, converts and strips undeclared properties before
39
+ * it checks. This only checks: the strict replacement where `Value.Parse` is used to validate (its other steps
40
+ * change nothing for a valid value of a schema without defaults or extra properties). Use `parse()` with
41
+ * `coerce: true` for conversion.
42
+ */
43
+ validate(value) {
44
+ return this.check(value) ? { ok: true, value } : { ok: false, errors: this.errors(value) };
45
+ }
46
+ }
47
+ /** A validator compiled with `coerce: true`: it also coerces loosely typed input towards the schema. */
48
+ export class CoercingValidator extends Validator {
49
+ coercer = undefined;
50
+ /**
51
+ * `value` coerced towards the schema: numeric strings to numbers, `"true"` to `true`, a single value to a one-item
52
+ * array, an optional `null` dropped, the first union branch the value converts to (the README lists every rule).
53
+ * Only the changed paths are copied; `value` comes back itself when nothing changes, and is never modified. Values
54
+ * that cannot be coerced stay as they are. The rules are built on the first call.
55
+ */
56
+ coerce(value) {
57
+ this.coercer ??= new Coercer(compiler(this.schema, this.options), this.schema);
58
+ return this.coercer.coerce(value);
59
+ }
60
+ /** Coerces `value`, then checks it: the coerced value when it is valid, else its errors. */
61
+ parse(value) {
62
+ return this.validate(this.coerce(value));
63
+ }
64
+ }
65
+ function compileCheck(schema, options) {
66
+ return compiler(schema, options).root();
67
+ }
68
+ // Most schemas compile without tracking the dynamic scope; one with a bookended $dynamicRef compiles again with it.
69
+ function compiler(schema, options) {
70
+ const plain = new Compiler(schema, options, false);
71
+ try {
72
+ plain.root();
73
+ return plain;
74
+ }
75
+ catch (error) {
76
+ if (error !== RESTART)
77
+ throw error;
78
+ return new Compiler(schema, options, true);
79
+ }
80
+ }
81
+ //# sourceMappingURL=validator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validator.js","sourceRoot":"","sources":["../src/validator.ts"],"names":[],"mappings":"AAAA,mHAAmH;AAGnH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAazC,MAAM,UAAU,OAAO,CACtB,MAAS,EACT,OAAO,GAAmB,UAAU;IAEpC,MAAM,KAAK,GAAG,YAAY,C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EAAE,CAAC;IAC5F,CAAC;CACD;AAED,wGAAwG;AACxG,MAAM,OAAO,iBAA+B,SAAQ,SAAY;IACvD,OAAO,GAAwB,SAAS,CAAC;IAEjD;;;;;OAKG;IACH,MAAM,CAAC,KAAc;QACpB,IAAI,CAAC,OAAO,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAC/E,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAED,4FAA4F;IAC5F,KAAK,CAAC,KAAc;QACnB,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1C,CAAC;CACD;AAED,SAAS,YAAY,CAAC,MAAkB,EAAE,OAAuB;IAChE,OAAO,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAC;AACzC,CAAC;AAED,oHAAoH;AACpH,SAAS,QAAQ,CAAC,MAAkB,EAAE,OAAuB;IAC5D,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;IACnD,IAAI,CAAC;QACJ,KAAK,CAAC,IAAI,EAAE,CAAC;QACb,OAAO,KAAK,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,KAAK,OAAO;YAAE,MAAM,KAAK,CAAC;QACnC,OAAO,IAAI,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5C,CAAC;AACF,CAAC","sourcesContent":["// compile(): a schema to a Validator, whose `check` is the fast boolean path and whose `errors` explains failures.\n\nimport type { Static } from \"./builders.ts\";\nimport { Coercer } from \"./coerce.ts\";\nimport { Compiler, RESTART } from \"./compiler.ts\";\nimport { Explainer } from \"./explain.ts\";\nimport type { Check, CompileOptions, JsonSchema, ParseResult, ValidationError } from \"./types.ts\";\n\n/**\n * Compiles `schema` into a Validator. Throws if the schema is malformed or a `$ref` names nothing (register other\n * documents with `options.schemas`). The schema is read, never modified. With `coerce: true` the validator also\n * coerces and parses loosely typed input.\n */\nexport function compile<S extends JsonSchema>(\n\tschema: S,\n\toptions: CompileOptions & { readonly coerce: true },\n): CoercingValidator<Static<S>>;\nexport function compile<S extends JsonSchema>(schema: S, options?: CompileOptions): Validator<Static<S>>;\nexport function compile<S extends JsonSchema>(\n\tschema: S,\n\toptions: CompileOptions = NO_OPTIONS,\n): Validator<Static<S>> | CoercingValidator<Static<S>> {\n\tconst check = compileCheck(schema, options);\n\treturn options.coerce === true\n\t\t? new CoercingValidator(schema, options, check)\n\t\t: new Validator(schema, options, check);\n}\n\nconst NO_OPTIONS: CompileOptions = Object.freeze({});\n\n/** A compiled schema whose valid values have type `T` (`unknown` unless the schema was built with the builders). */\nexport class Validator<T = unknown> {\n\t/** Whether `value` is valid. A plain function: it may be passed around without its validator. */\n\treadonly check: (value: unknown) => value is T;\n\tprotected readonly schema: JsonSchema;\n\tprotected readonly options: CompileOptions;\n\tprivate explainer: Explainer | undefined = undefined;\n\n\tconstructor(schema: JsonSchema, options: CompileOptions, check: Check) {\n\t\t// A compiled check is exactly the test that a value has the schema's static type.\n\t\tthis.check = check as (value: unknown) => value is T;\n\t\tthis.schema = schema;\n\t\tthis.options = options;\n\t}\n\n\t/**\n\t * Why `value` is invalid: one error per failing keyword, `[]` when it is valid. Built on the first failure and\n\t * kept, so a validator whose values always pass never pays for it.\n\t */\n\terrors(value: unknown): ValidationError[] {\n\t\tif (this.check(value)) return [];\n\t\tthis.explainer ??= new Explainer(compiler(this.schema, this.options), this.schema);\n\t\treturn this.explainer.errors(value);\n\t}\n\n\t/**\n\t * `value` itself when it is valid, else its errors: check() and errors() in one result, without coercion.\n\t *\n\t * Not TypeBox's `Value.Parse`, which also clones, fills defaults, converts and strips undeclared properties before\n\t * it checks. This only checks: the strict replacement where `Value.Parse` is used to validate (its other steps\n\t * change nothing for a valid value of a schema without defaults or extra properties). Use `parse()` with\n\t * `coerce: true` for conversion.\n\t */\n\tvalidate(value: unknown): ParseResult<T> {\n\t\treturn this.check(value) ? { ok: true, value } : { ok: false, errors: this.errors(value) };\n\t}\n}\n\n/** A validator compiled with `coerce: true`: it also coerces loosely typed input towards the schema. */\nexport class CoercingValidator<T = unknown> extends Validator<T> {\n\tprivate coercer: Coercer | undefined = undefined;\n\n\t/**\n\t * `value` coerced towards the schema: numeric strings to numbers, `\"true\"` to `true`, a single value to a one-item\n\t * array, an optional `null` dropped, the first union branch the value converts to (the README lists every rule).\n\t * Only the changed paths are copied; `value` comes back itself when nothing changes, and is never modified. Values\n\t * that cannot be coerced stay as they are. The rules are built on the first call.\n\t */\n\tcoerce(value: unknown): unknown {\n\t\tthis.coercer ??= new Coercer(compiler(this.schema, this.options), this.schema);\n\t\treturn this.coercer.coerce(value);\n\t}\n\n\t/** Coerces `value`, then checks it: the coerced value when it is valid, else its errors. */\n\tparse(value: unknown): ParseResult<T> {\n\t\treturn this.validate(this.coerce(value));\n\t}\n}\n\nfunction compileCheck(schema: JsonSchema, options: CompileOptions): Check {\n\treturn compiler(schema, options).root();\n}\n\n// Most schemas compile without tracking the dynamic scope; one with a bookended $dynamicRef compiles again with it.\nfunction compiler(schema: JsonSchema, options: CompileOptions): Compiler {\n\tconst plain = new Compiler(schema, options, false);\n\ttry {\n\t\tplain.root();\n\t\treturn plain;\n\t} catch (error) {\n\t\tif (error !== RESTART) throw error;\n\t\treturn new Compiler(schema, options, true);\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,13 +1,66 @@
1
1
  {
2
2
  "name": "@bastani/atomic-schema",
3
- "version": "0.0.0-placeholder.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.9.88-alpha.1",
4
+ "description": "Atomic's JSON Schema builders and validation without eval: drafts 2020-12 and 7, compiled into plain closures",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "source": "./src/index.ts",
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ },
14
+ "./formats": {
15
+ "source": "./src/formats/index.ts",
16
+ "types": "./dist/formats/index.d.ts",
17
+ "import": "./dist/formats/index.js"
18
+ }
19
+ },
20
+ "sideEffects": false,
21
+ "files": [
22
+ "dist",
23
+ "README.md",
24
+ "CHANGELOG.md",
25
+ "THIRD_PARTY_NOTICES",
26
+ "LICENSE",
27
+ "NOTICE.md"
28
+ ],
6
29
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
30
+ "clean": "node ../../scripts/package-files.mjs clean dist",
31
+ "build": "tsc -p tsconfig.build.json",
32
+ "test": "vitest --run",
33
+ "prepublishOnly": "npm run clean && npm run build"
34
+ },
35
+ "keywords": [
36
+ "json-schema",
37
+ "validation",
38
+ "validator",
39
+ "csp",
40
+ "atomic"
41
+ ],
42
+ "author": "Earendil Works",
43
+ "license": "MIT",
44
+ "contributors": [
45
+ "Bastani, Inc."
46
+ ],
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/bastani-inc/atomic.git",
53
+ "directory": "packages/schema"
54
+ },
55
+ "homepage": "https://docs.bastani.ai/libraries/schema",
56
+ "bugs": {
57
+ "url": "https://github.com/bastani-inc/atomic/issues"
58
+ },
59
+ "engines": {
60
+ "node": ">=22.19.0"
8
61
  },
9
- "keywords": [],
10
- "author": "",
11
- "license": "ISC",
12
- "type": "commonjs"
62
+ "devDependencies": {
63
+ "typebox": "1.3.27",
64
+ "vitest": "4.1.12"
65
+ }
13
66
  }