@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
@@ -1,109 +1,207 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const assert = require("./assert.cjs");
3
4
  const predicates = require("./predicates.cjs");
4
- const delegate = (p) => {
5
- return (value) => p(value);
5
+ const isEqual = (value, exact) => value === exact;
6
+ const isGte = (value, min) => value >= min;
7
+ const isLte = (value, max) => value <= max;
8
+ const inRange = (value, [min, max]) => value >= min && value <= max;
9
+ const startsWith$1 = (value, prefix) => value.startsWith(prefix);
10
+ const endsWith$1 = (value, suffix) => value.endsWith(suffix);
11
+ const matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
12
+ const isMultipleOf = (value, step) => {
13
+ if (step === 0) {
14
+ return false;
15
+ }
16
+ const quotient = value / step;
17
+ const nearestInteger = Math.round(quotient);
18
+ const delta = Math.abs(quotient - nearestInteger);
19
+ return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
6
20
  };
7
- const Assert = (predicate, options) => {
8
- const extender = (asserts = []) => {
9
- return {
10
- That(...asserts2) {
11
- return Object.assign(delegate(predicate), options, extender(asserts2));
12
- },
13
- get also() {
14
- return asserts;
15
- }
16
- };
17
- };
18
- return Object.assign(delegate(predicate), options, extender());
19
- };
20
- const isGTE = (min) => (x) => predicates.isNumber(x) && x >= min;
21
- const isLTE = (max) => (x) => predicates.isNumber(x) && x <= max;
22
- const IsEqual = (exact) => Assert((x) => predicates.isExact(exact)(x.length), {
23
- fqn: "@modulify/validator/HasLength[exact]",
24
- bail: false,
25
- reason: "exact",
26
- meta: exact
27
- });
28
- const IsGTE = (min) => Assert((x) => isGTE(min)(x.length), {
29
- fqn: "@modulify/validator/HasLength[min]",
30
- bail: false,
31
- reason: "min",
32
- meta: min
33
- });
34
- const IsLTE = (max) => Assert((x) => isLTE(max)(x.length), {
35
- fqn: "@modulify/validator/HasLength[max]",
36
- bail: false,
37
- reason: "max",
38
- meta: max
21
+ const length = (value) => value.length;
22
+ const size = (value) => value.size;
23
+ const isBoolean = assert.assert(predicates.isBoolean, { name: "isBoolean", bail: true, code: "type.boolean" });
24
+ const isBigInt = assert.assert(predicates.isBigInt, { name: "isBigInt", bail: true, code: "type.bigint" });
25
+ const isBlob = assert.assert(predicates.isBlob, { name: "isBlob", bail: true, code: "type.blob" });
26
+ const isDate = assert.assert(predicates.isDate, { name: "isDate", bail: true, code: "type.date" });
27
+ const isDefined = assert.assert((value) => value !== void 0, {
28
+ name: "isDefined",
29
+ bail: true,
30
+ code: "value.defined"
39
31
  });
40
- const HasLength = ({
32
+ const isEmail = assert.assert(predicates.isEmail, { name: "isEmail", bail: true, code: "string.email" });
33
+ const isFile = assert.assert(predicates.isFile, { name: "isFile", bail: true, code: "type.file" });
34
+ const isFunction = assert.assert(predicates.isFunction, { name: "isFunction", bail: true, code: "type.function" });
35
+ const isMap = assert.assert(predicates.isMap, { name: "isMap", bail: true, code: "type.map" });
36
+ const isNaN = assert.assert(predicates.isNaN, { name: "isNaN", bail: true, code: "number.nan" });
37
+ const isNull = assert.assert(predicates.isNull, { name: "isNull", bail: true, code: "type.null" });
38
+ const isNumber = assert.assert(predicates.isNumber, { name: "isNumber", bail: true, code: "type.number" });
39
+ const isSet = assert.assert(predicates.isSet, { name: "isSet", bail: true, code: "type.set" });
40
+ const isString = assert.assert(predicates.isString, { name: "isString", bail: true, code: "type.string" });
41
+ const isSymbol = assert.assert(predicates.isSymbol, { name: "isSymbol", bail: true, code: "type.symbol" });
42
+ const hasLength = ({
43
+ exact = null,
44
+ max = null,
45
+ min = null,
46
+ range = null,
47
+ bail = false
48
+ } = {}) => {
49
+ const constraints = [];
50
+ if (exact !== null) constraints.push([length, isEqual, "length.exact", exact]);
51
+ if (max !== null) constraints.push([length, isLte, "length.max", max]);
52
+ if (min !== null) constraints.push([length, isGte, "length.min", min]);
53
+ if (range !== null) constraints.push([length, inRange, "length.range", range]);
54
+ return assert.assert(
55
+ (value) => predicates.isArray(value) || predicates.isString(value),
56
+ {
57
+ name: "hasLength",
58
+ bail,
59
+ code: "length.unsupported-type"
60
+ },
61
+ constraints
62
+ );
63
+ };
64
+ const hasSize = ({
41
65
  exact = null,
42
66
  max = null,
43
67
  min = null,
68
+ range = null,
44
69
  bail = false
45
- }) => Assert(predicates.Or(predicates.isString, predicates.isArray), {
46
- fqn: "@modulify/validator/HasLength",
47
- bail,
48
- reason: "unsupported"
49
- }).That(
50
- ...exact !== null ? [IsEqual(exact)] : [],
51
- ...max !== null ? [IsLTE(max)] : [],
52
- ...min !== null ? [IsGTE(min)] : []
70
+ } = {}) => {
71
+ const constraints = [];
72
+ if (exact !== null) constraints.push([size, isEqual, "size.exact", exact]);
73
+ if (max !== null) constraints.push([size, isLte, "size.max", max]);
74
+ if (min !== null) constraints.push([size, isGte, "size.min", min]);
75
+ if (range !== null) constraints.push([size, inRange, "size.range", range]);
76
+ return assert.assert(
77
+ (value) => predicates.isMap(value) || predicates.isSet(value),
78
+ {
79
+ name: "hasSize",
80
+ bail,
81
+ code: "size.unsupported-type"
82
+ },
83
+ constraints
84
+ );
85
+ };
86
+ const hasPattern = (pattern, {
87
+ bail = false
88
+ } = {}) => assert.assert(
89
+ predicates.isString,
90
+ {
91
+ name: "hasPattern",
92
+ bail,
93
+ code: "string.unsupported-type"
94
+ },
95
+ [[
96
+ (value) => value,
97
+ matchesPattern,
98
+ "string.pattern",
99
+ pattern
100
+ ]]
53
101
  );
54
- const IsBoolean = Assert(predicates.isBoolean, {
55
- fqn: "@modulify/validator/IsBoolean",
56
- bail: true
57
- });
58
- const IsDate = Assert(predicates.isDate, {
59
- fqn: "@modulify/validator/IsDate",
60
- bail: true
61
- });
62
- const IsDefined = Assert(predicates.Not(predicates.isUndefined), {
63
- fqn: "@modulify/validator/IsDefined",
64
- bail: true,
65
- reason: "undefined"
66
- });
67
- const IsEmail = Assert(predicates.isEmail, {
68
- fqn: "@modulify/validator/IsEmail",
69
- bail: true
70
- });
71
- const IsNull = Assert(predicates.isNull, {
72
- fqn: "@modulify/validator/IsNull",
73
- bail: true
74
- });
75
- const IsNumber = Assert(predicates.isNumber, {
76
- fqn: "@modulify/validator/IsNumber",
77
- bail: true
78
- });
79
- const IsString = Assert(predicates.isString, {
80
- fqn: "@modulify/validator/IsString",
81
- bail: true
82
- });
83
- const IsSymbol = Assert(predicates.isSymbol, {
84
- fqn: "@modulify/validator/IsSymbol",
85
- bail: true
86
- });
87
- const OneOf = (values, {
102
+ const startsWith = (prefix, {
103
+ bail = false
104
+ } = {}) => assert.assert(
105
+ predicates.isString,
106
+ {
107
+ name: "startsWith",
108
+ bail,
109
+ code: "string.unsupported-type"
110
+ },
111
+ [[
112
+ (value) => value,
113
+ startsWith$1,
114
+ "string.starts-with",
115
+ prefix
116
+ ]]
117
+ );
118
+ const endsWith = (suffix, {
119
+ bail = false
120
+ } = {}) => assert.assert(
121
+ predicates.isString,
122
+ {
123
+ name: "endsWith",
124
+ bail,
125
+ code: "string.unsupported-type"
126
+ },
127
+ [[
128
+ (value) => value,
129
+ endsWith$1,
130
+ "string.ends-with",
131
+ suffix
132
+ ]]
133
+ );
134
+ const hasValue = ({
135
+ exact = null,
136
+ max = null,
137
+ min = null,
138
+ range = null,
139
+ bail = false
140
+ } = {}) => {
141
+ const constraints = [];
142
+ if (exact !== null) constraints.push([(value) => value, isEqual, "number.exact", exact]);
143
+ if (max !== null) constraints.push([(value) => value, isLte, "number.max", max]);
144
+ if (min !== null) constraints.push([(value) => value, isGte, "number.min", min]);
145
+ if (range !== null) constraints.push([(value) => value, inRange, "number.range", range]);
146
+ return assert.assert(
147
+ predicates.isNumber,
148
+ {
149
+ name: "hasValue",
150
+ bail,
151
+ code: "number.unsupported-type"
152
+ },
153
+ constraints
154
+ );
155
+ };
156
+ const multipleOf = (step, {
157
+ bail = false
158
+ } = {}) => assert.assert(
159
+ predicates.isNumber,
160
+ {
161
+ name: "multipleOf",
162
+ bail,
163
+ code: "number.unsupported-type"
164
+ },
165
+ [[
166
+ (value) => value,
167
+ isMultipleOf,
168
+ "number.multiple-of",
169
+ step
170
+ ]]
171
+ );
172
+ const oneOf = (values, {
88
173
  equalTo = (a, b) => a === b,
89
174
  bail = false
90
175
  } = {}) => {
91
176
  const haystack = predicates.isArray(values) ? values : Object.values(values);
92
- const oneOf = (value) => haystack.some((allowed) => equalTo(allowed, value));
93
- return Assert(oneOf, {
94
- fqn: "@modulify/validator/OneOf",
177
+ return assert.assert((value) => haystack.some((item) => equalTo(item, value)), {
178
+ name: "oneOf",
95
179
  bail,
96
- meta: haystack
180
+ code: "value.one-of",
181
+ args: [haystack]
97
182
  });
98
183
  };
99
- exports.Assert = Assert;
100
- exports.HasLength = HasLength;
101
- exports.IsBoolean = IsBoolean;
102
- exports.IsDate = IsDate;
103
- exports.IsDefined = IsDefined;
104
- exports.IsEmail = IsEmail;
105
- exports.IsNull = IsNull;
106
- exports.IsNumber = IsNumber;
107
- exports.IsString = IsString;
108
- exports.IsSymbol = IsSymbol;
109
- exports.OneOf = OneOf;
184
+ exports.assert = assert.assert;
185
+ exports.endsWith = endsWith;
186
+ exports.hasLength = hasLength;
187
+ exports.hasPattern = hasPattern;
188
+ exports.hasSize = hasSize;
189
+ exports.hasValue = hasValue;
190
+ exports.isBigInt = isBigInt;
191
+ exports.isBlob = isBlob;
192
+ exports.isBoolean = isBoolean;
193
+ exports.isDate = isDate;
194
+ exports.isDefined = isDefined;
195
+ exports.isEmail = isEmail;
196
+ exports.isFile = isFile;
197
+ exports.isFunction = isFunction;
198
+ exports.isMap = isMap;
199
+ exports.isNaN = isNaN;
200
+ exports.isNull = isNull;
201
+ exports.isNumber = isNumber;
202
+ exports.isSet = isSet;
203
+ exports.isString = isString;
204
+ exports.isSymbol = isSymbol;
205
+ exports.multipleOf = multipleOf;
206
+ exports.oneOf = oneOf;
207
+ exports.startsWith = startsWith;
@@ -1,2 +1,58 @@
1
- export * from './assertions/index'
2
- export {}
1
+ import { AssertionConstraint, Assertion } from '../types';
2
+ import { assert } from './assert';
3
+ export { assert };
4
+ type LengthAssertionConstraint = AssertionConstraint<string | unknown[], number, [exact: number], 'length.exact'> | AssertionConstraint<string | unknown[], number, [max: number], 'length.max'> | AssertionConstraint<string | unknown[], number, [min: number], 'length.min'> | AssertionConstraint<string | unknown[], number, [range: [number, number]], 'length.range'>;
5
+ type SizeAssertionConstraint = AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [exact: number], 'size.exact'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [max: number], 'size.max'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [min: number], 'size.min'> | AssertionConstraint<Map<unknown, unknown> | Set<unknown>, number, [range: [number, number]], 'size.range'>;
6
+ type ValueAssertionConstraint = AssertionConstraint<number, number, [exact: number], 'number.exact'> | AssertionConstraint<number, number, [max: number], 'number.max'> | AssertionConstraint<number, number, [min: number], 'number.min'> | AssertionConstraint<number, number, [range: [number, number]], 'number.range'>;
7
+ export declare const isBoolean: Assertion<boolean, [], "type.boolean", undefined, "isBoolean">;
8
+ export declare const isBigInt: Assertion<bigint, [], "type.bigint", undefined, "isBigInt">;
9
+ export declare const isBlob: Assertion<Blob, [], "type.blob", undefined, "isBlob">;
10
+ export declare const isDate: Assertion<Date, [], "type.date", undefined, "isDate">;
11
+ export declare const isDefined: Assertion<{}, [], "value.defined", undefined, "isDefined">;
12
+ export declare const isEmail: Assertion<string, [], "string.email", undefined, "isEmail">;
13
+ export declare const isFile: Assertion<File, [], "type.file", undefined, "isFile">;
14
+ export declare const isFunction: Assertion<(...args: never[]) => unknown, [], "type.function", undefined, "isFunction">;
15
+ export declare const isMap: Assertion<Map<unknown, unknown>, [], "type.map", undefined, "isMap">;
16
+ export declare const isNaN: Assertion<number, [], "number.nan", undefined, "isNaN">;
17
+ export declare const isNull: Assertion<null, [], "type.null", undefined, "isNull">;
18
+ export declare const isNumber: Assertion<number, [], "type.number", undefined, "isNumber">;
19
+ export declare const isSet: Assertion<Set<unknown>, [], "type.set", undefined, "isSet">;
20
+ export declare const isString: Assertion<string, [], "type.string", undefined, "isString">;
21
+ export declare const isSymbol: Assertion<symbol, [], "type.symbol", undefined, "isSymbol">;
22
+ export declare const hasLength: ({ exact, max, min, range, bail, }?: {
23
+ exact?: number | null;
24
+ max?: number | null;
25
+ min?: number | null;
26
+ range?: [number, number] | null;
27
+ bail?: boolean;
28
+ }) => Assertion<string | unknown[], LengthAssertionConstraint[], "length.unsupported-type", undefined, "hasLength">;
29
+ export declare const hasSize: ({ exact, max, min, range, bail, }?: {
30
+ exact?: number | null;
31
+ max?: number | null;
32
+ min?: number | null;
33
+ range?: [number, number] | null;
34
+ bail?: boolean;
35
+ }) => Assertion<Map<unknown, unknown> | Set<unknown>, SizeAssertionConstraint[], "size.unsupported-type", undefined, "hasSize">;
36
+ export declare const hasPattern: (pattern: RegExp, { bail, }?: {
37
+ bail?: boolean;
38
+ }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, pattern: RegExp) => boolean, "string.pattern", RegExp]], "string.unsupported-type", undefined, "hasPattern">;
39
+ export declare const startsWith: (prefix: string, { bail, }?: {
40
+ bail?: boolean;
41
+ }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, prefix: string) => boolean, "string.starts-with", string]], "string.unsupported-type", undefined, "startsWith">;
42
+ export declare const endsWith: (suffix: string, { bail, }?: {
43
+ bail?: boolean;
44
+ }) => Assertion<string, readonly [readonly [(value: string) => string, (value: string, suffix: string) => boolean, "string.ends-with", string]], "string.unsupported-type", undefined, "endsWith">;
45
+ export declare const hasValue: ({ exact, max, min, range, bail, }?: {
46
+ exact?: number | null;
47
+ max?: number | null;
48
+ min?: number | null;
49
+ range?: [number, number] | null;
50
+ bail?: boolean;
51
+ }) => Assertion<number, ValueAssertionConstraint[], "number.unsupported-type", undefined, "hasValue">;
52
+ export declare const multipleOf: (step: number, { bail, }?: {
53
+ bail?: boolean;
54
+ }) => Assertion<number, readonly [readonly [(value: number) => number, (value: number, step: number) => boolean, "number.multiple-of", number]], "number.unsupported-type", undefined, "multipleOf">;
55
+ export declare const oneOf: <Actual = unknown>(values: Actual[] | Record<string, Actual>, { equalTo, bail, }?: {
56
+ equalTo?: (a: Actual, b: unknown) => boolean;
57
+ bail?: boolean;
58
+ }) => Assertion<Actual, [], "value.one-of", readonly [Actual[]], "oneOf">;
@@ -1,109 +1,207 @@
1
- import { Or, isArray, isString, isExact, isNumber, isBoolean, isDate, Not, isUndefined, isEmail, isNull, isSymbol } from "./predicates.mjs";
2
- const delegate = (p) => {
3
- return (value) => p(value);
1
+ import { assert } from "./assert.mjs";
2
+ import { isString as isString$1, isArray, isMap as isMap$1, isSet as isSet$1, isNumber as isNumber$1, isBigInt as isBigInt$1, isBlob as isBlob$1, isBoolean as isBoolean$1, isDate as isDate$1, isEmail as isEmail$1, isFile as isFile$1, isFunction as isFunction$1, isNaN as isNaN$1, isNull as isNull$1, isSymbol as isSymbol$1 } from "./predicates.mjs";
3
+ const isEqual = (value, exact) => value === exact;
4
+ const isGte = (value, min) => value >= min;
5
+ const isLte = (value, max) => value <= max;
6
+ const inRange = (value, [min, max]) => value >= min && value <= max;
7
+ const startsWith$1 = (value, prefix) => value.startsWith(prefix);
8
+ const endsWith$1 = (value, suffix) => value.endsWith(suffix);
9
+ const matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
10
+ const isMultipleOf = (value, step) => {
11
+ if (step === 0) {
12
+ return false;
13
+ }
14
+ const quotient = value / step;
15
+ const nearestInteger = Math.round(quotient);
16
+ const delta = Math.abs(quotient - nearestInteger);
17
+ return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
4
18
  };
5
- const Assert = (predicate, options) => {
6
- const extender = (asserts = []) => {
7
- return {
8
- That(...asserts2) {
9
- return Object.assign(delegate(predicate), options, extender(asserts2));
10
- },
11
- get also() {
12
- return asserts;
13
- }
14
- };
15
- };
16
- return Object.assign(delegate(predicate), options, extender());
17
- };
18
- const isGTE = (min) => (x) => isNumber(x) && x >= min;
19
- const isLTE = (max) => (x) => isNumber(x) && x <= max;
20
- const IsEqual = (exact) => Assert((x) => isExact(exact)(x.length), {
21
- fqn: "@modulify/validator/HasLength[exact]",
22
- bail: false,
23
- reason: "exact",
24
- meta: exact
25
- });
26
- const IsGTE = (min) => Assert((x) => isGTE(min)(x.length), {
27
- fqn: "@modulify/validator/HasLength[min]",
28
- bail: false,
29
- reason: "min",
30
- meta: min
31
- });
32
- const IsLTE = (max) => Assert((x) => isLTE(max)(x.length), {
33
- fqn: "@modulify/validator/HasLength[max]",
34
- bail: false,
35
- reason: "max",
36
- meta: max
19
+ const length = (value) => value.length;
20
+ const size = (value) => value.size;
21
+ const isBoolean = assert(isBoolean$1, { name: "isBoolean", bail: true, code: "type.boolean" });
22
+ const isBigInt = assert(isBigInt$1, { name: "isBigInt", bail: true, code: "type.bigint" });
23
+ const isBlob = assert(isBlob$1, { name: "isBlob", bail: true, code: "type.blob" });
24
+ const isDate = assert(isDate$1, { name: "isDate", bail: true, code: "type.date" });
25
+ const isDefined = assert((value) => value !== void 0, {
26
+ name: "isDefined",
27
+ bail: true,
28
+ code: "value.defined"
37
29
  });
38
- const HasLength = ({
30
+ const isEmail = assert(isEmail$1, { name: "isEmail", bail: true, code: "string.email" });
31
+ const isFile = assert(isFile$1, { name: "isFile", bail: true, code: "type.file" });
32
+ const isFunction = assert(isFunction$1, { name: "isFunction", bail: true, code: "type.function" });
33
+ const isMap = assert(isMap$1, { name: "isMap", bail: true, code: "type.map" });
34
+ const isNaN = assert(isNaN$1, { name: "isNaN", bail: true, code: "number.nan" });
35
+ const isNull = assert(isNull$1, { name: "isNull", bail: true, code: "type.null" });
36
+ const isNumber = assert(isNumber$1, { name: "isNumber", bail: true, code: "type.number" });
37
+ const isSet = assert(isSet$1, { name: "isSet", bail: true, code: "type.set" });
38
+ const isString = assert(isString$1, { name: "isString", bail: true, code: "type.string" });
39
+ const isSymbol = assert(isSymbol$1, { name: "isSymbol", bail: true, code: "type.symbol" });
40
+ const hasLength = ({
41
+ exact = null,
42
+ max = null,
43
+ min = null,
44
+ range = null,
45
+ bail = false
46
+ } = {}) => {
47
+ const constraints = [];
48
+ if (exact !== null) constraints.push([length, isEqual, "length.exact", exact]);
49
+ if (max !== null) constraints.push([length, isLte, "length.max", max]);
50
+ if (min !== null) constraints.push([length, isGte, "length.min", min]);
51
+ if (range !== null) constraints.push([length, inRange, "length.range", range]);
52
+ return assert(
53
+ (value) => isArray(value) || isString$1(value),
54
+ {
55
+ name: "hasLength",
56
+ bail,
57
+ code: "length.unsupported-type"
58
+ },
59
+ constraints
60
+ );
61
+ };
62
+ const hasSize = ({
39
63
  exact = null,
40
64
  max = null,
41
65
  min = null,
66
+ range = null,
42
67
  bail = false
43
- }) => Assert(Or(isString, isArray), {
44
- fqn: "@modulify/validator/HasLength",
45
- bail,
46
- reason: "unsupported"
47
- }).That(
48
- ...exact !== null ? [IsEqual(exact)] : [],
49
- ...max !== null ? [IsLTE(max)] : [],
50
- ...min !== null ? [IsGTE(min)] : []
68
+ } = {}) => {
69
+ const constraints = [];
70
+ if (exact !== null) constraints.push([size, isEqual, "size.exact", exact]);
71
+ if (max !== null) constraints.push([size, isLte, "size.max", max]);
72
+ if (min !== null) constraints.push([size, isGte, "size.min", min]);
73
+ if (range !== null) constraints.push([size, inRange, "size.range", range]);
74
+ return assert(
75
+ (value) => isMap$1(value) || isSet$1(value),
76
+ {
77
+ name: "hasSize",
78
+ bail,
79
+ code: "size.unsupported-type"
80
+ },
81
+ constraints
82
+ );
83
+ };
84
+ const hasPattern = (pattern, {
85
+ bail = false
86
+ } = {}) => assert(
87
+ isString$1,
88
+ {
89
+ name: "hasPattern",
90
+ bail,
91
+ code: "string.unsupported-type"
92
+ },
93
+ [[
94
+ (value) => value,
95
+ matchesPattern,
96
+ "string.pattern",
97
+ pattern
98
+ ]]
51
99
  );
52
- const IsBoolean = Assert(isBoolean, {
53
- fqn: "@modulify/validator/IsBoolean",
54
- bail: true
55
- });
56
- const IsDate = Assert(isDate, {
57
- fqn: "@modulify/validator/IsDate",
58
- bail: true
59
- });
60
- const IsDefined = Assert(Not(isUndefined), {
61
- fqn: "@modulify/validator/IsDefined",
62
- bail: true,
63
- reason: "undefined"
64
- });
65
- const IsEmail = Assert(isEmail, {
66
- fqn: "@modulify/validator/IsEmail",
67
- bail: true
68
- });
69
- const IsNull = Assert(isNull, {
70
- fqn: "@modulify/validator/IsNull",
71
- bail: true
72
- });
73
- const IsNumber = Assert(isNumber, {
74
- fqn: "@modulify/validator/IsNumber",
75
- bail: true
76
- });
77
- const IsString = Assert(isString, {
78
- fqn: "@modulify/validator/IsString",
79
- bail: true
80
- });
81
- const IsSymbol = Assert(isSymbol, {
82
- fqn: "@modulify/validator/IsSymbol",
83
- bail: true
84
- });
85
- const OneOf = (values, {
100
+ const startsWith = (prefix, {
101
+ bail = false
102
+ } = {}) => assert(
103
+ isString$1,
104
+ {
105
+ name: "startsWith",
106
+ bail,
107
+ code: "string.unsupported-type"
108
+ },
109
+ [[
110
+ (value) => value,
111
+ startsWith$1,
112
+ "string.starts-with",
113
+ prefix
114
+ ]]
115
+ );
116
+ const endsWith = (suffix, {
117
+ bail = false
118
+ } = {}) => assert(
119
+ isString$1,
120
+ {
121
+ name: "endsWith",
122
+ bail,
123
+ code: "string.unsupported-type"
124
+ },
125
+ [[
126
+ (value) => value,
127
+ endsWith$1,
128
+ "string.ends-with",
129
+ suffix
130
+ ]]
131
+ );
132
+ const hasValue = ({
133
+ exact = null,
134
+ max = null,
135
+ min = null,
136
+ range = null,
137
+ bail = false
138
+ } = {}) => {
139
+ const constraints = [];
140
+ if (exact !== null) constraints.push([(value) => value, isEqual, "number.exact", exact]);
141
+ if (max !== null) constraints.push([(value) => value, isLte, "number.max", max]);
142
+ if (min !== null) constraints.push([(value) => value, isGte, "number.min", min]);
143
+ if (range !== null) constraints.push([(value) => value, inRange, "number.range", range]);
144
+ return assert(
145
+ isNumber$1,
146
+ {
147
+ name: "hasValue",
148
+ bail,
149
+ code: "number.unsupported-type"
150
+ },
151
+ constraints
152
+ );
153
+ };
154
+ const multipleOf = (step, {
155
+ bail = false
156
+ } = {}) => assert(
157
+ isNumber$1,
158
+ {
159
+ name: "multipleOf",
160
+ bail,
161
+ code: "number.unsupported-type"
162
+ },
163
+ [[
164
+ (value) => value,
165
+ isMultipleOf,
166
+ "number.multiple-of",
167
+ step
168
+ ]]
169
+ );
170
+ const oneOf = (values, {
86
171
  equalTo = (a, b) => a === b,
87
172
  bail = false
88
173
  } = {}) => {
89
174
  const haystack = isArray(values) ? values : Object.values(values);
90
- const oneOf = (value) => haystack.some((allowed) => equalTo(allowed, value));
91
- return Assert(oneOf, {
92
- fqn: "@modulify/validator/OneOf",
175
+ return assert((value) => haystack.some((item) => equalTo(item, value)), {
176
+ name: "oneOf",
93
177
  bail,
94
- meta: haystack
178
+ code: "value.one-of",
179
+ args: [haystack]
95
180
  });
96
181
  };
97
182
  export {
98
- Assert,
99
- HasLength,
100
- IsBoolean,
101
- IsDate,
102
- IsDefined,
103
- IsEmail,
104
- IsNull,
105
- IsNumber,
106
- IsString,
107
- IsSymbol,
108
- OneOf
183
+ assert,
184
+ endsWith,
185
+ hasLength,
186
+ hasPattern,
187
+ hasSize,
188
+ hasValue,
189
+ isBigInt,
190
+ isBlob,
191
+ isBoolean,
192
+ isDate,
193
+ isDefined,
194
+ isEmail,
195
+ isFile,
196
+ isFunction,
197
+ isMap,
198
+ isNaN,
199
+ isNull,
200
+ isNumber,
201
+ isSet,
202
+ isString,
203
+ isSymbol,
204
+ multipleOf,
205
+ oneOf,
206
+ startsWith
109
207
  };
@@ -0,0 +1,8 @@
1
+ export declare const isEqual: (value: number, exact: number) => boolean;
2
+ export declare const isGte: (value: number, min: number) => boolean;
3
+ export declare const isLte: (value: number, max: number) => boolean;
4
+ export declare const inRange: (value: number, [min, max]: [number, number]) => boolean;
5
+ export declare const startsWith: (value: string, prefix: string) => boolean;
6
+ export declare const endsWith: (value: string, suffix: string) => boolean;
7
+ export declare const matchesPattern: (value: string, pattern: RegExp) => boolean;
8
+ export declare const isMultipleOf: (value: number, step: number) => boolean;