@modulify/validator 0.2.1 → 0.3.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 +29 -1
- package/README.md +12 -2
- package/dist/assert.cjs +99 -63
- package/dist/assert.d.cts +37 -0
- package/dist/assert.d.mts +37 -0
- package/dist/assert.d.ts +24 -3
- package/dist/assert.mjs +94 -64
- package/dist/assertions.cjs +283 -178
- package/dist/assertions.d.cts +56 -0
- package/dist/assertions.d.mts +56 -0
- package/dist/assertions.d.ts +43 -45
- package/dist/assertions.mjs +274 -201
- package/dist/checkers.cjs +23 -0
- package/dist/checkers.d.cts +8 -0
- package/dist/checkers.d.mts +8 -0
- package/dist/checkers.d.ts +1 -1
- package/dist/checkers.mjs +16 -0
- package/dist/combinators.cjs +305 -304
- package/dist/combinators.d.cts +16 -0
- package/dist/combinators.d.mts +16 -0
- package/dist/combinators.d.ts +15 -16
- package/dist/combinators.mjs +305 -314
- package/dist/constraints.cjs +23 -0
- package/dist/constraints.d.cts +4 -0
- package/dist/constraints.d.mts +4 -0
- package/dist/constraints.d.ts +2 -2
- package/dist/constraints.mjs +21 -0
- package/dist/extractors.cjs +6 -0
- package/dist/extractors.d.cts +2 -0
- package/dist/extractors.d.mts +2 -0
- package/dist/extractors.mjs +5 -0
- package/dist/index.cjs +140 -219
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.mjs +93 -222
- package/dist/json-schema.cjs +364 -489
- package/dist/json-schema.d.cts +14 -0
- package/dist/json-schema.d.mts +14 -0
- package/dist/json-schema.d.ts +3 -3
- package/dist/json-schema.mjs +365 -492
- package/dist/metadata.cjs +98 -7
- package/dist/metadata.d.cts +8 -0
- package/dist/metadata.d.mts +8 -0
- package/dist/metadata.d.ts +2 -2
- package/dist/metadata.mjs +93 -7
- package/dist/predicates.cjs +98 -41
- package/dist/predicates.d.cts +77 -0
- package/dist/predicates.d.mts +77 -0
- package/dist/predicates.d.ts +25 -4
- package/dist/predicates.mjs +92 -66
- package/dist/types/index.d.cts +984 -0
- package/dist/types/index.d.mts +984 -0
- package/dist/types/index.d.ts +984 -0
- package/dist/types/json-schema.d.cts +75 -0
- package/dist/types/json-schema.d.mts +75 -0
- package/dist/types/json-schema.d.ts +75 -0
- package/dist/violations.cjs +81 -0
- package/dist/violations.d.cts +29 -0
- package/dist/violations.d.mts +29 -0
- package/dist/violations.d.ts +1 -1
- package/dist/violations.mjs +80 -0
- package/docs/RELEASING.md +66 -0
- package/docs/en/00-index.md +2 -0
- package/docs/en/01-shape-api.md +35 -10
- package/docs/en/02-metadata-and-introspection.md +2 -1
- package/docs/en/03-violations.md +1 -1
- package/docs/en/04-json-schema-export.md +5 -0
- package/docs/en/05-public-api.md +35 -3
- package/docs/en/06-common-recipes.md +2 -1
- package/docs/en/07-ai-reference.md +5 -2
- package/docs/en/08-violation-code-types.md +28 -2
- package/docs/en/09-migration.md +75 -0
- package/docs/ru/00-index.md +2 -0
- package/docs/ru/01-shape-api.md +35 -10
- package/docs/ru/02-metadata-and-introspection.md +2 -1
- package/docs/ru/03-violations.md +1 -1
- package/docs/ru/04-json-schema-export.md +5 -0
- package/docs/ru/05-public-api.md +35 -3
- package/docs/ru/06-common-recipes.md +2 -1
- package/docs/ru/07-ai-reference.md +5 -2
- package/docs/ru/08-violation-code-types.md +28 -2
- package/docs/ru/09-migration.md +76 -0
- package/docs/ru/README.md +10 -2
- package/package.json +63 -37
- package/types/index.d.ts +275 -115
- package/dist/metadata.cjs.js +0 -130
- package/dist/metadata.es.js +0 -131
package/dist/metadata.cjs.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const isValidator = (constraint) => "run" in constraint;
|
|
3
|
-
function arrayify(value) {
|
|
4
|
-
return Array.isArray(value) ? [...value] : [value];
|
|
5
|
-
}
|
|
6
|
-
function matchesConstraints(value, constraints) {
|
|
7
|
-
return arrayify(constraints).every((constraint) => constraint.check(value));
|
|
8
|
-
}
|
|
9
|
-
const constraintDescriptorSymbol = /* @__PURE__ */ Symbol("modulify.validator.descriptor");
|
|
10
|
-
const constraintMetadataSymbol = /* @__PURE__ */ Symbol("modulify.validator.metadata");
|
|
11
|
-
const cloneCallableConstraint = (constraint) => {
|
|
12
|
-
const source = constraint;
|
|
13
|
-
const cloned = ((value) => source(value));
|
|
14
|
-
const {
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
16
|
-
length: _length,
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
18
|
-
name: _name,
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
20
|
-
prototype: _prototype,
|
|
21
|
-
...descriptors
|
|
22
|
-
} = Object.getOwnPropertyDescriptors(source);
|
|
23
|
-
Object.defineProperties(cloned, descriptors);
|
|
24
|
-
try {
|
|
25
|
-
Object.defineProperty(cloned, "name", {
|
|
26
|
-
configurable: true,
|
|
27
|
-
value: source.name
|
|
28
|
-
});
|
|
29
|
-
} catch {
|
|
30
|
-
}
|
|
31
|
-
Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
|
|
32
|
-
return cloned;
|
|
33
|
-
};
|
|
34
|
-
const cloneConstraint = (constraint) => {
|
|
35
|
-
if (typeof constraint === "function") {
|
|
36
|
-
return cloneCallableConstraint(constraint);
|
|
37
|
-
}
|
|
38
|
-
return Object.create(
|
|
39
|
-
Object.getPrototypeOf(constraint),
|
|
40
|
-
Object.getOwnPropertyDescriptors(constraint)
|
|
41
|
-
);
|
|
42
|
-
};
|
|
43
|
-
const freezeMetadata = (metadata) => Object.freeze({ ...metadata });
|
|
44
|
-
const getConstraintMetadata = (constraint) => {
|
|
45
|
-
return constraint[constraintMetadataSymbol];
|
|
46
|
-
};
|
|
47
|
-
const getDescriptorFactory = (constraint) => {
|
|
48
|
-
return constraint[constraintDescriptorSymbol];
|
|
49
|
-
};
|
|
50
|
-
const getPublicDescriptor = (constraint) => {
|
|
51
|
-
if (!isValidator(constraint)) {
|
|
52
|
-
return void 0;
|
|
53
|
-
}
|
|
54
|
-
const { describe: describe2 } = constraint;
|
|
55
|
-
return typeof describe2 === "function" ? describe2.call(constraint) : void 0;
|
|
56
|
-
};
|
|
57
|
-
const inferAssertionDescriptor = (assertion) => ({
|
|
58
|
-
kind: "assertion",
|
|
59
|
-
name: assertion.name,
|
|
60
|
-
bail: assertion.bail,
|
|
61
|
-
code: assertion.name,
|
|
62
|
-
args: [],
|
|
63
|
-
constraints: assertion.constraints.map(([, , code, ...args]) => ({
|
|
64
|
-
code,
|
|
65
|
-
args
|
|
66
|
-
}))
|
|
67
|
-
});
|
|
68
|
-
const withMetadata = (descriptor, metadata) => {
|
|
69
|
-
if (!metadata) {
|
|
70
|
-
return descriptor;
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
...descriptor,
|
|
74
|
-
metadata
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
const attachConstraintDescriptor = (constraint, describe2) => {
|
|
78
|
-
Object.defineProperty(constraint, constraintDescriptorSymbol, {
|
|
79
|
-
configurable: false,
|
|
80
|
-
enumerable: false,
|
|
81
|
-
value: describe2,
|
|
82
|
-
writable: false
|
|
83
|
-
});
|
|
84
|
-
return constraint;
|
|
85
|
-
};
|
|
86
|
-
const meta = (constraint, metadata) => {
|
|
87
|
-
const cloned = cloneConstraint(constraint);
|
|
88
|
-
const nextMetadata = freezeMetadata({
|
|
89
|
-
...getConstraintMetadata(constraint) ?? {},
|
|
90
|
-
...metadata
|
|
91
|
-
});
|
|
92
|
-
Object.defineProperty(cloned, constraintMetadataSymbol, {
|
|
93
|
-
configurable: true,
|
|
94
|
-
enumerable: false,
|
|
95
|
-
value: nextMetadata,
|
|
96
|
-
writable: false
|
|
97
|
-
});
|
|
98
|
-
return cloned;
|
|
99
|
-
};
|
|
100
|
-
const custom = (validator) => validator;
|
|
101
|
-
const describeConstraints = (constraints) => {
|
|
102
|
-
const values = arrayify(constraints);
|
|
103
|
-
return values.length === 1 ? describe(values[0]) : {
|
|
104
|
-
kind: "allOf",
|
|
105
|
-
constraints: values.map((value) => describe(value))
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
const describe = (constraint) => {
|
|
109
|
-
const factory = getDescriptorFactory(constraint);
|
|
110
|
-
const metadata = getConstraintMetadata(constraint);
|
|
111
|
-
if (factory) {
|
|
112
|
-
return withMetadata(factory(), metadata);
|
|
113
|
-
}
|
|
114
|
-
const publicDescriptor = getPublicDescriptor(constraint);
|
|
115
|
-
if (publicDescriptor) {
|
|
116
|
-
return withMetadata(publicDescriptor, metadata);
|
|
117
|
-
}
|
|
118
|
-
if (isValidator(constraint)) {
|
|
119
|
-
return withMetadata({ kind: "validator" }, metadata);
|
|
120
|
-
}
|
|
121
|
-
return withMetadata(inferAssertionDescriptor(constraint), metadata);
|
|
122
|
-
};
|
|
123
|
-
exports.arrayify = arrayify;
|
|
124
|
-
exports.attachConstraintDescriptor = attachConstraintDescriptor;
|
|
125
|
-
exports.custom = custom;
|
|
126
|
-
exports.describe = describe;
|
|
127
|
-
exports.describeConstraints = describeConstraints;
|
|
128
|
-
exports.isValidator = isValidator;
|
|
129
|
-
exports.matchesConstraints = matchesConstraints;
|
|
130
|
-
exports.meta = meta;
|
package/dist/metadata.es.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
const isValidator = (constraint) => "run" in constraint;
|
|
2
|
-
function arrayify(value) {
|
|
3
|
-
return Array.isArray(value) ? [...value] : [value];
|
|
4
|
-
}
|
|
5
|
-
function matchesConstraints(value, constraints) {
|
|
6
|
-
return arrayify(constraints).every((constraint) => constraint.check(value));
|
|
7
|
-
}
|
|
8
|
-
const constraintDescriptorSymbol = /* @__PURE__ */ Symbol("modulify.validator.descriptor");
|
|
9
|
-
const constraintMetadataSymbol = /* @__PURE__ */ Symbol("modulify.validator.metadata");
|
|
10
|
-
const cloneCallableConstraint = (constraint) => {
|
|
11
|
-
const source = constraint;
|
|
12
|
-
const cloned = ((value) => source(value));
|
|
13
|
-
const {
|
|
14
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
-
length: _length,
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
17
|
-
name: _name,
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
19
|
-
prototype: _prototype,
|
|
20
|
-
...descriptors
|
|
21
|
-
} = Object.getOwnPropertyDescriptors(source);
|
|
22
|
-
Object.defineProperties(cloned, descriptors);
|
|
23
|
-
try {
|
|
24
|
-
Object.defineProperty(cloned, "name", {
|
|
25
|
-
configurable: true,
|
|
26
|
-
value: source.name
|
|
27
|
-
});
|
|
28
|
-
} catch {
|
|
29
|
-
}
|
|
30
|
-
Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
|
|
31
|
-
return cloned;
|
|
32
|
-
};
|
|
33
|
-
const cloneConstraint = (constraint) => {
|
|
34
|
-
if (typeof constraint === "function") {
|
|
35
|
-
return cloneCallableConstraint(constraint);
|
|
36
|
-
}
|
|
37
|
-
return Object.create(
|
|
38
|
-
Object.getPrototypeOf(constraint),
|
|
39
|
-
Object.getOwnPropertyDescriptors(constraint)
|
|
40
|
-
);
|
|
41
|
-
};
|
|
42
|
-
const freezeMetadata = (metadata) => Object.freeze({ ...metadata });
|
|
43
|
-
const getConstraintMetadata = (constraint) => {
|
|
44
|
-
return constraint[constraintMetadataSymbol];
|
|
45
|
-
};
|
|
46
|
-
const getDescriptorFactory = (constraint) => {
|
|
47
|
-
return constraint[constraintDescriptorSymbol];
|
|
48
|
-
};
|
|
49
|
-
const getPublicDescriptor = (constraint) => {
|
|
50
|
-
if (!isValidator(constraint)) {
|
|
51
|
-
return void 0;
|
|
52
|
-
}
|
|
53
|
-
const { describe: describe2 } = constraint;
|
|
54
|
-
return typeof describe2 === "function" ? describe2.call(constraint) : void 0;
|
|
55
|
-
};
|
|
56
|
-
const inferAssertionDescriptor = (assertion) => ({
|
|
57
|
-
kind: "assertion",
|
|
58
|
-
name: assertion.name,
|
|
59
|
-
bail: assertion.bail,
|
|
60
|
-
code: assertion.name,
|
|
61
|
-
args: [],
|
|
62
|
-
constraints: assertion.constraints.map(([, , code, ...args]) => ({
|
|
63
|
-
code,
|
|
64
|
-
args
|
|
65
|
-
}))
|
|
66
|
-
});
|
|
67
|
-
const withMetadata = (descriptor, metadata) => {
|
|
68
|
-
if (!metadata) {
|
|
69
|
-
return descriptor;
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
...descriptor,
|
|
73
|
-
metadata
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
const attachConstraintDescriptor = (constraint, describe2) => {
|
|
77
|
-
Object.defineProperty(constraint, constraintDescriptorSymbol, {
|
|
78
|
-
configurable: false,
|
|
79
|
-
enumerable: false,
|
|
80
|
-
value: describe2,
|
|
81
|
-
writable: false
|
|
82
|
-
});
|
|
83
|
-
return constraint;
|
|
84
|
-
};
|
|
85
|
-
const meta = (constraint, metadata) => {
|
|
86
|
-
const cloned = cloneConstraint(constraint);
|
|
87
|
-
const nextMetadata = freezeMetadata({
|
|
88
|
-
...getConstraintMetadata(constraint) ?? {},
|
|
89
|
-
...metadata
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(cloned, constraintMetadataSymbol, {
|
|
92
|
-
configurable: true,
|
|
93
|
-
enumerable: false,
|
|
94
|
-
value: nextMetadata,
|
|
95
|
-
writable: false
|
|
96
|
-
});
|
|
97
|
-
return cloned;
|
|
98
|
-
};
|
|
99
|
-
const custom = (validator) => validator;
|
|
100
|
-
const describeConstraints = (constraints) => {
|
|
101
|
-
const values = arrayify(constraints);
|
|
102
|
-
return values.length === 1 ? describe(values[0]) : {
|
|
103
|
-
kind: "allOf",
|
|
104
|
-
constraints: values.map((value) => describe(value))
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
const describe = (constraint) => {
|
|
108
|
-
const factory = getDescriptorFactory(constraint);
|
|
109
|
-
const metadata = getConstraintMetadata(constraint);
|
|
110
|
-
if (factory) {
|
|
111
|
-
return withMetadata(factory(), metadata);
|
|
112
|
-
}
|
|
113
|
-
const publicDescriptor = getPublicDescriptor(constraint);
|
|
114
|
-
if (publicDescriptor) {
|
|
115
|
-
return withMetadata(publicDescriptor, metadata);
|
|
116
|
-
}
|
|
117
|
-
if (isValidator(constraint)) {
|
|
118
|
-
return withMetadata({ kind: "validator" }, metadata);
|
|
119
|
-
}
|
|
120
|
-
return withMetadata(inferAssertionDescriptor(constraint), metadata);
|
|
121
|
-
};
|
|
122
|
-
export {
|
|
123
|
-
arrayify as a,
|
|
124
|
-
describe as b,
|
|
125
|
-
custom as c,
|
|
126
|
-
describeConstraints as d,
|
|
127
|
-
attachConstraintDescriptor as e,
|
|
128
|
-
matchesConstraints as f,
|
|
129
|
-
isValidator as i,
|
|
130
|
-
meta as m
|
|
131
|
-
};
|