umt 2.20.0 → 3.0.0
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/module/Validate/array/arrayOf.d.ts +9 -6
- package/module/Validate/array/arrayOf.js +2 -3
- package/module/Validate/array/arrayOf.js.map +1 -1
- package/module/Validate/array/index.d.ts +0 -1
- package/module/Validate/array/index.js +0 -1
- package/module/Validate/array/index.js.map +1 -1
- package/module/Validate/object/intersection.d.ts +5 -2
- package/module/Validate/object/intersection.js.map +1 -1
- package/module/Validate/string/oneOf.d.ts +10 -7
- package/module/Validate/string/oneOf.js +5 -3
- package/module/Validate/string/oneOf.js.map +1 -1
- package/module/Validate/type.d.ts +14 -2
- package/module/es5/Validate/array/arrayOf.d.ts +9 -6
- package/module/es5/Validate/array/arrayOf.js +10 -5
- package/module/es5/Validate/array/index.d.ts +0 -1
- package/module/es5/Validate/array/index.js +0 -11
- package/module/es5/Validate/object/intersection.d.ts +5 -2
- package/module/es5/Validate/object/intersection.js +6 -0
- package/module/es5/Validate/string/oneOf.d.ts +10 -7
- package/module/es5/Validate/string/oneOf.js +8 -6
- package/module/es5/Validate/type.d.ts +14 -2
- package/package.json +1 -1
- package/module/Validate/array/core.d.ts +0 -14
- package/module/Validate/array/core.js +0 -43
- package/module/Validate/array/core.js.map +0 -1
- package/module/es5/Validate/array/core.d.ts +0 -14
- package/module/es5/Validate/array/core.js +0 -62
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import type { ValidateCoreReturnType } from "../../Validate/type";
|
|
1
|
+
import type { ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
2
|
+
type ExtractValidatedType<V> = V extends (value: never) => {
|
|
3
|
+
type: infer T;
|
|
4
|
+
} ? ValidateType<T> : never;
|
|
2
5
|
/**
|
|
3
6
|
* Creates an array validator that validates every element with a single validator
|
|
4
|
-
* @template
|
|
5
|
-
* @
|
|
6
|
-
* @param {Function} validator - Validator applied to each element (e.g. an object validator)
|
|
7
|
+
* @template V - The validator function applied to each element (its return `type` field carries the element type)
|
|
8
|
+
* @param {V} validator - Validator applied to each element (e.g. an object validator)
|
|
7
9
|
* @param {string} [message] - Custom error message for array type validation
|
|
8
10
|
* @returns {Function} - Validator function for arrays whose elements satisfy the given validator
|
|
9
11
|
*/
|
|
10
|
-
export declare const arrayOf: <
|
|
12
|
+
export declare const arrayOf: <V extends (value: never) => {
|
|
11
13
|
type: unknown;
|
|
12
14
|
message: string;
|
|
13
15
|
validate: boolean;
|
|
14
|
-
}>(validator:
|
|
16
|
+
}>(validator: V, message?: string) => (values: ExtractValidatedType<V>[]) => ValidateCoreReturnType<ExtractValidatedType<V>[]>;
|
|
17
|
+
export {};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { isArray } from "../../Validate/isArray";
|
|
2
2
|
/**
|
|
3
3
|
* Creates an array validator that validates every element with a single validator
|
|
4
|
-
* @template
|
|
5
|
-
* @
|
|
6
|
-
* @param {Function} validator - Validator applied to each element (e.g. an object validator)
|
|
4
|
+
* @template V - The validator function applied to each element (its return `type` field carries the element type)
|
|
5
|
+
* @param {V} validator - Validator applied to each element (e.g. an object validator)
|
|
7
6
|
* @param {string} [message] - Custom error message for array type validation
|
|
8
7
|
* @returns {Function} - Validator function for arrays whose elements satisfy the given validator
|
|
9
8
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arrayOf.js","sourceRoot":"","sources":["../../../src/Validate/array/arrayOf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"arrayOf.js","sourceRoot":"","sources":["../../../src/Validate/array/arrayOf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAY7C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAOrB,SAAY,EACZ,OAAgB,EAChB,EAAE;IAEF,OAAO,CAAC,MAAiB,EAAqC,EAAE;QAC9D,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACrB,OAAO;gBACL,QAAQ,EAAE,KAAK;gBACf,OAAO,EAAE,OAAO,IAAI,EAAE;gBACtB,IAAI,EAAE,MAAM;aACb,CAAC;QACJ,CAAC;QACD,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,MAAM,GACV,SAID,CAAC,KAAK,CAAC,CAAC;YACT,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;gBACrB,OAAO;oBACL,QAAQ,EAAE,KAAK;oBACf,OAAO,EAAE,MAAM,CAAC,OAAO;oBACvB,IAAI,EAAE,MAAM;iBACb,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,OAAO,EAAE,EAAE;YACX,IAAI,EAAE,MAAM;SACb,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Validate/array/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/Validate/array/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import type { UnionToIntersection } from "../../types/logic";
|
|
2
1
|
import type { ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
3
2
|
type ExtractValidatedType<V> = V extends (value: never) => {
|
|
4
3
|
type: infer T;
|
|
5
4
|
} ? ValidateType<T> : never;
|
|
5
|
+
type IntersectValidatedTypes<Vs> = Vs extends readonly [
|
|
6
|
+
infer Head,
|
|
7
|
+
...infer Tail
|
|
8
|
+
] ? ExtractValidatedType<Head> & IntersectValidatedTypes<Tail> : unknown;
|
|
6
9
|
/**
|
|
7
10
|
* Creates an intersection validator that passes only if all given validators pass
|
|
8
11
|
* @param validators - Validator functions to compose as an intersection (logical AND)
|
|
9
12
|
* @returns {Function} - Validator that checks if the value matches all validators
|
|
10
13
|
*/
|
|
11
|
-
export declare const intersection: <Vs extends ((value: never) => ValidateCoreReturnType<unknown>)[]>(...validators: [...Vs]) => (value:
|
|
14
|
+
export declare const intersection: <Vs extends ((value: never) => ValidateCoreReturnType<unknown>)[]>(...validators: [...Vs]) => (value: IntersectValidatedTypes<Vs>) => ValidateCoreReturnType<IntersectValidatedTypes<Vs>>;
|
|
12
15
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../../src/Validate/object/intersection.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../../src/Validate/object/intersection.ts"],"names":[],"mappings":"AA2BA;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAG1B,GAAG,UAAmB,EACtB,EAAE;IACF,OAAO,CACL,KAAkC,EACmB,EAAE;QACvD,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACnC,MAAM,MAAM,GACV,SAGD,CAAC,KAAK,CAAC,CAAC;YACT,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;gBACrB,OAAO;oBACL,QAAQ,EAAE,KAAK;oBACf,OAAO,EAAE,MAAM,CAAC,OAAO;oBACvB,IAAI,EAAE,KAAsD;iBAC7D,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,OAAO,EAAE,EAAE;YACX,IAAI,EAAE,KAAsD;SAC7D,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -5,20 +5,23 @@
|
|
|
5
5
|
* such as `'standard' | 'squat' | 'decanter' | 'round' | 'tall' | 'flask'`.
|
|
6
6
|
*
|
|
7
7
|
* The validator's return type carries the literal union through its `type`
|
|
8
|
-
* field
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* field as a `LiteralBrand<T>` so that consumers like `object()`, `union()`,
|
|
9
|
+
* and `intersection()` can preserve the literal union in their inferred types.
|
|
10
|
+
* The brand also prevents collisions with reserved type tags such as
|
|
11
|
+
* `"string"`, `"number"`, or `"boolean"` when an allowed value happens to
|
|
12
|
+
* match one of those tag strings.
|
|
11
13
|
*/
|
|
14
|
+
import type { LiteralBrand } from "../../Validate/type";
|
|
12
15
|
/**
|
|
13
16
|
* Return type produced by a `oneOf` validator. Structurally compatible with
|
|
14
|
-
* `ValidateCoreReturnType<unknown>`, but exposes the literal union
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
+
* `ValidateCoreReturnType<unknown>`, but exposes the literal union via a
|
|
18
|
+
* `LiteralBrand` so the inferred type can flow through `object()`, `union()`,
|
|
19
|
+
* and `intersection()` without being collapsed by `ValidateType`.
|
|
17
20
|
*/
|
|
18
21
|
export interface OneOfReturnType<T extends string> {
|
|
19
22
|
validate: boolean;
|
|
20
23
|
message: string;
|
|
21
|
-
type: T
|
|
24
|
+
type: LiteralBrand<T>;
|
|
22
25
|
}
|
|
23
26
|
/**
|
|
24
27
|
* Creates a top-level validator that checks if a string value is one of the
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* such as `'standard' | 'squat' | 'decanter' | 'round' | 'tall' | 'flask'`.
|
|
6
6
|
*
|
|
7
7
|
* The validator's return type carries the literal union through its `type`
|
|
8
|
-
* field
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* field as a `LiteralBrand<T>` so that consumers like `object()`, `union()`,
|
|
9
|
+
* and `intersection()` can preserve the literal union in their inferred types.
|
|
10
|
+
* The brand also prevents collisions with reserved type tags such as
|
|
11
|
+
* `"string"`, `"number"`, or `"boolean"` when an allowed value happens to
|
|
12
|
+
* match one of those tag strings.
|
|
11
13
|
*/
|
|
12
14
|
/**
|
|
13
15
|
* Creates a top-level validator that checks if a string value is one of the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oneOf.js","sourceRoot":"","sources":["../../../src/Validate/string/oneOf.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"oneOf.js","sourceRoot":"","sources":["../../../src/Validate/string/oneOf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAgBH;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CACnB,MAAS,EACT,OAAgB,EAChB,EAAE;IACF,MAAM,OAAO,GAAG,IAAI,GAAG,CAAS,MAAM,CAAC,CAAC;IACxC,OAAO,CAAC,KAAa,EAA8B,EAAE;QACnD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACnC,OAAO;YACL,QAAQ,EAAE,OAAO;YACjB,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC;YACvC,IAAI,EAAE,KAAgC;SACvC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -30,16 +30,28 @@ export interface ValidateReturnType<T> {
|
|
|
30
30
|
* @template T - The type of value to validate
|
|
31
31
|
*/
|
|
32
32
|
export type ValidateFunctionType<T> = (value: T) => boolean;
|
|
33
|
+
declare const LITERAL_BRAND: unique symbol;
|
|
34
|
+
/**
|
|
35
|
+
* Branded literal type used by validators (such as `oneOf`) that need to keep
|
|
36
|
+
* a string literal verbatim through `ValidateType`. The brand prevents the
|
|
37
|
+
* literal from colliding with reserved type tags like `"string"`, `"number"`,
|
|
38
|
+
* or `"boolean"` when the same string also happens to name a primitive type.
|
|
39
|
+
* @template T - The literal value preserved by the brand
|
|
40
|
+
*/
|
|
41
|
+
export type LiteralBrand<T extends string> = T & {
|
|
42
|
+
readonly [LITERAL_BRAND]: T;
|
|
43
|
+
};
|
|
33
44
|
export type _ValidateType<T> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : T;
|
|
34
45
|
export type _ValidateType2<T> = T extends "undefined" ? undefined : T extends "null" ? null : T;
|
|
35
46
|
export type _ValidateType3<T> = T extends "bigint" ? bigint : T extends "any" ? any : T extends "unknown" ? unknown : T extends "never" ? never : T;
|
|
36
47
|
/**
|
|
37
48
|
* Maps string literal type names back to their TypeScript types
|
|
38
49
|
* @template T - The string literal type name ("string", "number", "boolean", "bigint", "undefined", "null", "any", "unknown", "never")
|
|
39
|
-
* @returns The corresponding TypeScript type or the original type T when no tag matches
|
|
50
|
+
* @returns The corresponding TypeScript type, the unwrapped literal when `T` is a `LiteralBrand`, or the original type `T` when no tag matches
|
|
40
51
|
*/
|
|
41
|
-
export type ValidateType<T> = T extends "string" | "number" | "boolean" ? _ValidateType<T> : T extends "undefined" | "null" ? _ValidateType2<T> : T extends "bigint" | "any" | "unknown" | "never" ? _ValidateType3<T> : T;
|
|
52
|
+
export type ValidateType<T> = T extends LiteralBrand<infer L> ? L : T extends "string" | "number" | "boolean" ? _ValidateType<T> : T extends "undefined" | "null" ? _ValidateType2<T> : T extends "bigint" | "any" | "unknown" | "never" ? _ValidateType3<T> : T;
|
|
42
53
|
export type SchemaToInterface<T extends (value: any) => ValidateCoreReturnType<any>> = ValidateType<ReturnType<T>["type"]>;
|
|
43
54
|
export type OptionalKeys<T> = {
|
|
44
55
|
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
45
56
|
}[keyof T];
|
|
57
|
+
export {};
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import type { ValidateCoreReturnType } from "../../Validate/type";
|
|
1
|
+
import type { ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
2
|
+
type ExtractValidatedType<V> = V extends (value: never) => {
|
|
3
|
+
type: infer T;
|
|
4
|
+
} ? ValidateType<T> : never;
|
|
2
5
|
/**
|
|
3
6
|
* Creates an array validator that validates every element with a single validator
|
|
4
|
-
* @template
|
|
5
|
-
* @
|
|
6
|
-
* @param {Function} validator - Validator applied to each element (e.g. an object validator)
|
|
7
|
+
* @template V - The validator function applied to each element (its return `type` field carries the element type)
|
|
8
|
+
* @param {V} validator - Validator applied to each element (e.g. an object validator)
|
|
7
9
|
* @param {string} [message] - Custom error message for array type validation
|
|
8
10
|
* @returns {Function} - Validator function for arrays whose elements satisfy the given validator
|
|
9
11
|
*/
|
|
10
|
-
export declare const arrayOf: <
|
|
12
|
+
export declare const arrayOf: <V extends (value: never) => {
|
|
11
13
|
type: unknown;
|
|
12
14
|
message: string;
|
|
13
15
|
validate: boolean;
|
|
14
|
-
}>(validator:
|
|
16
|
+
}>(validator: V, message?: string) => (values: ExtractValidatedType<V>[]) => ValidateCoreReturnType<ExtractValidatedType<V>[]>;
|
|
17
|
+
export {};
|
|
@@ -8,11 +8,16 @@ var _isArray = require("../../Validate/isArray");
|
|
|
8
8
|
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
9
9
|
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
10
10
|
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
11
|
+
// Extract the validated value type by reading the validator's `type` field
|
|
12
|
+
// (and applying `ValidateType` to map type tags like "string" back to the
|
|
13
|
+
// runtime type). Reading the field directly lets validators that expose the
|
|
14
|
+
// literal union via the `type` field (such as `oneOf`) flow through arrayOf
|
|
15
|
+
// without being collapsed to `string`.
|
|
16
|
+
|
|
11
17
|
/**
|
|
12
18
|
* Creates an array validator that validates every element with a single validator
|
|
13
|
-
* @template
|
|
14
|
-
* @
|
|
15
|
-
* @param {Function} validator - Validator applied to each element (e.g. an object validator)
|
|
19
|
+
* @template V - The validator function applied to each element (its return `type` field carries the element type)
|
|
20
|
+
* @param {V} validator - Validator applied to each element (e.g. an object validator)
|
|
16
21
|
* @param {string} [message] - Custom error message for array type validation
|
|
17
22
|
* @returns {Function} - Validator function for arrays whose elements satisfy the given validator
|
|
18
23
|
*/
|
|
@@ -29,8 +34,8 @@ var arrayOf = exports.arrayOf = function arrayOf(validator, message) {
|
|
|
29
34
|
_step;
|
|
30
35
|
try {
|
|
31
36
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
32
|
-
var
|
|
33
|
-
var result = validator(
|
|
37
|
+
var _value = _step.value;
|
|
38
|
+
var result = validator(_value);
|
|
34
39
|
if (!result.validate) {
|
|
35
40
|
return {
|
|
36
41
|
validate: false,
|
|
@@ -13,15 +13,4 @@ Object.keys(_arrayOf).forEach(function (key) {
|
|
|
13
13
|
return _arrayOf[key];
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
|
-
});
|
|
17
|
-
var _core = require("./core");
|
|
18
|
-
Object.keys(_core).forEach(function (key) {
|
|
19
|
-
if (key === "default" || key === "__esModule") return;
|
|
20
|
-
if (key in exports && exports[key] === _core[key]) return;
|
|
21
|
-
Object.defineProperty(exports, key, {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function get() {
|
|
24
|
-
return _core[key];
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
16
|
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import type { UnionToIntersection } from "../../types/logic";
|
|
2
1
|
import type { ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
3
2
|
type ExtractValidatedType<V> = V extends (value: never) => {
|
|
4
3
|
type: infer T;
|
|
5
4
|
} ? ValidateType<T> : never;
|
|
5
|
+
type IntersectValidatedTypes<Vs> = Vs extends readonly [
|
|
6
|
+
infer Head,
|
|
7
|
+
...infer Tail
|
|
8
|
+
] ? ExtractValidatedType<Head> & IntersectValidatedTypes<Tail> : unknown;
|
|
6
9
|
/**
|
|
7
10
|
* Creates an intersection validator that passes only if all given validators pass
|
|
8
11
|
* @param validators - Validator functions to compose as an intersection (logical AND)
|
|
9
12
|
* @returns {Function} - Validator that checks if the value matches all validators
|
|
10
13
|
*/
|
|
11
|
-
export declare const intersection: <Vs extends ((value: never) => ValidateCoreReturnType<unknown>)[]>(...validators: [...Vs]) => (value:
|
|
14
|
+
export declare const intersection: <Vs extends ((value: never) => ValidateCoreReturnType<unknown>)[]>(...validators: [...Vs]) => (value: IntersectValidatedTypes<Vs>) => ValidateCoreReturnType<IntersectValidatedTypes<Vs>>;
|
|
12
15
|
export {};
|
|
@@ -10,6 +10,12 @@ exports.intersection = void 0;
|
|
|
10
10
|
// literal union via the `type` field (such as `oneOf`) flow through
|
|
11
11
|
// intersection without being collapsed by `Types<T>`.
|
|
12
12
|
|
|
13
|
+
// Walk the validator tuple and `&`-combine each element's extracted type.
|
|
14
|
+
// Reducing element-by-element preserves any union exposed by an individual
|
|
15
|
+
// validator (e.g. `union(A, B)`), unlike `UnionToIntersection` over
|
|
16
|
+
// `Vs[number]` which would distribute over the inner union and collapse
|
|
17
|
+
// `(A | B) & C` into `A & B & C`.
|
|
18
|
+
|
|
13
19
|
/**
|
|
14
20
|
* Creates an intersection validator that passes only if all given validators pass
|
|
15
21
|
* @param validators - Validator functions to compose as an intersection (logical AND)
|
|
@@ -5,20 +5,23 @@
|
|
|
5
5
|
* such as `'standard' | 'squat' | 'decanter' | 'round' | 'tall' | 'flask'`.
|
|
6
6
|
*
|
|
7
7
|
* The validator's return type carries the literal union through its `type`
|
|
8
|
-
* field
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* field as a `LiteralBrand<T>` so that consumers like `object()`, `union()`,
|
|
9
|
+
* and `intersection()` can preserve the literal union in their inferred types.
|
|
10
|
+
* The brand also prevents collisions with reserved type tags such as
|
|
11
|
+
* `"string"`, `"number"`, or `"boolean"` when an allowed value happens to
|
|
12
|
+
* match one of those tag strings.
|
|
11
13
|
*/
|
|
14
|
+
import type { LiteralBrand } from "../../Validate/type";
|
|
12
15
|
/**
|
|
13
16
|
* Return type produced by a `oneOf` validator. Structurally compatible with
|
|
14
|
-
* `ValidateCoreReturnType<unknown>`, but exposes the literal union
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
+
* `ValidateCoreReturnType<unknown>`, but exposes the literal union via a
|
|
18
|
+
* `LiteralBrand` so the inferred type can flow through `object()`, `union()`,
|
|
19
|
+
* and `intersection()` without being collapsed by `ValidateType`.
|
|
17
20
|
*/
|
|
18
21
|
export interface OneOfReturnType<T extends string> {
|
|
19
22
|
validate: boolean;
|
|
20
23
|
message: string;
|
|
21
|
-
type: T
|
|
24
|
+
type: LiteralBrand<T>;
|
|
22
25
|
}
|
|
23
26
|
/**
|
|
24
27
|
* Creates a top-level validator that checks if a string value is one of the
|
|
@@ -11,16 +11,18 @@ exports.oneOf = void 0;
|
|
|
11
11
|
* such as `'standard' | 'squat' | 'decanter' | 'round' | 'tall' | 'flask'`.
|
|
12
12
|
*
|
|
13
13
|
* The validator's return type carries the literal union through its `type`
|
|
14
|
-
* field
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* field as a `LiteralBrand<T>` so that consumers like `object()`, `union()`,
|
|
15
|
+
* and `intersection()` can preserve the literal union in their inferred types.
|
|
16
|
+
* The brand also prevents collisions with reserved type tags such as
|
|
17
|
+
* `"string"`, `"number"`, or `"boolean"` when an allowed value happens to
|
|
18
|
+
* match one of those tag strings.
|
|
17
19
|
*/
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* Return type produced by a `oneOf` validator. Structurally compatible with
|
|
21
|
-
* `ValidateCoreReturnType<unknown>`, but exposes the literal union
|
|
22
|
-
*
|
|
23
|
-
*
|
|
23
|
+
* `ValidateCoreReturnType<unknown>`, but exposes the literal union via a
|
|
24
|
+
* `LiteralBrand` so the inferred type can flow through `object()`, `union()`,
|
|
25
|
+
* and `intersection()` without being collapsed by `ValidateType`.
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
28
|
/**
|
|
@@ -30,16 +30,28 @@ export interface ValidateReturnType<T> {
|
|
|
30
30
|
* @template T - The type of value to validate
|
|
31
31
|
*/
|
|
32
32
|
export type ValidateFunctionType<T> = (value: T) => boolean;
|
|
33
|
+
declare const LITERAL_BRAND: unique symbol;
|
|
34
|
+
/**
|
|
35
|
+
* Branded literal type used by validators (such as `oneOf`) that need to keep
|
|
36
|
+
* a string literal verbatim through `ValidateType`. The brand prevents the
|
|
37
|
+
* literal from colliding with reserved type tags like `"string"`, `"number"`,
|
|
38
|
+
* or `"boolean"` when the same string also happens to name a primitive type.
|
|
39
|
+
* @template T - The literal value preserved by the brand
|
|
40
|
+
*/
|
|
41
|
+
export type LiteralBrand<T extends string> = T & {
|
|
42
|
+
readonly [LITERAL_BRAND]: T;
|
|
43
|
+
};
|
|
33
44
|
export type _ValidateType<T> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : T;
|
|
34
45
|
export type _ValidateType2<T> = T extends "undefined" ? undefined : T extends "null" ? null : T;
|
|
35
46
|
export type _ValidateType3<T> = T extends "bigint" ? bigint : T extends "any" ? any : T extends "unknown" ? unknown : T extends "never" ? never : T;
|
|
36
47
|
/**
|
|
37
48
|
* Maps string literal type names back to their TypeScript types
|
|
38
49
|
* @template T - The string literal type name ("string", "number", "boolean", "bigint", "undefined", "null", "any", "unknown", "never")
|
|
39
|
-
* @returns The corresponding TypeScript type or the original type T when no tag matches
|
|
50
|
+
* @returns The corresponding TypeScript type, the unwrapped literal when `T` is a `LiteralBrand`, or the original type `T` when no tag matches
|
|
40
51
|
*/
|
|
41
|
-
export type ValidateType<T> = T extends "string" | "number" | "boolean" ? _ValidateType<T> : T extends "undefined" | "null" ? _ValidateType2<T> : T extends "bigint" | "any" | "unknown" | "never" ? _ValidateType3<T> : T;
|
|
52
|
+
export type ValidateType<T> = T extends LiteralBrand<infer L> ? L : T extends "string" | "number" | "boolean" ? _ValidateType<T> : T extends "undefined" | "null" ? _ValidateType2<T> : T extends "bigint" | "any" | "unknown" | "never" ? _ValidateType3<T> : T;
|
|
42
53
|
export type SchemaToInterface<T extends (value: any) => ValidateCoreReturnType<any>> = ValidateType<ReturnType<T>["type"]>;
|
|
43
54
|
export type OptionalKeys<T> = {
|
|
44
55
|
[K in keyof T]: undefined extends T[K] ? K : never;
|
|
45
56
|
}[keyof T];
|
|
57
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Array validation core module
|
|
3
|
-
* Provides functionality to validate arrays with type-specific validation rules
|
|
4
|
-
*/
|
|
5
|
-
import type { Types, ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
6
|
-
/**
|
|
7
|
-
* Creates an array validator with type-specific validation rules
|
|
8
|
-
* @template A - Type of array elements (string | number | boolean)
|
|
9
|
-
* @template O - Object containing validation functions for each type
|
|
10
|
-
* @param {O} option - Validation functions for each type
|
|
11
|
-
* @param {string} [message] - Custom error message for array type validation
|
|
12
|
-
* @returns {Function} - Validator function for arrays
|
|
13
|
-
*/
|
|
14
|
-
export declare const array: <A extends string | number | boolean, O extends { [P in Types<A>]: (value: ValidateType<P>) => ValidateCoreReturnType<ValidateType<P>>; } = { [P in Types<A>]: (value: ValidateType<P>) => ValidateCoreReturnType<ValidateType<P>>; }>(option?: O, message?: string) => (values: A[]) => ValidateCoreReturnType<A[]>;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Array validation core module
|
|
3
|
-
* Provides functionality to validate arrays with type-specific validation rules
|
|
4
|
-
*/
|
|
5
|
-
import { isArray } from "../../Validate/isArray";
|
|
6
|
-
import { isNotEmpty } from "../../Validate/isNotEmpty";
|
|
7
|
-
/**
|
|
8
|
-
* Creates an array validator with type-specific validation rules
|
|
9
|
-
* @template A - Type of array elements (string | number | boolean)
|
|
10
|
-
* @template O - Object containing validation functions for each type
|
|
11
|
-
* @param {O} option - Validation functions for each type
|
|
12
|
-
* @param {string} [message] - Custom error message for array type validation
|
|
13
|
-
* @returns {Function} - Validator function for arrays
|
|
14
|
-
*/
|
|
15
|
-
export const array = (option = {}, message) => {
|
|
16
|
-
return (values) => {
|
|
17
|
-
if (!isArray(values)) {
|
|
18
|
-
return {
|
|
19
|
-
validate: false,
|
|
20
|
-
message: message ?? "",
|
|
21
|
-
type: values,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
if (isNotEmpty(option)) {
|
|
25
|
-
for (const value of values) {
|
|
26
|
-
const validator = option[typeof value];
|
|
27
|
-
if (!validator?.(value).validate) {
|
|
28
|
-
return {
|
|
29
|
-
validate: false,
|
|
30
|
-
message: validator?.(value).message || "",
|
|
31
|
-
type: values,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
validate: true,
|
|
38
|
-
message: "",
|
|
39
|
-
type: values,
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
};
|
|
43
|
-
//# sourceMappingURL=core.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../../src/Validate/array/core.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAOnD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAYnB,SAAY,EAAO,EACnB,OAAgB,EAChB,EAAE;IACF,OAAO,CAAC,MAAW,EAA+B,EAAE;QAClD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;YACrB,OAAO;gBACL,QAAQ,EAAE,KAAK;gBACf,OAAO,EAAE,OAAO,IAAI,EAAE;gBACtB,IAAI,EAAE,MAAM;aACb,CAAC;QACJ,CAAC;QACD,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACvB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBAC3B,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,KAAiB,CAAC,CAAC;gBACnD,IAAI,CAAC,SAAS,EAAE,CAAC,KAAc,CAAC,CAAC,QAAQ,EAAE,CAAC;oBAC1C,OAAO;wBACL,QAAQ,EAAE,KAAK;wBACf,OAAO,EAAE,SAAS,EAAE,CAAC,KAAc,CAAC,CAAC,OAAO,IAAI,EAAE;wBAClD,IAAI,EAAE,MAAM;qBACb,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,OAAO,EAAE,EAAE;YACX,IAAI,EAAE,MAAM;SACb,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Array validation core module
|
|
3
|
-
* Provides functionality to validate arrays with type-specific validation rules
|
|
4
|
-
*/
|
|
5
|
-
import type { Types, ValidateCoreReturnType, ValidateType } from "../../Validate/type";
|
|
6
|
-
/**
|
|
7
|
-
* Creates an array validator with type-specific validation rules
|
|
8
|
-
* @template A - Type of array elements (string | number | boolean)
|
|
9
|
-
* @template O - Object containing validation functions for each type
|
|
10
|
-
* @param {O} option - Validation functions for each type
|
|
11
|
-
* @param {string} [message] - Custom error message for array type validation
|
|
12
|
-
* @returns {Function} - Validator function for arrays
|
|
13
|
-
*/
|
|
14
|
-
export declare const array: <A extends string | number | boolean, O extends { [P in Types<A>]: (value: ValidateType<P>) => ValidateCoreReturnType<ValidateType<P>>; } = { [P in Types<A>]: (value: ValidateType<P>) => ValidateCoreReturnType<ValidateType<P>>; }>(option?: O, message?: string) => (values: A[]) => ValidateCoreReturnType<A[]>;
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.array = void 0;
|
|
7
|
-
var _isArray = require("../../Validate/isArray");
|
|
8
|
-
var _isNotEmpty = require("../../Validate/isNotEmpty");
|
|
9
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
10
|
-
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
11
|
-
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
12
|
-
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /**
|
|
13
|
-
* Array validation core module
|
|
14
|
-
* Provides functionality to validate arrays with type-specific validation rules
|
|
15
|
-
*/
|
|
16
|
-
/**
|
|
17
|
-
* Creates an array validator with type-specific validation rules
|
|
18
|
-
* @template A - Type of array elements (string | number | boolean)
|
|
19
|
-
* @template O - Object containing validation functions for each type
|
|
20
|
-
* @param {O} option - Validation functions for each type
|
|
21
|
-
* @param {string} [message] - Custom error message for array type validation
|
|
22
|
-
* @returns {Function} - Validator function for arrays
|
|
23
|
-
*/
|
|
24
|
-
var array = exports.array = function array() {
|
|
25
|
-
var option = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
26
|
-
var message = arguments.length > 1 ? arguments[1] : undefined;
|
|
27
|
-
return function (values) {
|
|
28
|
-
if (!(0, _isArray.isArray)(values)) {
|
|
29
|
-
return {
|
|
30
|
-
validate: false,
|
|
31
|
-
message: message !== null && message !== void 0 ? message : "",
|
|
32
|
-
type: values
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
if ((0, _isNotEmpty.isNotEmpty)(option)) {
|
|
36
|
-
var _iterator = _createForOfIteratorHelper(values),
|
|
37
|
-
_step;
|
|
38
|
-
try {
|
|
39
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
40
|
-
var _value = _step.value;
|
|
41
|
-
var validator = option[_typeof(_value)];
|
|
42
|
-
if (!(validator !== null && validator !== void 0 && validator(_value).validate)) {
|
|
43
|
-
return {
|
|
44
|
-
validate: false,
|
|
45
|
-
message: (validator === null || validator === void 0 ? void 0 : validator(_value).message) || "",
|
|
46
|
-
type: values
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
} catch (err) {
|
|
51
|
-
_iterator.e(err);
|
|
52
|
-
} finally {
|
|
53
|
-
_iterator.f();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
validate: true,
|
|
58
|
-
message: "",
|
|
59
|
-
type: values
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
};
|