@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.
- package/CHANGELOG.md +31 -0
- package/README.md +324 -107
- package/dist/assert.cjs +66 -0
- package/dist/assert.d.ts +16 -0
- package/dist/assert.mjs +66 -0
- package/dist/assertions.cjs +190 -92
- package/dist/assertions.d.ts +58 -2
- package/dist/assertions.mjs +191 -93
- package/dist/checkers.d.ts +8 -0
- package/dist/combinators.cjs +341 -0
- package/dist/combinators.d.ts +17 -0
- package/dist/combinators.mjs +341 -0
- package/dist/constraints.d.ts +4 -0
- package/dist/extractors.d.ts +2 -0
- package/dist/index.cjs +172 -61
- package/dist/index.d.ts +10 -4
- package/dist/index.mjs +176 -64
- package/dist/json-schema.cjs +514 -0
- package/dist/json-schema.d.ts +14 -0
- package/dist/json-schema.mjs +514 -0
- package/dist/metadata.cjs +8 -0
- package/dist/metadata.cjs.js +130 -0
- package/dist/metadata.d.ts +8 -0
- package/dist/metadata.es.js +131 -0
- package/dist/metadata.mjs +8 -0
- package/dist/predicates.cjs +40 -5
- package/dist/predicates.d.ts +25 -3
- package/dist/predicates.mjs +40 -5
- package/dist/violations.d.ts +29 -0
- package/docs/en/00-index.md +14 -0
- package/docs/en/01-shape-api.md +348 -0
- package/docs/en/02-metadata-and-introspection.md +276 -0
- package/docs/en/03-violations.md +267 -0
- package/docs/en/04-json-schema-export.md +264 -0
- package/docs/en/05-public-api.md +123 -0
- package/docs/en/06-common-recipes.md +273 -0
- package/docs/en/07-ai-reference.md +215 -0
- package/docs/en/08-violation-code-types.md +241 -0
- package/docs/ru/00-index.md +15 -0
- package/docs/ru/01-shape-api.md +348 -0
- package/docs/ru/02-metadata-and-introspection.md +276 -0
- package/docs/ru/03-violations.md +267 -0
- package/docs/ru/04-json-schema-export.md +264 -0
- package/docs/ru/05-public-api.md +123 -0
- package/docs/ru/06-common-recipes.md +273 -0
- package/docs/ru/07-ai-reference.md +215 -0
- package/docs/ru/08-violation-code-types.md +241 -0
- package/docs/ru/README.md +371 -0
- package/package.json +51 -33
- package/types/index.d.ts +789 -30
- package/types/json-schema.d.ts +75 -0
- package/dist/assertions/Assert.d.ts +0 -2
- package/dist/assertions/HasLength.d.ts +0 -7
- package/dist/assertions/check.d.ts +0 -3
- package/dist/assertions/index.d.ts +0 -16
- package/dist/runners/Each.d.ts +0 -3
- package/dist/runners/HasProperties.d.ts +0 -6
- package/dist/runners/index.d.ts +0 -2
- package/dist/runners.cjs +0 -32
- package/dist/runners.d.ts +0 -2
- 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,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[]>;
|
package/dist/runners/Each.d.ts
DELETED
|
@@ -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;
|
package/dist/runners/index.d.ts
DELETED
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
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
|
-
};
|