@modulify/validator 0.1.0 → 0.2.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 (61) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +324 -107
  3. package/dist/assert.cjs +66 -0
  4. package/dist/assert.d.ts +16 -0
  5. package/dist/assert.mjs +66 -0
  6. package/dist/assertions.cjs +190 -92
  7. package/dist/assertions.d.ts +58 -2
  8. package/dist/assertions.mjs +191 -93
  9. package/dist/checkers.d.ts +8 -0
  10. package/dist/combinators.cjs +341 -0
  11. package/dist/combinators.d.ts +17 -0
  12. package/dist/combinators.mjs +341 -0
  13. package/dist/constraints.d.ts +4 -0
  14. package/dist/extractors.d.ts +2 -0
  15. package/dist/index.cjs +172 -61
  16. package/dist/index.d.ts +10 -4
  17. package/dist/index.mjs +176 -64
  18. package/dist/json-schema.cjs +514 -0
  19. package/dist/json-schema.d.ts +14 -0
  20. package/dist/json-schema.mjs +514 -0
  21. package/dist/metadata.cjs +8 -0
  22. package/dist/metadata.cjs.js +130 -0
  23. package/dist/metadata.d.ts +8 -0
  24. package/dist/metadata.es.js +131 -0
  25. package/dist/metadata.mjs +8 -0
  26. package/dist/predicates.cjs +40 -5
  27. package/dist/predicates.d.ts +25 -3
  28. package/dist/predicates.mjs +40 -5
  29. package/dist/violations.d.ts +29 -0
  30. package/docs/en/00-index.md +14 -0
  31. package/docs/en/01-shape-api.md +348 -0
  32. package/docs/en/02-metadata-and-introspection.md +276 -0
  33. package/docs/en/03-violations.md +267 -0
  34. package/docs/en/04-json-schema-export.md +264 -0
  35. package/docs/en/05-public-api.md +123 -0
  36. package/docs/en/06-common-recipes.md +273 -0
  37. package/docs/en/07-ai-reference.md +215 -0
  38. package/docs/en/08-violation-code-types.md +241 -0
  39. package/docs/ru/00-index.md +15 -0
  40. package/docs/ru/01-shape-api.md +348 -0
  41. package/docs/ru/02-metadata-and-introspection.md +276 -0
  42. package/docs/ru/03-violations.md +267 -0
  43. package/docs/ru/04-json-schema-export.md +264 -0
  44. package/docs/ru/05-public-api.md +123 -0
  45. package/docs/ru/06-common-recipes.md +273 -0
  46. package/docs/ru/07-ai-reference.md +215 -0
  47. package/docs/ru/08-violation-code-types.md +241 -0
  48. package/docs/ru/README.md +371 -0
  49. package/package.json +51 -33
  50. package/types/index.d.ts +789 -30
  51. package/types/json-schema.d.ts +75 -0
  52. package/dist/assertions/Assert.d.ts +0 -2
  53. package/dist/assertions/HasLength.d.ts +0 -7
  54. package/dist/assertions/check.d.ts +0 -3
  55. package/dist/assertions/index.d.ts +0 -16
  56. package/dist/runners/Each.d.ts +0 -3
  57. package/dist/runners/HasProperties.d.ts +0 -6
  58. package/dist/runners/index.d.ts +0 -2
  59. package/dist/runners.cjs +0 -32
  60. package/dist/runners.d.ts +0 -2
  61. package/dist/runners.mjs +0 -32
@@ -0,0 +1,75 @@
1
+ import type {
2
+ Constraint,
3
+ ConstraintDescriptor,
4
+ MaybeMany,
5
+ } from './index'
6
+
7
+ /** JSON Schema primitive `type` values used by `toJsonSchema(...)`. */
8
+ export type JsonSchemaTypeName =
9
+ | 'string'
10
+ | 'number'
11
+ | 'integer'
12
+ | 'boolean'
13
+ | 'object'
14
+ | 'array'
15
+ | 'null'
16
+
17
+ /** Minimal JSON Schema object shape emitted by the built-in exporter. */
18
+ export interface JsonSchema {
19
+ readonly $schema?: string;
20
+ readonly $id?: string;
21
+ readonly $comment?: string;
22
+ readonly title?: string;
23
+ readonly description?: string;
24
+ readonly format?: string;
25
+ readonly default?: unknown;
26
+ readonly examples?: readonly unknown[];
27
+ readonly deprecated?: boolean;
28
+ readonly readOnly?: boolean;
29
+ readonly writeOnly?: boolean;
30
+ readonly type?: JsonSchemaTypeName | readonly JsonSchemaTypeName[];
31
+ readonly const?: unknown;
32
+ readonly enum?: readonly unknown[];
33
+ readonly allOf?: readonly JsonSchema[];
34
+ readonly anyOf?: readonly JsonSchema[];
35
+ readonly oneOf?: readonly JsonSchema[];
36
+ readonly properties?: Readonly<Record<string, JsonSchema>>;
37
+ readonly required?: readonly string[];
38
+ readonly additionalProperties?: boolean | JsonSchema;
39
+ readonly items?: JsonSchema;
40
+ readonly prefixItems?: readonly JsonSchema[];
41
+ readonly minItems?: number;
42
+ readonly maxItems?: number;
43
+ readonly minLength?: number;
44
+ readonly maxLength?: number;
45
+ readonly pattern?: string;
46
+ readonly minimum?: number;
47
+ readonly maximum?: number;
48
+ readonly multipleOf?: number;
49
+ }
50
+
51
+ /** Controls how `toJsonSchema(...)` handles constraints that cannot be represented faithfully. */
52
+ export type JsonSchemaExportMode = 'bestEffort' | 'strict'
53
+
54
+ /** Options for the JSON Schema exporter. */
55
+ export interface ToJsonSchemaOptions {
56
+ readonly mode?: JsonSchemaExportMode
57
+ }
58
+
59
+ /** Error thrown by `toJsonSchema(...)` in strict mode when a node cannot be exported faithfully. */
60
+ export declare class JsonSchemaExportError extends Error {
61
+ constructor(message: string, options: {
62
+ descriptor: ConstraintDescriptor
63
+ reason: string
64
+ path?: readonly PropertyKey[]
65
+ })
66
+ readonly descriptor: ConstraintDescriptor
67
+ readonly reason: string
68
+ readonly path: readonly PropertyKey[]
69
+ }
70
+
71
+ /** Derives a JSON Schema object from one or many public constraint descriptors. */
72
+ export declare const toJsonSchema: <const C extends MaybeMany<Constraint>>(
73
+ constraints: C,
74
+ options?: ToJsonSchemaOptions
75
+ ) => JsonSchema
@@ -1,2 +0,0 @@
1
- import { Assertion, Meta, Predicate } from '../../types';
2
- export declare const Assert: <T = unknown, M = unknown>(predicate: Predicate<T>, options: Meta<M>) => Assertion<T, M>;
@@ -1,7 +0,0 @@
1
- import { Assertion } from '../../types';
2
- export declare const HasLength: ({ exact, max, min, bail, }: {
3
- exact?: number | null;
4
- max?: number | null;
5
- min?: number | null;
6
- bail?: boolean;
7
- }) => Assertion<string | unknown[], unknown>;
@@ -1,3 +0,0 @@
1
- import { Assertion, Violation } from '../../types';
2
- declare const check: <T = unknown, M = unknown>(assert: Assertion<T, M>, value: unknown, path?: PropertyKey[]) => null | Violation<M>;
3
- export default check;
@@ -1,16 +0,0 @@
1
- import { Assert } from './Assert';
2
- import { Assertion } from '../../types';
3
- export { Assert };
4
- export { HasLength } from './HasLength';
5
- export declare const IsBoolean: Assertion<boolean, unknown>;
6
- export declare const IsDate: Assertion<Date, unknown>;
7
- export declare const IsDefined: Assertion<unknown, unknown>;
8
- export declare const IsEmail: Assertion<string, unknown>;
9
- export declare const IsNull: Assertion<null, unknown>;
10
- export declare const IsNumber: Assertion<number, unknown>;
11
- export declare const IsString: Assertion<string, unknown>;
12
- export declare const IsSymbol: Assertion<symbol, unknown>;
13
- export declare const OneOf: <Actual = unknown>(values: Actual[] | Record<string, Actual>, { equalTo, bail, }?: {
14
- equalTo?: (a: Actual, b: unknown) => boolean;
15
- bail?: boolean;
16
- }) => Assertion<Actual, Actual[]>;
@@ -1,3 +0,0 @@
1
- import { Constraint, MaybeMany, ValidationRunner } from '../../types';
2
- declare const _default: (constraints: MaybeMany<Constraint>) => ValidationRunner;
3
- export default _default;
@@ -1,6 +0,0 @@
1
- import { Constraint, MaybeMany, ValidationRunner } from '../../types';
2
- export type Descriptor<T extends object> = {
3
- [P in keyof T]: MaybeMany<Constraint>;
4
- };
5
- declare const _default: <T extends object>(descriptor: Descriptor<T>) => ValidationRunner;
6
- export default _default;
@@ -1,2 +0,0 @@
1
- export { default as Each } from './Each';
2
- export { default as HasProperties } from './HasProperties';
package/dist/runners.cjs DELETED
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const predicates = require("./predicates.cjs");
4
- const Each = (constraints) => {
5
- return {
6
- run(validate, value, path) {
7
- return predicates.isArray(value) ? value.map((v, i) => validate(v, constraints, [...path, i])) : [validate(value, constraints, [...path])];
8
- }
9
- };
10
- };
11
- const HasProperties = (descriptor) => ({
12
- run(validate, value, path) {
13
- if (predicates.isRecord(value)) {
14
- const fields = Object.keys(descriptor);
15
- return fields.reduce((accumulator, key) => [
16
- ...accumulator,
17
- validate(value[key], descriptor[key], [...path, key])
18
- ], []);
19
- } else {
20
- return [
21
- [{
22
- value,
23
- path,
24
- violates: "@modulify/validator/HasProperties",
25
- reason: "unsupported"
26
- }]
27
- ];
28
- }
29
- }
30
- });
31
- exports.Each = Each;
32
- exports.HasProperties = HasProperties;
package/dist/runners.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './runners/index'
2
- export {}
package/dist/runners.mjs DELETED
@@ -1,32 +0,0 @@
1
- import { isArray, isRecord } from "./predicates.mjs";
2
- const Each = (constraints) => {
3
- return {
4
- run(validate, value, path) {
5
- return isArray(value) ? value.map((v, i) => validate(v, constraints, [...path, i])) : [validate(value, constraints, [...path])];
6
- }
7
- };
8
- };
9
- const HasProperties = (descriptor) => ({
10
- run(validate, value, path) {
11
- if (isRecord(value)) {
12
- const fields = Object.keys(descriptor);
13
- return fields.reduce((accumulator, key) => [
14
- ...accumulator,
15
- validate(value[key], descriptor[key], [...path, key])
16
- ], []);
17
- } else {
18
- return [
19
- [{
20
- value,
21
- path,
22
- violates: "@modulify/validator/HasProperties",
23
- reason: "unsupported"
24
- }]
25
- ];
26
- }
27
- }
28
- });
29
- export {
30
- Each,
31
- HasProperties
32
- };