@modulify/validator 0.3.0 → 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/dist/index.mjs CHANGED
@@ -1,85 +1,9 @@
1
- import { a as refine, c as custom, d as meta, f as arrayify, i as isRefinementAssertion, l as describe, o as runRefinementAssertion, p as isValidator, t as assert } from "./assert-CzMg5aO7.mjs";
2
- import { A as startsWith, C as isSafeInteger, D as isValidDate, E as isSymbol, O as multipleOf, S as isRegExp, T as isString, _ as isMap, a as hasValue, b as isNumber, c as isBoolean, d as isEmail, f as isError, g as isInteger, h as isFunction, i as hasSize, k as oneOf, l as isDate, m as isFiniteNumber, n as hasLength, o as isBigInt, p as isFile, r as hasPattern, s as isBlob, t as endsWith, u as isDefined, v as isNaN, w as isSet, x as isPromiseLike, y as isNull } from "./assertions-DPYCHREw.mjs";
1
+ import { arrayify, isValidator } from "./constraints.mjs";
2
+ import { custom, describe, meta } from "./metadata.mjs";
3
+ import { assert, isRefinementAssertion, refine, runRefinementAssertion } from "./assert.mjs";
4
+ import { endsWith, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isError, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isValidDate, multipleOf, oneOf, startsWith } from "./assertions.mjs";
3
5
  import { discriminatedUnion, each, exact, nullable, nullish, optional, record, shape, tuple, union } from "./combinators.mjs";
4
- //#region src/violations.ts
5
- var ROOT_PATH = Object.freeze([]);
6
- var normalizePath = (path) => path ?? ROOT_PATH;
7
- var isSamePath = (left, right = ROOT_PATH) => {
8
- const normalizedLeft = normalizePath(left);
9
- if (normalizedLeft.length !== right.length) return false;
10
- return normalizedLeft.every((part, index) => Object.is(part, right[index]));
11
- };
12
- var isPathPrefix = (path, prefix) => {
13
- if (prefix.length > path.length) return false;
14
- return prefix.every((part, index) => Object.is(part, path[index]));
15
- };
16
- var createTreeState = (path) => ({
17
- path: Object.freeze([...path]),
18
- self: [],
19
- subtree: [],
20
- children: /* @__PURE__ */ new Map()
21
- });
22
- var TreeNode = class TreeNode {
23
- constructor(state) {
24
- this.path = state.path;
25
- this.self = new ViolationCollection(state.self);
26
- this.subtree = new ViolationCollection(state.subtree);
27
- this.children = new Map([...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)]));
28
- }
29
- at(path) {
30
- if (!isPathPrefix(path, this.path)) return;
31
- if (path.length === this.path.length) return this;
32
- return this.children.get(path[this.path.length])?.at(path);
33
- }
34
- };
35
- var buildTree = (violations) => {
36
- const root = createTreeState(ROOT_PATH);
37
- violations.forEach((violation) => {
38
- const path = normalizePath(violation.path);
39
- root.subtree.push(violation);
40
- if (path.length === 0) {
41
- root.self.push(violation);
42
- return;
43
- }
44
- let current = root;
45
- const currentPath = [];
46
- path.forEach((segment, index) => {
47
- currentPath.push(segment);
48
- let child = current.children.get(segment);
49
- if (!child) {
50
- child = createTreeState(currentPath);
51
- current.children.set(segment, child);
52
- }
53
- child.subtree.push(violation);
54
- if (index === path.length - 1) child.self.push(violation);
55
- current = child;
56
- });
57
- });
58
- return new TreeNode(root);
59
- };
60
- var ViolationCollection = class ViolationCollection {
61
- constructor(violations) {
62
- this.violations = [...violations];
63
- this.size = this.violations.length;
64
- }
65
- [Symbol.iterator]() {
66
- return this.violations[Symbol.iterator]();
67
- }
68
- forEach(callback) {
69
- this.violations.forEach((violation, index) => callback(violation, index, this));
70
- }
71
- map(callback) {
72
- return this.violations.map((violation, index) => callback(violation, index, this));
73
- }
74
- at(path) {
75
- return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
76
- }
77
- tree() {
78
- return buildTree(this.violations);
79
- }
80
- };
81
- var collection = (violations) => new ViolationCollection(violations);
82
- //#endregion
6
+ import { ViolationCollection, collection } from "./violations.mjs";
83
7
  //#region src/index.ts
84
8
  var collectViolations = async (value, constraints, path = []) => {
85
9
  const validations = [];
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_assert = require("./assert-BDOtHdLx.cjs");
2
+ const require_metadata = require("./metadata.cjs");
3
3
  //#region src/json-schema.ts
4
4
  var jsonSchemaMetadataKeys = [
5
5
  "title",
@@ -379,7 +379,7 @@ var exportDescriptor = (descriptor, context) => {
379
379
  return applyMetadata(schema, descriptor.metadata);
380
380
  };
381
381
  var toJsonSchema = (constraints, options = {}) => {
382
- return exportDescriptor(require_assert.describeConstraints(constraints), {
382
+ return exportDescriptor(require_metadata.describeConstraints(constraints), {
383
383
  mode: options.mode ?? "bestEffort",
384
384
  path: []
385
385
  });
@@ -1,4 +1,4 @@
1
- import { u as describeConstraints } from "./assert-CzMg5aO7.mjs";
1
+ import { describeConstraints } from "./metadata.mjs";
2
2
  //#region src/json-schema.ts
3
3
  var jsonSchemaMetadataKeys = [
4
4
  "title",
package/dist/metadata.cjs CHANGED
@@ -1,7 +1,99 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_assert = require("./assert-BDOtHdLx.cjs");
3
- exports.attachConstraintDescriptor = require_assert.attachConstraintDescriptor;
4
- exports.custom = require_assert.custom;
5
- exports.describe = require_assert.describe;
6
- exports.describeConstraints = require_assert.describeConstraints;
7
- exports.meta = require_assert.meta;
2
+ const require_constraints = require("./constraints.cjs");
3
+ //#region src/metadata.ts
4
+ var constraintDescriptorSymbol = Symbol("modulify.validator.descriptor");
5
+ var constraintMetadataSymbol = Symbol("modulify.validator.metadata");
6
+ var cloneCallableConstraint = (constraint) => {
7
+ const source = constraint;
8
+ const cloned = ((value) => source(value));
9
+ const { length: _length, name: _name, prototype: _prototype, ...descriptors } = Object.getOwnPropertyDescriptors(source);
10
+ Object.defineProperties(cloned, descriptors);
11
+ try {
12
+ Object.defineProperty(cloned, "name", {
13
+ configurable: true,
14
+ value: source.name
15
+ });
16
+ } catch {}
17
+ Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
18
+ return cloned;
19
+ };
20
+ var cloneConstraint = (constraint) => {
21
+ if (typeof constraint === "function") return cloneCallableConstraint(constraint);
22
+ return Object.create(Object.getPrototypeOf(constraint), Object.getOwnPropertyDescriptors(constraint));
23
+ };
24
+ var freezeMetadata = (metadata) => Object.freeze({ ...metadata });
25
+ var getConstraintMetadata = (constraint) => {
26
+ return constraint[constraintMetadataSymbol];
27
+ };
28
+ var getDescriptorFactory = (constraint) => {
29
+ return constraint[constraintDescriptorSymbol];
30
+ };
31
+ var getPublicDescriptor = (constraint) => {
32
+ if (!require_constraints.isValidator(constraint)) return;
33
+ const { describe } = constraint;
34
+ return typeof describe === "function" ? describe.call(constraint) : void 0;
35
+ };
36
+ var inferAssertionDescriptor = (assertion) => ({
37
+ kind: "assertion",
38
+ name: assertion.name,
39
+ bail: assertion.bail,
40
+ code: assertion.name,
41
+ args: [],
42
+ constraints: assertion.constraints.map(([, , code, ...args]) => ({
43
+ code,
44
+ args
45
+ }))
46
+ });
47
+ var withMetadata = (descriptor, metadata) => {
48
+ if (!metadata) return descriptor;
49
+ return {
50
+ ...descriptor,
51
+ metadata
52
+ };
53
+ };
54
+ var attachConstraintDescriptor = (constraint, describe) => {
55
+ Object.defineProperty(constraint, constraintDescriptorSymbol, {
56
+ configurable: false,
57
+ enumerable: false,
58
+ value: describe,
59
+ writable: false
60
+ });
61
+ return constraint;
62
+ };
63
+ var meta = (constraint, metadata) => {
64
+ const cloned = cloneConstraint(constraint);
65
+ const nextMetadata = freezeMetadata({
66
+ ...getConstraintMetadata(constraint) ?? {},
67
+ ...metadata
68
+ });
69
+ Object.defineProperty(cloned, constraintMetadataSymbol, {
70
+ configurable: true,
71
+ enumerable: false,
72
+ value: nextMetadata,
73
+ writable: false
74
+ });
75
+ return cloned;
76
+ };
77
+ var custom = (validator) => validator;
78
+ var describeConstraints = (constraints) => {
79
+ const values = require_constraints.arrayify(constraints);
80
+ return values.length === 1 ? describe(values[0]) : {
81
+ kind: "allOf",
82
+ constraints: values.map((value) => describe(value))
83
+ };
84
+ };
85
+ var describe = (constraint) => {
86
+ const factory = getDescriptorFactory(constraint);
87
+ const metadata = getConstraintMetadata(constraint);
88
+ if (factory) return withMetadata(factory(), metadata);
89
+ const publicDescriptor = getPublicDescriptor(constraint);
90
+ if (publicDescriptor) return withMetadata(publicDescriptor, metadata);
91
+ if (require_constraints.isValidator(constraint)) return withMetadata({ kind: "validator" }, metadata);
92
+ return withMetadata(inferAssertionDescriptor(constraint), metadata);
93
+ };
94
+ //#endregion
95
+ exports.attachConstraintDescriptor = attachConstraintDescriptor;
96
+ exports.custom = custom;
97
+ exports.describe = describe;
98
+ exports.describeConstraints = describeConstraints;
99
+ exports.meta = meta;
package/dist/metadata.mjs CHANGED
@@ -1,2 +1,94 @@
1
- import { c as custom, d as meta, l as describe, s as attachConstraintDescriptor, u as describeConstraints } from "./assert-CzMg5aO7.mjs";
1
+ import { arrayify, isValidator } from "./constraints.mjs";
2
+ //#region src/metadata.ts
3
+ var constraintDescriptorSymbol = Symbol("modulify.validator.descriptor");
4
+ var constraintMetadataSymbol = Symbol("modulify.validator.metadata");
5
+ var cloneCallableConstraint = (constraint) => {
6
+ const source = constraint;
7
+ const cloned = ((value) => source(value));
8
+ const { length: _length, name: _name, prototype: _prototype, ...descriptors } = Object.getOwnPropertyDescriptors(source);
9
+ Object.defineProperties(cloned, descriptors);
10
+ try {
11
+ Object.defineProperty(cloned, "name", {
12
+ configurable: true,
13
+ value: source.name
14
+ });
15
+ } catch {}
16
+ Object.setPrototypeOf(cloned, Object.getPrototypeOf(source));
17
+ return cloned;
18
+ };
19
+ var cloneConstraint = (constraint) => {
20
+ if (typeof constraint === "function") return cloneCallableConstraint(constraint);
21
+ return Object.create(Object.getPrototypeOf(constraint), Object.getOwnPropertyDescriptors(constraint));
22
+ };
23
+ var freezeMetadata = (metadata) => Object.freeze({ ...metadata });
24
+ var getConstraintMetadata = (constraint) => {
25
+ return constraint[constraintMetadataSymbol];
26
+ };
27
+ var getDescriptorFactory = (constraint) => {
28
+ return constraint[constraintDescriptorSymbol];
29
+ };
30
+ var getPublicDescriptor = (constraint) => {
31
+ if (!isValidator(constraint)) return;
32
+ const { describe } = constraint;
33
+ return typeof describe === "function" ? describe.call(constraint) : void 0;
34
+ };
35
+ var inferAssertionDescriptor = (assertion) => ({
36
+ kind: "assertion",
37
+ name: assertion.name,
38
+ bail: assertion.bail,
39
+ code: assertion.name,
40
+ args: [],
41
+ constraints: assertion.constraints.map(([, , code, ...args]) => ({
42
+ code,
43
+ args
44
+ }))
45
+ });
46
+ var withMetadata = (descriptor, metadata) => {
47
+ if (!metadata) return descriptor;
48
+ return {
49
+ ...descriptor,
50
+ metadata
51
+ };
52
+ };
53
+ var attachConstraintDescriptor = (constraint, describe) => {
54
+ Object.defineProperty(constraint, constraintDescriptorSymbol, {
55
+ configurable: false,
56
+ enumerable: false,
57
+ value: describe,
58
+ writable: false
59
+ });
60
+ return constraint;
61
+ };
62
+ var meta = (constraint, metadata) => {
63
+ const cloned = cloneConstraint(constraint);
64
+ const nextMetadata = freezeMetadata({
65
+ ...getConstraintMetadata(constraint) ?? {},
66
+ ...metadata
67
+ });
68
+ Object.defineProperty(cloned, constraintMetadataSymbol, {
69
+ configurable: true,
70
+ enumerable: false,
71
+ value: nextMetadata,
72
+ writable: false
73
+ });
74
+ return cloned;
75
+ };
76
+ var custom = (validator) => validator;
77
+ var describeConstraints = (constraints) => {
78
+ const values = arrayify(constraints);
79
+ return values.length === 1 ? describe(values[0]) : {
80
+ kind: "allOf",
81
+ constraints: values.map((value) => describe(value))
82
+ };
83
+ };
84
+ var describe = (constraint) => {
85
+ const factory = getDescriptorFactory(constraint);
86
+ const metadata = getConstraintMetadata(constraint);
87
+ if (factory) return withMetadata(factory(), metadata);
88
+ const publicDescriptor = getPublicDescriptor(constraint);
89
+ if (publicDescriptor) return withMetadata(publicDescriptor, metadata);
90
+ if (isValidator(constraint)) return withMetadata({ kind: "validator" }, metadata);
91
+ return withMetadata(inferAssertionDescriptor(constraint), metadata);
92
+ };
93
+ //#endregion
2
94
  export { attachConstraintDescriptor, custom, describe, describeConstraints, meta };
@@ -0,0 +1,81 @@
1
+ //#region src/violations.ts
2
+ var ROOT_PATH = Object.freeze([]);
3
+ var normalizePath = (path) => path ?? ROOT_PATH;
4
+ var isSamePath = (left, right = ROOT_PATH) => {
5
+ const normalizedLeft = normalizePath(left);
6
+ if (normalizedLeft.length !== right.length) return false;
7
+ return normalizedLeft.every((part, index) => Object.is(part, right[index]));
8
+ };
9
+ var isPathPrefix = (path, prefix) => {
10
+ if (prefix.length > path.length) return false;
11
+ return prefix.every((part, index) => Object.is(part, path[index]));
12
+ };
13
+ var createTreeState = (path) => ({
14
+ path: Object.freeze([...path]),
15
+ self: [],
16
+ subtree: [],
17
+ children: /* @__PURE__ */ new Map()
18
+ });
19
+ var TreeNode = class TreeNode {
20
+ constructor(state) {
21
+ this.path = state.path;
22
+ this.self = new ViolationCollection(state.self);
23
+ this.subtree = new ViolationCollection(state.subtree);
24
+ this.children = new Map([...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)]));
25
+ }
26
+ at(path) {
27
+ if (!isPathPrefix(path, this.path)) return;
28
+ if (path.length === this.path.length) return this;
29
+ return this.children.get(path[this.path.length])?.at(path);
30
+ }
31
+ };
32
+ var buildTree = (violations) => {
33
+ const root = createTreeState(ROOT_PATH);
34
+ violations.forEach((violation) => {
35
+ const path = normalizePath(violation.path);
36
+ root.subtree.push(violation);
37
+ if (path.length === 0) {
38
+ root.self.push(violation);
39
+ return;
40
+ }
41
+ let current = root;
42
+ const currentPath = [];
43
+ path.forEach((segment, index) => {
44
+ currentPath.push(segment);
45
+ let child = current.children.get(segment);
46
+ if (!child) {
47
+ child = createTreeState(currentPath);
48
+ current.children.set(segment, child);
49
+ }
50
+ child.subtree.push(violation);
51
+ if (index === path.length - 1) child.self.push(violation);
52
+ current = child;
53
+ });
54
+ });
55
+ return new TreeNode(root);
56
+ };
57
+ var ViolationCollection = class ViolationCollection {
58
+ constructor(violations) {
59
+ this.violations = [...violations];
60
+ this.size = this.violations.length;
61
+ }
62
+ [Symbol.iterator]() {
63
+ return this.violations[Symbol.iterator]();
64
+ }
65
+ forEach(callback) {
66
+ this.violations.forEach((violation, index) => callback(violation, index, this));
67
+ }
68
+ map(callback) {
69
+ return this.violations.map((violation, index) => callback(violation, index, this));
70
+ }
71
+ at(path) {
72
+ return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
73
+ }
74
+ tree() {
75
+ return buildTree(this.violations);
76
+ }
77
+ };
78
+ var collection = (violations) => new ViolationCollection(violations);
79
+ //#endregion
80
+ exports.ViolationCollection = ViolationCollection;
81
+ exports.collection = collection;
@@ -0,0 +1,80 @@
1
+ //#region src/violations.ts
2
+ var ROOT_PATH = Object.freeze([]);
3
+ var normalizePath = (path) => path ?? ROOT_PATH;
4
+ var isSamePath = (left, right = ROOT_PATH) => {
5
+ const normalizedLeft = normalizePath(left);
6
+ if (normalizedLeft.length !== right.length) return false;
7
+ return normalizedLeft.every((part, index) => Object.is(part, right[index]));
8
+ };
9
+ var isPathPrefix = (path, prefix) => {
10
+ if (prefix.length > path.length) return false;
11
+ return prefix.every((part, index) => Object.is(part, path[index]));
12
+ };
13
+ var createTreeState = (path) => ({
14
+ path: Object.freeze([...path]),
15
+ self: [],
16
+ subtree: [],
17
+ children: /* @__PURE__ */ new Map()
18
+ });
19
+ var TreeNode = class TreeNode {
20
+ constructor(state) {
21
+ this.path = state.path;
22
+ this.self = new ViolationCollection(state.self);
23
+ this.subtree = new ViolationCollection(state.subtree);
24
+ this.children = new Map([...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)]));
25
+ }
26
+ at(path) {
27
+ if (!isPathPrefix(path, this.path)) return;
28
+ if (path.length === this.path.length) return this;
29
+ return this.children.get(path[this.path.length])?.at(path);
30
+ }
31
+ };
32
+ var buildTree = (violations) => {
33
+ const root = createTreeState(ROOT_PATH);
34
+ violations.forEach((violation) => {
35
+ const path = normalizePath(violation.path);
36
+ root.subtree.push(violation);
37
+ if (path.length === 0) {
38
+ root.self.push(violation);
39
+ return;
40
+ }
41
+ let current = root;
42
+ const currentPath = [];
43
+ path.forEach((segment, index) => {
44
+ currentPath.push(segment);
45
+ let child = current.children.get(segment);
46
+ if (!child) {
47
+ child = createTreeState(currentPath);
48
+ current.children.set(segment, child);
49
+ }
50
+ child.subtree.push(violation);
51
+ if (index === path.length - 1) child.self.push(violation);
52
+ current = child;
53
+ });
54
+ });
55
+ return new TreeNode(root);
56
+ };
57
+ var ViolationCollection = class ViolationCollection {
58
+ constructor(violations) {
59
+ this.violations = [...violations];
60
+ this.size = this.violations.length;
61
+ }
62
+ [Symbol.iterator]() {
63
+ return this.violations[Symbol.iterator]();
64
+ }
65
+ forEach(callback) {
66
+ this.violations.forEach((violation, index) => callback(violation, index, this));
67
+ }
68
+ map(callback) {
69
+ return this.violations.map((violation, index) => callback(violation, index, this));
70
+ }
71
+ at(path) {
72
+ return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
73
+ }
74
+ tree() {
75
+ return buildTree(this.violations);
76
+ }
77
+ };
78
+ var collection = (violations) => new ViolationCollection(violations);
79
+ //#endregion
80
+ export { ViolationCollection, collection };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Declarative validation util for JavaScript",
5
5
  "license": "MIT",
6
- "version": "0.3.0",
6
+ "version": "0.3.1",
7
7
  "exports": {
8
8
  ".": {
9
9
  "import": {