@zipbul/baker 2.2.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/CHANGELOG.md +256 -0
- package/MIGRATION-3.0.md +104 -0
- package/README.md +109 -63
- package/dist/index.d.ts +7 -6
- package/dist/index.js +10 -321
- package/dist/src/collect.d.ts +13 -10
- package/dist/src/collect.js +26 -0
- package/dist/src/configure.d.ts +8 -6
- package/dist/src/configure.js +43 -0
- package/dist/src/create-rule.js +41 -0
- package/dist/src/decorators/field.d.ts +22 -18
- package/dist/src/decorators/field.js +268 -0
- package/dist/src/decorators/index.d.ts +1 -0
- package/dist/src/decorators/index.js +2 -2
- package/dist/src/decorators/recipe.d.ts +17 -0
- package/dist/src/decorators/recipe.js +23 -0
- package/dist/src/errors.d.ts +27 -17
- package/dist/src/errors.js +52 -0
- package/dist/src/functions/check-call-options.d.ts +8 -0
- package/dist/src/functions/check-call-options.js +51 -0
- package/dist/src/functions/deserialize.d.ts +13 -6
- package/dist/src/functions/deserialize.js +57 -0
- package/dist/src/functions/serialize.d.ts +10 -4
- package/dist/src/functions/serialize.js +52 -0
- package/dist/src/functions/validate.d.ts +13 -10
- package/dist/src/functions/validate.js +49 -0
- package/dist/src/interfaces.d.ts +1 -1
- package/dist/src/interfaces.js +4 -0
- package/dist/src/meta-access.d.ts +19 -0
- package/dist/src/meta-access.js +75 -0
- package/dist/src/registry.js +8 -0
- package/dist/src/rule-metadata.d.ts +11 -0
- package/dist/src/rule-metadata.js +17 -0
- package/dist/src/rule-plan.d.ts +10 -11
- package/dist/src/rule-plan.js +117 -0
- package/dist/src/rules/array.d.ts +7 -6
- package/dist/src/rules/array.js +96 -0
- package/dist/src/rules/common.js +77 -0
- package/dist/src/rules/date.js +35 -0
- package/dist/src/rules/index.d.ts +2 -4
- package/dist/src/rules/index.js +8 -21
- package/dist/src/rules/locales.d.ts +5 -4
- package/dist/src/rules/locales.js +249 -0
- package/dist/src/rules/number.js +79 -0
- package/dist/src/rules/object.d.ts +1 -1
- package/dist/src/rules/object.js +49 -0
- package/dist/src/rules/string.d.ts +83 -80
- package/dist/src/rules/string.js +1998 -0
- package/dist/src/rules/typechecker.js +143 -0
- package/dist/src/seal/circular-analyzer.js +63 -0
- package/dist/src/seal/codegen-utils.js +18 -0
- package/dist/src/seal/deserialize-builder.d.ts +8 -4
- package/dist/src/seal/deserialize-builder.js +1546 -0
- package/dist/src/seal/expose-validator.d.ts +3 -2
- package/dist/src/seal/expose-validator.js +65 -0
- package/dist/src/seal/seal-state.d.ts +10 -0
- package/dist/src/seal/seal-state.js +18 -0
- package/dist/src/seal/seal.d.ts +22 -21
- package/dist/src/seal/seal.js +431 -0
- package/dist/src/seal/serialize-builder.d.ts +3 -2
- package/dist/src/seal/serialize-builder.js +374 -0
- package/dist/src/seal/validate-meta.d.ts +13 -0
- package/dist/src/seal/validate-meta.js +61 -0
- package/dist/src/symbols.d.ts +1 -1
- package/dist/src/symbols.js +13 -2
- package/dist/src/transformers/collection.transformer.js +25 -0
- package/dist/src/transformers/date.transformer.js +18 -0
- package/dist/src/transformers/index.js +6 -2
- package/dist/src/transformers/luxon.transformer.d.ts +4 -2
- package/dist/src/transformers/luxon.transformer.js +34 -0
- package/dist/src/transformers/moment.transformer.d.ts +4 -2
- package/dist/src/transformers/moment.transformer.js +32 -0
- package/dist/src/transformers/number.transformer.js +8 -0
- package/dist/src/transformers/string.transformer.js +12 -0
- package/dist/src/types.d.ts +27 -25
- package/dist/src/types.js +1 -0
- package/dist/src/utils.d.ts +2 -2
- package/dist/src/utils.js +10 -0
- package/package.json +80 -67
- package/dist/index-03cysbck.js +0 -3
- package/dist/index-dcbd798a.js +0 -3
- package/dist/index-jp2yjd6g.js +0 -3
- package/dist/index-mw7met6r.js +0 -3
- package/dist/index-xdn55cz3.js +0 -1
- package/dist/src/functions/_run-sealed.d.ts +0 -7
- package/dist/src/functions/index.d.ts +0 -3
- package/dist/src/seal/index.d.ts +0 -5
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { BakerError } from '../errors.js';
|
|
2
|
+
import { makePlannedRule, makeRule, planCompare, planLiteral, planOr, planValue } from '../rule-plan.js';
|
|
3
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
4
|
+
// min — v >= n check. requiresType='number' (§4.7, §4.8 A)
|
|
5
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
6
|
+
export function min(n, opts) {
|
|
7
|
+
if (!Number.isFinite(n)) {
|
|
8
|
+
throw new BakerError(`min: bound must be a finite number, got ${n}`);
|
|
9
|
+
}
|
|
10
|
+
const exclusive = opts?.exclusive ?? false;
|
|
11
|
+
const plan = {
|
|
12
|
+
failure: planOr(planCompare(planValue(), '!==', planValue()), planCompare(planValue(), exclusive ? '<=' : '<', planLiteral(n))),
|
|
13
|
+
};
|
|
14
|
+
return makePlannedRule({
|
|
15
|
+
name: 'min',
|
|
16
|
+
requiresType: 'number',
|
|
17
|
+
constraints: exclusive ? { min: n, exclusive: true } : { min: n },
|
|
18
|
+
plan,
|
|
19
|
+
validate: exclusive ? value => typeof value === 'number' && value > n : value => typeof value === 'number' && value >= n,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
23
|
+
// max — v <= n check. requiresType='number' (§4.7, §4.8 A)
|
|
24
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
25
|
+
export function max(n, opts) {
|
|
26
|
+
if (!Number.isFinite(n)) {
|
|
27
|
+
throw new BakerError(`max: bound must be a finite number, got ${n}`);
|
|
28
|
+
}
|
|
29
|
+
const exclusive = opts?.exclusive ?? false;
|
|
30
|
+
const plan = {
|
|
31
|
+
failure: planOr(planCompare(planValue(), '!==', planValue()), planCompare(planValue(), exclusive ? '>=' : '>', planLiteral(n))),
|
|
32
|
+
};
|
|
33
|
+
return makePlannedRule({
|
|
34
|
+
name: 'max',
|
|
35
|
+
requiresType: 'number',
|
|
36
|
+
constraints: exclusive ? { max: n, exclusive: true } : { max: n },
|
|
37
|
+
plan,
|
|
38
|
+
validate: exclusive ? value => typeof value === 'number' && value < n : value => typeof value === 'number' && value <= n,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
42
|
+
// isPositive — v > 0 (0 not included). requiresType='number' (§4.8 A)
|
|
43
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
44
|
+
export const isPositive = makePlannedRule({
|
|
45
|
+
name: 'isPositive',
|
|
46
|
+
requiresType: 'number',
|
|
47
|
+
constraints: { min: 0, exclusive: true },
|
|
48
|
+
plan: {
|
|
49
|
+
failure: planOr(planCompare(planValue(), '!==', planValue()), planCompare(planValue(), '<=', planLiteral(0))),
|
|
50
|
+
},
|
|
51
|
+
validate: value => typeof value === 'number' && value > 0,
|
|
52
|
+
});
|
|
53
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
54
|
+
// isNegative — v < 0 (0 not included). requiresType='number' (§4.8 A)
|
|
55
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
56
|
+
export const isNegative = makePlannedRule({
|
|
57
|
+
name: 'isNegative',
|
|
58
|
+
requiresType: 'number',
|
|
59
|
+
constraints: { max: 0, exclusive: true },
|
|
60
|
+
plan: {
|
|
61
|
+
failure: planOr(planCompare(planValue(), '!==', planValue()), planCompare(planValue(), '>=', planLiteral(0))),
|
|
62
|
+
},
|
|
63
|
+
validate: value => typeof value === 'number' && value < 0,
|
|
64
|
+
});
|
|
65
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
66
|
+
// isDivisibleBy — v % n === 0 check. requiresType='number' (§4.8 A)
|
|
67
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
68
|
+
export function isDivisibleBy(n) {
|
|
69
|
+
if (n === 0) {
|
|
70
|
+
throw new BakerError('isDivisibleBy: divisor must not be zero');
|
|
71
|
+
}
|
|
72
|
+
return makeRule({
|
|
73
|
+
name: 'isDivisibleBy',
|
|
74
|
+
requiresType: 'number',
|
|
75
|
+
constraints: { divisor: n },
|
|
76
|
+
validate: value => typeof value === 'number' && value % n === 0,
|
|
77
|
+
emit: (varName, ctx) => `if (${varName} % ${n} !== 0) ${ctx.fail('isDivisibleBy')};`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -4,4 +4,4 @@ export interface IsNotEmptyObjectOptions {
|
|
|
4
4
|
nullable?: boolean;
|
|
5
5
|
}
|
|
6
6
|
export declare function isNotEmptyObject(options?: IsNotEmptyObjectOptions): EmittableRule;
|
|
7
|
-
export declare function isInstance(targetType: new (...args:
|
|
7
|
+
export declare function isInstance(targetType: new (...args: never[]) => object): EmittableRule;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { makeRule } from '../rule-plan.js';
|
|
2
|
+
export function isNotEmptyObject(options) {
|
|
3
|
+
const validate = (value) => {
|
|
4
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
const obj = value;
|
|
8
|
+
if (options?.nullable) {
|
|
9
|
+
for (const k in obj) {
|
|
10
|
+
if (obj[k] != null) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
for (const _k in obj) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
return false;
|
|
20
|
+
};
|
|
21
|
+
return makeRule({
|
|
22
|
+
name: 'isNotEmptyObject',
|
|
23
|
+
requiresType: 'object',
|
|
24
|
+
constraints: { nullable: options?.nullable },
|
|
25
|
+
validate,
|
|
26
|
+
// Codegen: for-in with break — measured ~1 ns faster than Object.keys allocation
|
|
27
|
+
// (Bun 1.3.13 / i7-13700K). The generated body is not subject to source-lint rules.
|
|
28
|
+
emit: (varName, ctx) => {
|
|
29
|
+
if (options?.nullable) {
|
|
30
|
+
return `{var __ne=false;for(var __k in ${varName}){if(${varName}[__k]!=null){__ne=true;break;}}if(!__ne) ${ctx.fail('isNotEmptyObject')};}`;
|
|
31
|
+
}
|
|
32
|
+
return `{var __ne=false;for(var __k in ${varName}){__ne=true;break;}if(!__ne) ${ctx.fail('isNotEmptyObject')};}`;
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
37
|
+
// isInstance(targetType) — checks if value is an instance of a specific class
|
|
38
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
39
|
+
export function isInstance(targetType) {
|
|
40
|
+
return makeRule({
|
|
41
|
+
name: 'isInstance',
|
|
42
|
+
constraints: { type: targetType.name },
|
|
43
|
+
validate: value => value instanceof targetType,
|
|
44
|
+
emit: (varName, ctx) => {
|
|
45
|
+
const i = ctx.addRef(targetType);
|
|
46
|
+
return `if (!(${varName} instanceof refs[${i}])) ${ctx.fail('isInstance')};`;
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -1,105 +1,108 @@
|
|
|
1
1
|
import type { EmittableRule } from '../types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
declare function minLength(min: number): EmittableRule;
|
|
3
|
+
declare function maxLength(max: number): EmittableRule;
|
|
4
|
+
declare function length(minLen: number, maxLen: number): EmittableRule;
|
|
5
|
+
declare function contains(seed: string): EmittableRule;
|
|
6
|
+
declare function notContains(seed: string): EmittableRule;
|
|
7
|
+
declare function matches(pattern: string | RegExp, modifiers?: string): EmittableRule;
|
|
8
|
+
declare const isLowercase: import("../types").InternalRule;
|
|
9
|
+
declare const isUppercase: import("../types").InternalRule;
|
|
10
|
+
declare const isAscii: EmittableRule;
|
|
11
|
+
declare const isAlpha: EmittableRule;
|
|
12
|
+
declare const isAlphanumeric: EmittableRule;
|
|
13
|
+
declare const isHttpToken: EmittableRule;
|
|
14
|
+
declare const isBooleanString: import("../types").InternalRule;
|
|
15
|
+
interface IsNumberStringOptions {
|
|
15
16
|
no_symbols?: boolean;
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
declare function isNumberString(options?: IsNumberStringOptions): EmittableRule;
|
|
19
|
+
declare function isDecimal(): EmittableRule;
|
|
20
|
+
declare const isFullWidth: EmittableRule;
|
|
21
|
+
declare const isHalfWidth: EmittableRule;
|
|
22
|
+
declare const isVariableWidth: EmittableRule;
|
|
23
|
+
declare const isMultibyte: EmittableRule;
|
|
24
|
+
declare const isSurrogatePair: EmittableRule;
|
|
25
|
+
declare const isHexadecimal: EmittableRule;
|
|
26
|
+
declare const isOctal: EmittableRule;
|
|
27
|
+
declare function isEmail(): EmittableRule;
|
|
28
|
+
interface IsURLOptions {
|
|
28
29
|
protocols?: string[];
|
|
29
30
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
declare function isURL(options?: IsURLOptions): EmittableRule;
|
|
32
|
+
declare function isUUID(version?: 1 | 2 | 3 | 4 | 5 | 'all'): EmittableRule;
|
|
33
|
+
declare function isIP(version?: 4 | 6): EmittableRule;
|
|
34
|
+
declare const isHexColor: EmittableRule;
|
|
35
|
+
declare function isRgbColor(includePercentValues?: boolean): EmittableRule;
|
|
36
|
+
declare const isHSL: EmittableRule;
|
|
37
|
+
interface IsMACAddressOptions {
|
|
37
38
|
no_separators?: boolean;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
declare function isMACAddress(options?: IsMACAddressOptions): EmittableRule;
|
|
41
|
+
declare function isISBN(version?: 10 | 13): EmittableRule;
|
|
42
|
+
declare const isISIN: EmittableRule;
|
|
43
|
+
interface IsISO8601Options {
|
|
43
44
|
strict?: boolean;
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
declare function isISO8601(options?: IsISO8601Options): EmittableRule;
|
|
47
|
+
declare const isISRC: EmittableRule;
|
|
48
|
+
interface IsISSNOptions {
|
|
48
49
|
requireHyphen?: boolean;
|
|
49
50
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
declare function isISSN(options?: IsISSNOptions): EmittableRule;
|
|
52
|
+
declare const isJWT: EmittableRule;
|
|
53
|
+
declare function isLatLong(): EmittableRule;
|
|
54
|
+
declare const isLocale: EmittableRule;
|
|
55
|
+
declare const isDataURI: EmittableRule;
|
|
56
|
+
interface IsFQDNOptions {
|
|
56
57
|
require_tld?: boolean;
|
|
57
58
|
allow_underscores?: boolean;
|
|
58
59
|
allow_trailing_dot?: boolean;
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
declare function isFQDN(options?: IsFQDNOptions): EmittableRule;
|
|
62
|
+
declare const isPort: EmittableRule;
|
|
63
|
+
declare const isEAN: EmittableRule;
|
|
64
|
+
declare const isISO31661Alpha2: import("../types").InternalRule;
|
|
65
|
+
declare const isISO31661Alpha3: import("../types").InternalRule;
|
|
66
|
+
declare const isBIC: EmittableRule;
|
|
67
|
+
declare const isFirebasePushId: EmittableRule;
|
|
68
|
+
declare const isSemVer: EmittableRule;
|
|
69
|
+
declare const isMongoId: EmittableRule;
|
|
70
|
+
declare const isJSON: import("../types").InternalRule;
|
|
71
|
+
declare function isBase32(): EmittableRule;
|
|
72
|
+
declare const isBase58: EmittableRule;
|
|
73
|
+
interface IsBase64Options {
|
|
73
74
|
urlSafe?: boolean;
|
|
74
75
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
declare function isBase64(options?: IsBase64Options): EmittableRule;
|
|
77
|
+
declare function isDateString(): EmittableRule;
|
|
78
|
+
declare const isMimeType: EmittableRule;
|
|
79
|
+
declare function isCurrency(): EmittableRule;
|
|
80
|
+
declare const isMagnetURI: EmittableRule;
|
|
81
|
+
declare const isCreditCard: import("../types").InternalRule;
|
|
82
|
+
interface IsIBANOptions {
|
|
82
83
|
allowSpaces?: boolean;
|
|
83
84
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
85
|
+
declare function isIBAN(options?: IsIBANOptions): EmittableRule;
|
|
86
|
+
declare function isByteLength(min: number, max?: number): EmittableRule;
|
|
87
|
+
declare function isHash(algorithm: string): EmittableRule;
|
|
88
|
+
declare const isRFC3339: EmittableRule;
|
|
89
|
+
declare const isMilitaryTime: EmittableRule;
|
|
90
|
+
declare const isLatitude: import("../types").InternalRule;
|
|
91
|
+
declare const isLongitude: import("../types").InternalRule;
|
|
92
|
+
declare const isEthereumAddress: EmittableRule;
|
|
93
|
+
declare const isBtcAddress: EmittableRule;
|
|
94
|
+
declare const isISO4217CurrencyCode: EmittableRule;
|
|
95
|
+
declare const isPhoneNumber: EmittableRule;
|
|
96
|
+
interface IsStrongPasswordOptions {
|
|
96
97
|
minLength?: number;
|
|
97
98
|
minLowercase?: number;
|
|
98
99
|
minUppercase?: number;
|
|
99
100
|
minNumbers?: number;
|
|
100
101
|
minSymbols?: number;
|
|
101
102
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
declare function isStrongPassword(options?: IsStrongPasswordOptions): EmittableRule;
|
|
104
|
+
declare function isTaxId(locale: string): EmittableRule;
|
|
105
|
+
declare function isULID(): EmittableRule;
|
|
106
|
+
declare function isCUID2(): EmittableRule;
|
|
107
|
+
export { minLength, maxLength, length, contains, notContains, matches, isLowercase, isUppercase, isAscii, isAlpha, isAlphanumeric, isHttpToken, isBooleanString, isNumberString, isDecimal, isFullWidth, isHalfWidth, isVariableWidth, isMultibyte, isSurrogatePair, isHexadecimal, isOctal, isEmail, isURL, isUUID, isIP, isHexColor, isRgbColor, isHSL, isMACAddress, isISBN, isISIN, isISO8601, isISRC, isISSN, isJWT, isLatLong, isLocale, isDataURI, isFQDN, isPort, isEAN, isISO31661Alpha2, isISO31661Alpha3, isBIC, isFirebasePushId, isSemVer, isMongoId, isJSON, isBase32, isBase58, isBase64, isDateString, isMimeType, isCurrency, isMagnetURI, isCreditCard, isIBAN, isByteLength, isHash, isRFC3339, isMilitaryTime, isLatitude, isLongitude, isEthereumAddress, isBtcAddress, isISO4217CurrencyCode, isPhoneNumber, isStrongPassword, isTaxId, isULID, isCUID2, };
|
|
108
|
+
export type { IsNumberStringOptions, IsURLOptions, IsMACAddressOptions, IsISO8601Options, IsISSNOptions, IsFQDNOptions, IsBase64Options, IsIBANOptions, IsStrongPasswordOptions, };
|