@zipbul/baker 3.0.1 → 3.2.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 CHANGED
@@ -1,5 +1,34 @@
1
1
  # @zipbul/baker
2
2
 
3
+ ## 3.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6b07d8d: Add the `oneOf` rule combinator and `arrayEvery`, `isRegExp`, `isFunction` rules.
8
+
9
+ - `oneOf(...rules)` — OR combinator: a value is valid if it matches at least one of the
10
+ given rules (first match wins, short-circuit). Fills baker's union-validation gap for
11
+ native-type unions (e.g. `boolean | string | RegExp | ...`) that the object discriminator
12
+ can't express. Sync branches compile to an inlined `||`; an async branch makes the rule
13
+ async. Note: semantics is "matches at least one" — not JSON-Schema `oneOf` (exactly-one).
14
+ - `arrayEvery(...rules)` — value is an array and every element satisfies all given rules.
15
+ Composable as a rule (e.g. `oneOf(isString, arrayEvery(isString))` for `string | string[]`).
16
+ - `isRegExp` / `isFunction` — the two missing type-checker primitives (`instanceof RegExp`,
17
+ `typeof === 'function'`).
18
+
19
+ All exported from `@zipbul/baker/rules`.
20
+
21
+ ## 3.1.0
22
+
23
+ ### Minor Changes
24
+
25
+ - c40834b: Add `isOrigin` and `isCorsOrigin` string rules for RFC 6454 §6.2 serialized-origin
26
+ validation. `isOrigin` accepts only the canonical WHATWG URL `.origin` form (rejecting
27
+ trailing slash, path/query/fragment, uppercase scheme/host, explicit default ports,
28
+ userinfo, and raw IDN — punycode required) plus the opaque `'null'` literal. `isCorsOrigin`
29
+ is the CORS superset that additionally accepts the `'*'` wildcard. Both work standalone
30
+ (`isOrigin('https://a.com')`) and as `@Field` rules.
31
+
3
32
  ## 3.0.1
4
33
 
5
34
  ### Patch Changes
@@ -0,0 +1,4 @@
1
+ import type { EmittableRule } from '../types';
2
+ declare function oneOf(...branches: EmittableRule[]): EmittableRule;
3
+ declare function arrayEvery(...rules: EmittableRule[]): EmittableRule;
4
+ export { oneOf, arrayEvery };
@@ -0,0 +1,111 @@
1
+ import { BakerError } from '../errors.js';
2
+ import { makeRule } from '../rule-plan.js';
3
+ // ─────────────────────────────────────────────────────────────────────────────
4
+ // Helpers
5
+ // ─────────────────────────────────────────────────────────────────────────────
6
+ /** Assert that a value is a baker rule (callable with `.emit` fn + `.ruleName` string). */
7
+ function assertRuleArg(value, combinator) {
8
+ if (typeof value === 'function' &&
9
+ typeof value.emit === 'function' &&
10
+ typeof value.ruleName === 'string') {
11
+ return;
12
+ }
13
+ throw new BakerError(`${combinator}: every argument must be a baker rule (function with .emit and .ruleName). Use createRule() or import a rule from @zipbul/baker/rules.`);
14
+ }
15
+ // ─────────────────────────────────────────────────────────────────────────────
16
+ // oneOf — OR combinator: value matches at least one of the given rules.
17
+ // (Not JSON-Schema `oneOf`/exactly-one — semantics is "matches at least one",
18
+ // first matching branch wins, short-circuit.)
19
+ // ─────────────────────────────────────────────────────────────────────────────
20
+ function oneOf(...branches) {
21
+ if (branches.length === 0) {
22
+ throw new BakerError('oneOf requires at least one rule.');
23
+ }
24
+ for (const b of branches) {
25
+ assertRuleArg(b, 'oneOf');
26
+ }
27
+ const constraints = { oneOf: branches.map(b => b.ruleName) };
28
+ const isAsync = branches.some(b => b.isAsync === true);
29
+ if (isAsync) {
30
+ const validate = async (value) => {
31
+ for (const b of branches) {
32
+ if (await b(value)) {
33
+ return true;
34
+ }
35
+ }
36
+ return false;
37
+ };
38
+ return makeRule({
39
+ name: 'oneOf',
40
+ constraints,
41
+ isAsync: true,
42
+ validate,
43
+ emit: (varName, ctx) => {
44
+ const i = ctx.addRef(validate);
45
+ return `if (!(await refs[${i}](${varName}))) ${ctx.fail('oneOf')};`;
46
+ },
47
+ });
48
+ }
49
+ const validate = (value) => branches.some(b => b(value));
50
+ return makeRule({
51
+ name: 'oneOf',
52
+ constraints,
53
+ validate,
54
+ emit: (varName, ctx) => {
55
+ const calls = branches.map(b => `refs[${ctx.addRef(b)}](${varName})`).join(' || ');
56
+ return `if (!(${calls})) ${ctx.fail('oneOf')};`;
57
+ },
58
+ });
59
+ }
60
+ // ─────────────────────────────────────────────────────────────────────────────
61
+ // arrayEvery — value is an array and every element satisfies all given rules
62
+ // (AND over the rules, applied per element). Arrays only — Set/Map element
63
+ // validation stays at the @Field level via arrayOf.
64
+ // ─────────────────────────────────────────────────────────────────────────────
65
+ function arrayEvery(...rules) {
66
+ if (rules.length === 0) {
67
+ throw new BakerError('arrayEvery requires at least one rule.');
68
+ }
69
+ for (const r of rules) {
70
+ assertRuleArg(r, 'arrayEvery');
71
+ }
72
+ const constraints = { arrayEvery: rules.map(r => r.ruleName) };
73
+ const isAsync = rules.some(r => r.isAsync === true);
74
+ if (isAsync) {
75
+ const validate = async (value) => {
76
+ if (!Array.isArray(value)) {
77
+ return false;
78
+ }
79
+ for (const el of value) {
80
+ for (const r of rules) {
81
+ if (!(await r(el))) {
82
+ return false;
83
+ }
84
+ }
85
+ }
86
+ return true;
87
+ };
88
+ return makeRule({
89
+ name: 'arrayEvery',
90
+ constraints,
91
+ isAsync: true,
92
+ validate,
93
+ emit: (varName, ctx) => {
94
+ const i = ctx.addRef(validate);
95
+ return `if (!(await refs[${i}](${varName}))) ${ctx.fail('arrayEvery')};`;
96
+ },
97
+ });
98
+ }
99
+ const elementPredicate = (el) => rules.every(r => r(el));
100
+ const validate = (value) => Array.isArray(value) && value.every(elementPredicate);
101
+ return makeRule({
102
+ name: 'arrayEvery',
103
+ constraints,
104
+ validate,
105
+ emit: (varName, ctx) => {
106
+ const i = ctx.addRef(elementPredicate);
107
+ return `if (!(Array.isArray(${varName}) && ${varName}.every(refs[${i}]))) ${ctx.fail('arrayEvery')};`;
108
+ },
109
+ });
110
+ }
111
+ export { oneOf, arrayEvery };
@@ -1,9 +1,10 @@
1
- export { isString, isNumber, isBoolean, isDate, isEnum, isInt, isArray, isObject } from './typechecker';
1
+ export { isString, isNumber, isBoolean, isDate, isEnum, isInt, isArray, isObject, isRegExp, isFunction } from './typechecker';
2
2
  export type { IsNumberOptions } from './typechecker';
3
+ export { oneOf, arrayEvery } from './combinators';
3
4
  export { min, max, isPositive, isNegative, isDivisibleBy } from './number';
4
5
  export { minDate, maxDate } from './date';
5
6
  export { equals, notEquals, isEmpty, isNotEmpty, isIn, isNotIn } from './common';
6
- 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, } from './string';
7
+ export { minLength, maxLength, length, contains, notContains, matches, isLowercase, isUppercase, isAscii, isAlpha, isAlphanumeric, isHttpToken, isOrigin, isCorsOrigin, 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, } from './string';
7
8
  export type { IsURLOptions, IsBase64Options, IsMACAddressOptions, IsIBANOptions, IsISSNOptions, IsFQDNOptions, IsISO8601Options, IsNumberStringOptions, IsStrongPasswordOptions, } from './string';
8
9
  export { arrayContains, arrayNotContains, arrayMinSize, arrayMaxSize, arrayUnique, arrayNotEmpty } from './array';
9
10
  export { isNotEmptyObject, isInstance } from './object';
@@ -1,8 +1,9 @@
1
- export { isString, isNumber, isBoolean, isDate, isEnum, isInt, isArray, isObject } from './typechecker.js';
1
+ export { isString, isNumber, isBoolean, isDate, isEnum, isInt, isArray, isObject, isRegExp, isFunction } from './typechecker.js';
2
+ export { oneOf, arrayEvery } from './combinators.js';
2
3
  export { min, max, isPositive, isNegative, isDivisibleBy } from './number.js';
3
4
  export { minDate, maxDate } from './date.js';
4
5
  export { equals, notEquals, isEmpty, isNotEmpty, isIn, isNotIn } from './common.js';
5
- 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, } from './string.js';
6
+ export { minLength, maxLength, length, contains, notContains, matches, isLowercase, isUppercase, isAscii, isAlpha, isAlphanumeric, isHttpToken, isOrigin, isCorsOrigin, 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, } from './string.js';
6
7
  export { arrayContains, arrayNotContains, arrayMinSize, arrayMaxSize, arrayUnique, arrayNotEmpty } from './array.js';
7
8
  export { isNotEmptyObject, isInstance } from './object.js';
8
9
  export { isMobilePhone, isPostalCode, isIdentityCard, isPassportNumber } from './locales.js';
@@ -11,6 +11,8 @@ declare const isAscii: EmittableRule;
11
11
  declare const isAlpha: EmittableRule;
12
12
  declare const isAlphanumeric: EmittableRule;
13
13
  declare const isHttpToken: EmittableRule;
14
+ declare const isOrigin: import("../types").InternalRule;
15
+ declare const isCorsOrigin: import("../types").InternalRule;
14
16
  declare const isBooleanString: import("../types").InternalRule;
15
17
  interface IsNumberStringOptions {
16
18
  no_symbols?: boolean;
@@ -104,5 +106,5 @@ declare function isStrongPassword(options?: IsStrongPasswordOptions): EmittableR
104
106
  declare function isTaxId(locale: string): EmittableRule;
105
107
  declare function isULID(): EmittableRule;
106
108
  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, };
109
+ export { minLength, maxLength, length, contains, notContains, matches, isLowercase, isUppercase, isAscii, isAlpha, isAlphanumeric, isHttpToken, isOrigin, isCorsOrigin, 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
110
  export type { IsNumberStringOptions, IsURLOptions, IsMACAddressOptions, IsISO8601Options, IsISSNOptions, IsFQDNOptions, IsBase64Options, IsIBANOptions, IsStrongPasswordOptions, };
@@ -129,6 +129,43 @@ const isHttpToken = makeStringRule('isHttpToken', v => HTTP_TOKEN_RE.test(v), (v
129
129
  const i = ctx.addRegex(HTTP_TOKEN_RE);
130
130
  return `if (!re[${i}].test(${varName})) ${ctx.fail('isHttpToken')};`;
131
131
  });
132
+ // RFC 6454 §6.2 serialized origin — a string equal to WHATWG URL `.origin`.
133
+ // The opaque-origin literal 'null' is matched explicitly because `new URL('null')` throws.
134
+ // '*' (CORS wildcard) is rejected here; use isCorsOrigin for the CORS superset.
135
+ const isOriginValue = (value) => {
136
+ if (value === 'null') {
137
+ return true;
138
+ }
139
+ try {
140
+ return new URL(value).origin === value;
141
+ }
142
+ catch {
143
+ return false;
144
+ }
145
+ };
146
+ const isOrigin = makeRule({
147
+ name: 'isOrigin',
148
+ requiresType: 'string',
149
+ constraints: { format: 'origin' },
150
+ validate: value => typeof value === 'string' && isOriginValue(value),
151
+ emit: (varName, ctx) => {
152
+ const i = ctx.addRef(isOriginValue);
153
+ return `if (!(refs[${i}](${varName}))) ${ctx.fail('isOrigin')};`;
154
+ },
155
+ });
156
+ // CORS superset of isOrigin: additionally accepts the '*' wildcard literal
157
+ // (Access-Control-Allow-Origin). Keep '*' out of the general isOrigin.
158
+ const isCorsOriginValue = (value) => value === '*' || isOriginValue(value);
159
+ const isCorsOrigin = makeRule({
160
+ name: 'isCorsOrigin',
161
+ requiresType: 'string',
162
+ constraints: { format: 'origin', allowWildcard: true },
163
+ validate: value => typeof value === 'string' && isCorsOriginValue(value),
164
+ emit: (varName, ctx) => {
165
+ const i = ctx.addRef(isCorsOriginValue);
166
+ return `if (!(refs[${i}](${varName}))) ${ctx.fail('isCorsOrigin')};`;
167
+ },
168
+ });
132
169
  // BooleanString: 'true' | 'false' | '1' | '0'
133
170
  const isBooleanString = makeRule({
134
171
  name: 'isBooleanString',
@@ -1995,4 +2032,4 @@ function isCUID2() {
1995
2032
  return `if (!re[${i}].test(${varName})) ${ctx.fail('isCUID2')};`;
1996
2033
  }, 'string', { format: 'cuid2' });
1997
2034
  }
1998
- 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, };
2035
+ export { minLength, maxLength, length, contains, notContains, matches, isLowercase, isUppercase, isAscii, isAlpha, isAlphanumeric, isHttpToken, isOrigin, isCorsOrigin, 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, };
@@ -12,3 +12,5 @@ export declare function isEnum(entity: object): EmittableRule;
12
12
  export declare const isInt: import("../types").InternalRule;
13
13
  export declare const isArray: import("../types").InternalRule;
14
14
  export declare const isObject: import("../types").InternalRule;
15
+ export declare const isRegExp: import("../types").InternalRule;
16
+ export declare const isFunction: import("../types").InternalRule;
@@ -141,3 +141,21 @@ export const isObject = makeRule({
141
141
  validate: value => typeof value === 'object' && value !== null && !Array.isArray(value),
142
142
  emit: (varName, ctx) => `if (typeof ${varName} !== 'object' || ${varName} === null || Array.isArray(${varName})) ${ctx.fail('isObject')};`,
143
143
  });
144
+ // ─────────────────────────────────────────────────────────────────────────────
145
+ // isRegExp — instanceof RegExp (self-narrowing, no typeof gate needed)
146
+ // ─────────────────────────────────────────────────────────────────────────────
147
+ export const isRegExp = makeRule({
148
+ name: 'isRegExp',
149
+ constraints: {},
150
+ validate: value => value instanceof RegExp,
151
+ emit: (varName, ctx) => `if (!(${varName} instanceof RegExp)) ${ctx.fail('isRegExp')};`,
152
+ });
153
+ // ─────────────────────────────────────────────────────────────────────────────
154
+ // isFunction — typeof check (accepts arrow fns, unlike isInstance(Function))
155
+ // ─────────────────────────────────────────────────────────────────────────────
156
+ export const isFunction = makeRule({
157
+ name: 'isFunction',
158
+ constraints: {},
159
+ validate: value => typeof value === 'function',
160
+ emit: (varName, ctx) => `if (typeof ${varName} !== 'function') ${ctx.fail('isFunction')};`,
161
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zipbul/baker",
3
- "version": "3.0.1",
3
+ "version": "3.2.0",
4
4
  "description": "Bun-only AOT decorator-based DTO validation & serialization. class-validator DX, sealed code generation, zero reflect-metadata.",
5
5
  "keywords": [
6
6
  "aot",