@wildo-ai/zod-decorators 1.1.10 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/install-bigint-json-encoding.d.ts +1 -1
- package/dist/esm/install-bigint-json-encoding.js +1 -1
- package/dist/esm/install-bigint-json-encoding.js.map +1 -1
- package/dist/esm/testing/decorated-test-schemas.d.ts +8 -8
- package/dist/esm/zod-bigdecimal.d.ts +16 -8
- package/dist/esm/zod-bigdecimal.d.ts.map +1 -1
- package/dist/esm/zod-bigdecimal.js.map +1 -1
- package/dist/esm/zod-code.d.ts +17 -9
- package/dist/esm/zod-code.d.ts.map +1 -1
- package/dist/esm/zod-code.js.map +1 -1
- package/dist/esm/zod-decorators.types.d.ts +446 -48
- package/dist/esm/zod-decorators.types.d.ts.map +1 -1
- package/dist/esm/zod-decorators.types.js +190 -14
- package/dist/esm/zod-decorators.types.js.map +1 -1
- package/dist/esm/zod-duration.d.ts +24 -16
- package/dist/esm/zod-duration.d.ts.map +1 -1
- package/dist/esm/zod-duration.js.map +1 -1
- package/dist/esm/zod-file.d.ts +5 -8
- package/dist/esm/zod-file.d.ts.map +1 -1
- package/dist/esm/zod-file.js +10 -21
- package/dist/esm/zod-file.js.map +1 -1
- package/dist/esm/zod-money.d.ts +22 -14
- package/dist/esm/zod-money.d.ts.map +1 -1
- package/dist/esm/zod-money.js.map +1 -1
- package/dist/esm/zod-phone.d.ts +22 -14
- package/dist/esm/zod-phone.d.ts.map +1 -1
- package/dist/esm/zod-phone.js.map +1 -1
- package/dist/esm/zod-range.d.ts.map +1 -1
- package/dist/esm/zod-range.js +5 -1
- package/dist/esm/zod-range.js.map +1 -1
- package/dist/esm/zod-utils.shared.d.ts +146 -11
- package/dist/esm/zod-utils.shared.d.ts.map +1 -1
- package/dist/esm/zod-utils.shared.js +358 -45
- package/dist/esm/zod-utils.shared.js.map +1 -1
- package/dist/esm/zod-wire-date.d.ts +107 -0
- package/dist/esm/zod-wire-date.d.ts.map +1 -0
- package/dist/esm/zod-wire-date.js +162 -0
- package/dist/esm/zod-wire-date.js.map +1 -0
- package/dist/esm/zod-year-month.d.ts +76 -0
- package/dist/esm/zod-year-month.d.ts.map +1 -0
- package/dist/esm/zod-year-month.js +115 -0
- package/dist/esm/zod-year-month.js.map +1 -0
- package/dist/esm/zod.module-override.d.ts +263 -159
- package/dist/esm/zod.module-override.d.ts.map +1 -1
- package/dist/esm/zod.module-override.js +608 -66
- package/dist/esm/zod.module-override.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +5 -5
package/dist/esm/index.d.ts
CHANGED
|
@@ -3,8 +3,10 @@ export declare function initZodDecorators(zodInstance?: typeof z): typeof z;
|
|
|
3
3
|
export * from './commons';
|
|
4
4
|
export * from './install-bigint-json-encoding';
|
|
5
5
|
export * from './zod-color';
|
|
6
|
+
export * from './zod-year-month';
|
|
6
7
|
export * from './zod-range';
|
|
7
8
|
export * from './zod-money';
|
|
9
|
+
export * from './zod-wire-date';
|
|
8
10
|
export * from './zod-bigdecimal';
|
|
9
11
|
export * from './zod-duration';
|
|
10
12
|
export * from './zod-phone';
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAiBxB,wBAAgB,iBAAiB,CAAC,WAAW,GAAE,OAAO,CAAK,GAAG,OAAO,CAAC,CAErE;AAED,cAAc,WAAW,CAAC;AAE1B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACj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,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AAIzC;;;;;;GAMG"}
|
package/dist/esm/index.js
CHANGED
|
@@ -18,8 +18,10 @@ export function initZodDecorators(zodInstance = z) {
|
|
|
18
18
|
export * from './commons.js';
|
|
19
19
|
export * from './install-bigint-json-encoding.js';
|
|
20
20
|
export * from './zod-color.js';
|
|
21
|
+
export * from './zod-year-month.js';
|
|
21
22
|
export * from './zod-range.js';
|
|
22
23
|
export * from './zod-money.js';
|
|
24
|
+
export * from './zod-wire-date.js';
|
|
23
25
|
export * from './zod-bigdecimal.js';
|
|
24
26
|
export * from './zod-duration.js';
|
|
25
27
|
export * from './zod-phone.js';
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAE3E,uEAAuE;AACvE,4FAA4F;AAC5F,0FAA0F;AAC1F,yBAAyB,CAAC,CAAC,CAAC,CAAC;AAE7B,6EAA6E;AAC7E,8EAA8E;AAC9E,+EAA+E;AAC/E,kFAAkF;AAClF,mFAAmF;AACnF,kFAAkF;AAClF,yBAAyB,EAAE,CAAC;AAE5B,MAAM,UAAU,iBAAiB,CAAC,cAAwB,CAAC;IACzD,OAAO,yBAAyB,CAAC,WAAW,CAAC,CAAC;AAChD,CAAC;AAED,cAAc,WAAW,CAAC;AAE1B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AAIzC;;;;;;GAMG","sourcesContent":["import { z } from 'zod';\nimport { ensureZodDecoratorsLoaded } from './zod.module-override';\nimport { installBigIntJsonEncoding } from './install-bigint-json-encoding';\n\n// CRITICAL: Apply decorators immediately when this module is imported.\n// This ensures z.string().isUnique() etc. work without needing to call initZodDecorators().\n// The bundler may tree-shake the call in zod.module-override.ts, so we duplicate it here.\nensureZodDecoratorsLoaded(z);\n\n// Install the type-level `bigint → decimal string` JSON encoding at the SAME\n// bootstrap. `z_money` (owned by this package) stores `amount` as a `bigint`,\n// which `JSON.stringify` cannot serialize (it has no `toJSON`, unlike `Date`).\n// Installing it here — the earliest point, before any schema or serializer runs —\n// makes every JSON boundary (res.json, socket.io, queue, redis, HMAC, third-party)\n// bigint-safe with no per-boundary opt-in. See `install-bigint-json-encoding.ts`.\ninstallBigIntJsonEncoding();\n\nexport function initZodDecorators(zodInstance: typeof z = z): typeof z {\n return ensureZodDecoratorsLoaded(zodInstance);\n}\n\nexport * from './commons';\n\nexport * from './install-bigint-json-encoding';\nexport * from './zod-color';\nexport * from './zod-range';\nexport * from './zod-money';\nexport * from './zod-bigdecimal';\nexport * from './zod-duration';\nexport * from './zod-phone';\nexport * from './zod-address';\nexport * from './zod-code';\nexport * from './zod-file';\nexport * from './zod-versioned';\nexport * from './zod-document-sequence';\nexport * from './currencies.shared.schemas';\nexport * from './zod-utils.shared';\nexport * from './zod-decorators.types';\nexport * from './zod.module-override';\nexport * from './files/file-extensions.enum';\nexport * from './files/extension-nature-map';\nexport * from './files/mime-registry';\nexport * from './files/agent-file-formats';\nexport * from './files/unified-registry';\n\n\n\n/**\n * Initialize Zod decorators for the current Zod instance.\n * Safe to call multiple times - will only apply decorators once.\n *\n * This function should be called early in your application startup,\n * especially when using custom-shared-lib or custom-backend-lib packages.\n */\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,yBAAyB,EAAE,MAAM,uBAAuB,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,MAAM,gCAAgC,CAAC;AAE3E,uEAAuE;AACvE,4FAA4F;AAC5F,0FAA0F;AAC1F,yBAAyB,CAAC,CAAC,CAAC,CAAC;AAE7B,6EAA6E;AAC7E,8EAA8E;AAC9E,+EAA+E;AAC/E,kFAAkF;AAClF,mFAAmF;AACnF,kFAAkF;AAClF,yBAAyB,EAAE,CAAC;AAE5B,MAAM,UAAU,iBAAiB,CAAC,cAAwB,CAAC;IACzD,OAAO,yBAAyB,CAAC,WAAW,CAAC,CAAC;AAChD,CAAC;AAED,cAAc,WAAW,CAAC;AAE1B,cAAc,gCAAgC,CAAC;AAC/C,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AAIzC;;;;;;GAMG","sourcesContent":["import { z } from 'zod';\nimport { ensureZodDecoratorsLoaded } from './zod.module-override';\nimport { installBigIntJsonEncoding } from './install-bigint-json-encoding';\n\n// CRITICAL: Apply decorators immediately when this module is imported.\n// This ensures z.string().isUnique() etc. work without needing to call initZodDecorators().\n// The bundler may tree-shake the call in zod.module-override.ts, so we duplicate it here.\nensureZodDecoratorsLoaded(z);\n\n// Install the type-level `bigint → decimal string` JSON encoding at the SAME\n// bootstrap. `z_money` (owned by this package) stores `amount` as a `bigint`,\n// which `JSON.stringify` cannot serialize (it has no `toJSON`, unlike `Date`).\n// Installing it here — the earliest point, before any schema or serializer runs —\n// makes every JSON boundary (res.json, socket.io, queue, redis, HMAC, third-party)\n// bigint-safe with no per-boundary opt-in. See `install-bigint-json-encoding.ts`.\ninstallBigIntJsonEncoding();\n\nexport function initZodDecorators(zodInstance: typeof z = z): typeof z {\n return ensureZodDecoratorsLoaded(zodInstance);\n}\n\nexport * from './commons';\n\nexport * from './install-bigint-json-encoding';\nexport * from './zod-color';\nexport * from './zod-year-month';\nexport * from './zod-range';\nexport * from './zod-money';\nexport * from './zod-wire-date';\nexport * from './zod-bigdecimal';\nexport * from './zod-duration';\nexport * from './zod-phone';\nexport * from './zod-address';\nexport * from './zod-code';\nexport * from './zod-file';\nexport * from './zod-versioned';\nexport * from './zod-document-sequence';\nexport * from './currencies.shared.schemas';\nexport * from './zod-utils.shared';\nexport * from './zod-decorators.types';\nexport * from './zod.module-override';\nexport * from './files/file-extensions.enum';\nexport * from './files/extension-nature-map';\nexport * from './files/mime-registry';\nexport * from './files/agent-file-formats';\nexport * from './files/unified-registry';\n\n\n\n/**\n * Initialize Zod decorators for the current Zod instance.\n * Safe to call multiple times - will only apply decorators once.\n *\n * This function should be called early in your application startup,\n * especially when using custom-shared-lib or custom-backend-lib packages.\n */\n"]}
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
* | Concern | `Date` | money `bigint` |
|
|
59
59
|
* |---|---|---|
|
|
60
60
|
* | SERIALIZE (runtime type → wire string) — PRIMARY | `Date.prototype.toJSON` (built-in) | **this module** (`installBigIntJsonEncoding`) |
|
|
61
|
-
* | COERCE (wire string → runtime type, on receive) | `z.date()` → `
|
|
61
|
+
* | COERCE (wire string → runtime type, on receive) | `z.date()` → `z_wireDate()` | `z.bigint()` → string-accepting `z.preprocess` |
|
|
62
62
|
* | — both coerce leaves rewritten by | `ResourcesConfigOperationsDtosBuilderUtils.applyNetworkBoundaryCoercion` (`@wildo-ai/saas-models`) — used by the SHARED `responseDto`/`requestDto`, so backend serialize-parse, frontend receive-parse, and worker re-hydration all coerce identically | (same) |
|
|
63
63
|
* | In-BODY pre-serialize normalization (backend) | `transformResponseDates` (Date→ISO string in the body) | **none by design** — bigint stays bigint in the body and relies on the SERIALIZE half above (a `transformResponseBigints` twin would be redundant with this `toJSON`) |
|
|
64
64
|
* | DEFENSE-IN-DEPTH serialize (belt-and-suspenders) | (n/a — `toJSON` is built-in) | `ResourceSerializationUtils.bigIntSafeJsonReplacer` / `stringifyWithBigInt` (queue, webhook), and the Express app-level `json replacer` (`http-server.backend.service`) |
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
* | Concern | `Date` | money `bigint` |
|
|
59
59
|
* |---|---|---|
|
|
60
60
|
* | SERIALIZE (runtime type → wire string) — PRIMARY | `Date.prototype.toJSON` (built-in) | **this module** (`installBigIntJsonEncoding`) |
|
|
61
|
-
* | COERCE (wire string → runtime type, on receive) | `z.date()` → `
|
|
61
|
+
* | COERCE (wire string → runtime type, on receive) | `z.date()` → `z_wireDate()` | `z.bigint()` → string-accepting `z.preprocess` |
|
|
62
62
|
* | — both coerce leaves rewritten by | `ResourcesConfigOperationsDtosBuilderUtils.applyNetworkBoundaryCoercion` (`@wildo-ai/saas-models`) — used by the SHARED `responseDto`/`requestDto`, so backend serialize-parse, frontend receive-parse, and worker re-hydration all coerce identically | (same) |
|
|
63
63
|
* | In-BODY pre-serialize normalization (backend) | `transformResponseDates` (Date→ISO string in the body) | **none by design** — bigint stays bigint in the body and relies on the SERIALIZE half above (a `transformResponseBigints` twin would be redundant with this `toJSON`) |
|
|
64
64
|
* | DEFENSE-IN-DEPTH serialize (belt-and-suspenders) | (n/a — `toJSON` is built-in) | `ResourceSerializationUtils.bigIntSafeJsonReplacer` / `stringifyWithBigInt` (queue, webhook), and the Express app-level `json replacer` (`http-server.backend.service`) |
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"install-bigint-json-encoding.js","sourceRoot":"","sources":["../../src/install-bigint-json-encoding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AAWH;;;;GAIG;AACH,MAAM,UAAU,yBAAyB;IACvC,IAAI,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,KAAK,UAAU;QAAE,OAAO;IAC1D,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,EAAE;QAChD,KAAK,EAAE,SAAS,MAAM;YACpB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;QACD,QAAQ,EAAE,IAAI;QACd,YAAY,EAAE,IAAI;QAClB,UAAU,EAAE,KAAK;KAClB,CAAC,CAAC;AACL,CAAC","sourcesContent":["/**\n * Process-wide JSON encoding for `bigint` — the type-level counterpart of\n * `Date.prototype.toJSON`.\n *\n * ## Why this exists\n *\n * `z_money` (`MoneyValue`) stores its `amount` as a `bigint` — deliberately, to\n * avoid the >2^53 precision loss `number` would introduce for monetary amounts.\n * But `bigint` is the one runtime type in the resource pipeline that has **no\n * JSON representation**: `JSON.stringify(1999n)` throws\n * `TypeError: Do not know how to serialize a BigInt`. `Date` never has this\n * problem because `Date.prototype.toJSON` exists, so *every* serializer — Express\n * `res.json`, `socket.io` emit, queue encoders, `redis` cache writes, HMAC\n * integrity hashes, any third-party lib — inherits date→ISO-string encoding for\n * free. `bigint` had no such method, so each of those boundaries had to opt in\n * to a bigint-safe stringify individually — a treadmill where the next bare\n * `JSON.stringify(payload)` silently reintroduces a 500 (and, worse, the\n * `socket.io` encoder is a separate serialization path an Express `json replacer`\n * can never reach).\n *\n * Installing `BigInt.prototype.toJSON` supplies the missing type-level encoding\n * once, so bigint round-trips through *every* JSON boundary exactly as `Date`\n * does — no per-boundary opt-in.\n *\n * ## Encoding choice: decimal STRING, not number\n *\n * `1999n → \"1999\"`. A string round-trips losslessly (`BigInt(\"1999\")`), whereas\n * `Number(1999n)` would reintroduce precisely the precision risk `bigint` exists\n * to prevent. This matches the framework's documented money wire-contract: the\n * response-parsing schema coerces the decimal-string form back to `bigint` on\n * receive (`applyNetworkBoundaryCoercion` rewrites `z.bigint()` leaves to accept\n * it). The provider-boundary `Number()` narrowing (e.g. Stripe's numeric amount\n * API) stays a deliberate, explicit, local conversion in provider code and is\n * unaffected by this default.\n *\n * ## Why a global built-in augmentation (and why it is safe)\n *\n * This mutates a built-in prototype — normally an anti-pattern. It is the\n * correct choice here for the same reason `Date.prototype.toJSON` is: the JSON\n * form of a value is a property of its *type*, not of each call site. It is\n * additive (no method is being replaced — `BigInt.prototype.toJSON` does not\n * otherwise exist), non-enumerable (invisible to `for...in` / `Object.keys`),\n * and idempotent. No known library relies on `JSON.stringify(bigint)` throwing.\n *\n * Installed as a top-level side effect of `@wildo-ai/zod-decorators` (the package\n * that owns `z_money` and therefore introduces bigint into resource payloads in\n * the first place), so the encoding is guaranteed present wherever money can be —\n * every backend/companion/worker/minion runtime AND the browser (which protects\n * the frontend request-submit path too).\n *\n * ## Network-boundary mechanism map (the canonical trace)\n *\n * `bigint` (money) and `Date` are the two runtime types that do NOT survive a JSON\n * round-trip as themselves. The framework handles BOTH with a symmetric two-half\n * contract. This module is the OUTBOUND (serialize) half for `bigint`; the entries\n * below are its counterparts. When editing any one, keep the pair coherent.\n *\n * | Concern | `Date` | money `bigint` |\n * |---|---|---|\n * | SERIALIZE (runtime type → wire string) — PRIMARY | `Date.prototype.toJSON` (built-in) | **this module** (`installBigIntJsonEncoding`) |\n * | COERCE (wire string → runtime type, on receive) | `z.date()` → `
|
|
1
|
+
{"version":3,"file":"install-bigint-json-encoding.js","sourceRoot":"","sources":["../../src/install-bigint-json-encoding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkEG;AAWH;;;;GAIG;AACH,MAAM,UAAU,yBAAyB;IACvC,IAAI,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,KAAK,UAAU;QAAE,OAAO;IAC1D,MAAM,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,EAAE;QAChD,KAAK,EAAE,SAAS,MAAM;YACpB,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;QACzB,CAAC;QACD,QAAQ,EAAE,IAAI;QACd,YAAY,EAAE,IAAI;QAClB,UAAU,EAAE,KAAK;KAClB,CAAC,CAAC;AACL,CAAC","sourcesContent":["/**\n * Process-wide JSON encoding for `bigint` — the type-level counterpart of\n * `Date.prototype.toJSON`.\n *\n * ## Why this exists\n *\n * `z_money` (`MoneyValue`) stores its `amount` as a `bigint` — deliberately, to\n * avoid the >2^53 precision loss `number` would introduce for monetary amounts.\n * But `bigint` is the one runtime type in the resource pipeline that has **no\n * JSON representation**: `JSON.stringify(1999n)` throws\n * `TypeError: Do not know how to serialize a BigInt`. `Date` never has this\n * problem because `Date.prototype.toJSON` exists, so *every* serializer — Express\n * `res.json`, `socket.io` emit, queue encoders, `redis` cache writes, HMAC\n * integrity hashes, any third-party lib — inherits date→ISO-string encoding for\n * free. `bigint` had no such method, so each of those boundaries had to opt in\n * to a bigint-safe stringify individually — a treadmill where the next bare\n * `JSON.stringify(payload)` silently reintroduces a 500 (and, worse, the\n * `socket.io` encoder is a separate serialization path an Express `json replacer`\n * can never reach).\n *\n * Installing `BigInt.prototype.toJSON` supplies the missing type-level encoding\n * once, so bigint round-trips through *every* JSON boundary exactly as `Date`\n * does — no per-boundary opt-in.\n *\n * ## Encoding choice: decimal STRING, not number\n *\n * `1999n → \"1999\"`. A string round-trips losslessly (`BigInt(\"1999\")`), whereas\n * `Number(1999n)` would reintroduce precisely the precision risk `bigint` exists\n * to prevent. This matches the framework's documented money wire-contract: the\n * response-parsing schema coerces the decimal-string form back to `bigint` on\n * receive (`applyNetworkBoundaryCoercion` rewrites `z.bigint()` leaves to accept\n * it). The provider-boundary `Number()` narrowing (e.g. Stripe's numeric amount\n * API) stays a deliberate, explicit, local conversion in provider code and is\n * unaffected by this default.\n *\n * ## Why a global built-in augmentation (and why it is safe)\n *\n * This mutates a built-in prototype — normally an anti-pattern. It is the\n * correct choice here for the same reason `Date.prototype.toJSON` is: the JSON\n * form of a value is a property of its *type*, not of each call site. It is\n * additive (no method is being replaced — `BigInt.prototype.toJSON` does not\n * otherwise exist), non-enumerable (invisible to `for...in` / `Object.keys`),\n * and idempotent. No known library relies on `JSON.stringify(bigint)` throwing.\n *\n * Installed as a top-level side effect of `@wildo-ai/zod-decorators` (the package\n * that owns `z_money` and therefore introduces bigint into resource payloads in\n * the first place), so the encoding is guaranteed present wherever money can be —\n * every backend/companion/worker/minion runtime AND the browser (which protects\n * the frontend request-submit path too).\n *\n * ## Network-boundary mechanism map (the canonical trace)\n *\n * `bigint` (money) and `Date` are the two runtime types that do NOT survive a JSON\n * round-trip as themselves. The framework handles BOTH with a symmetric two-half\n * contract. This module is the OUTBOUND (serialize) half for `bigint`; the entries\n * below are its counterparts. When editing any one, keep the pair coherent.\n *\n * | Concern | `Date` | money `bigint` |\n * |---|---|---|\n * | SERIALIZE (runtime type → wire string) — PRIMARY | `Date.prototype.toJSON` (built-in) | **this module** (`installBigIntJsonEncoding`) |\n * | COERCE (wire string → runtime type, on receive) | `z.date()` → `z_wireDate()` | `z.bigint()` → string-accepting `z.preprocess` |\n * | — both coerce leaves rewritten by | `ResourcesConfigOperationsDtosBuilderUtils.applyNetworkBoundaryCoercion` (`@wildo-ai/saas-models`) — used by the SHARED `responseDto`/`requestDto`, so backend serialize-parse, frontend receive-parse, and worker re-hydration all coerce identically | (same) |\n * | In-BODY pre-serialize normalization (backend) | `transformResponseDates` (Date→ISO string in the body) | **none by design** — bigint stays bigint in the body and relies on the SERIALIZE half above (a `transformResponseBigints` twin would be redundant with this `toJSON`) |\n * | DEFENSE-IN-DEPTH serialize (belt-and-suspenders) | (n/a — `toJSON` is built-in) | `ResourceSerializationUtils.bigIntSafeJsonReplacer` / `stringifyWithBigInt` (queue, webhook), and the Express app-level `json replacer` (`http-server.backend.service`) |\n *\n * Type origin: `MoneyValue.amount` (`z_money`) in this package's `zod-money.ts`.\n */\n\ndeclare global {\n // The type-level encoding installed by `installBigIntJsonEncoding`. Declared so\n // `BigInt.prototype.toJSON` is typed rather than accessed through `any`.\n interface BigInt {\n /** JSON representation of this bigint — its decimal string form (lossless). */\n toJSON(): string;\n }\n}\n\n/**\n * Install `BigInt.prototype.toJSON` (bigint → decimal string) if not already\n * present. Idempotent and safe to call from multiple bootstraps / duplicate\n * package instances — the first install wins and later calls are no-ops.\n */\nexport function installBigIntJsonEncoding(): void {\n if (typeof BigInt.prototype.toJSON === 'function') return;\n Object.defineProperty(BigInt.prototype, 'toJSON', {\n value: function toJSON(this: bigint): string {\n return this.toString();\n },\n writable: true,\n configurable: true,\n enumerable: false,\n });\n}\n"]}
|
|
@@ -9,19 +9,19 @@ import { z } from 'zod';
|
|
|
9
9
|
* Minimal “resource row” with primary key, summary, FK, and backend-only fields.
|
|
10
10
|
*/
|
|
11
11
|
export declare const decoratedTestEntitySchema: z.ZodObject<{
|
|
12
|
-
_id:
|
|
13
|
-
title:
|
|
14
|
-
ownerId:
|
|
15
|
-
internalNote:
|
|
12
|
+
_id: z.ZodString;
|
|
13
|
+
title: z.ZodString;
|
|
14
|
+
ownerId: z.ZodString;
|
|
15
|
+
internalNote: z.ZodString;
|
|
16
16
|
}, z.core.$strip>;
|
|
17
17
|
/**
|
|
18
18
|
* Variant-style row with a discriminator enum + summary field.
|
|
19
19
|
*/
|
|
20
20
|
export declare const decoratedTestVariantSchema: z.ZodObject<{
|
|
21
|
-
kind:
|
|
22
|
-
B: "B";
|
|
21
|
+
kind: z.ZodEnum<{
|
|
23
22
|
A: "A";
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
B: "B";
|
|
24
|
+
}>;
|
|
25
|
+
label: z.ZodString;
|
|
26
26
|
}, z.core.$strip>;
|
|
27
27
|
//# sourceMappingURL=decorated-test-schemas.d.ts.map
|
|
@@ -60,19 +60,27 @@ export declare const BigDecimalValueSchema: z.ZodString;
|
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
62
|
export declare function createBigDecimalSchema(options?: BigDecimalSchemaOptions): DecoratedBigDecimalSchema;
|
|
63
|
+
/**
|
|
64
|
+
* The chainable methods of `DecoratedBigDecimalSchema`, returning `this` so a chain of several decorations keeps
|
|
65
|
+
* the type it reached, whatever the receiver is (`typeof field` in a resource specification's field map).
|
|
66
|
+
* A method returning the bare `DecoratedBigDecimalSchema` once won the overload inside the intersection, so that
|
|
67
|
+
* field map rejected `schemaShape.x.<factory>Spec(...)` on a field decorated first. Every other decorator
|
|
68
|
+
* returns the receiver's own type too (#3031), so `.isSummaryField()` leaves the field a `DecoratedBigDecimalSchema`.
|
|
69
|
+
*/
|
|
70
|
+
export interface DecoratedBigDecimalSchemaMethods {
|
|
71
|
+
[ZodDecorator.IS_DB_INDEXED](): this;
|
|
72
|
+
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): this;
|
|
73
|
+
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): this;
|
|
74
|
+
[ZodDecorator.BIGDECIMAL_UI](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_UI]): this;
|
|
75
|
+
[ZodDecorator.BIGDECIMAL_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_SPEC]): this;
|
|
76
|
+
addDecorators(decorators: DecoratorInput[]): this;
|
|
77
|
+
}
|
|
63
78
|
/**
|
|
64
79
|
* Return type for bigdecimal schemas - includes decorator methods.
|
|
65
80
|
* Note: IS_FOREIGN_KEY is not applicable to bigdecimal values.
|
|
66
81
|
* IS_DB_INDEXED indexes the string value directly for query optimization.
|
|
67
82
|
*/
|
|
68
|
-
export type DecoratedBigDecimalSchema = z.ZodString &
|
|
69
|
-
[ZodDecorator.IS_DB_INDEXED](): DecoratedBigDecimalSchema;
|
|
70
|
-
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedBigDecimalSchema;
|
|
71
|
-
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedBigDecimalSchema;
|
|
72
|
-
[ZodDecorator.BIGDECIMAL_UI](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_UI]): DecoratedBigDecimalSchema;
|
|
73
|
-
[ZodDecorator.BIGDECIMAL_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_SPEC]): DecoratedBigDecimalSchema;
|
|
74
|
-
addDecorators(decorators: DecoratorInput[]): DecoratedBigDecimalSchema;
|
|
75
|
-
};
|
|
83
|
+
export type DecoratedBigDecimalSchema = z.ZodString & DecoratedBigDecimalSchemaMethods;
|
|
76
84
|
/**
|
|
77
85
|
* Creates a bigdecimal schema for precise decimal numbers stored as strings.
|
|
78
86
|
* Named z_bigdecimal to indicate it's a Zod helper function exported standalone.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod-bigdecimal.d.ts","sourceRoot":"","sources":["../../src/zod-bigdecimal.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAkE,uBAAuB,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACxL,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,GAAG,MAAM,QAAQ,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAcrC;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,CAAC,CAAC,SAGtB,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,yBAAyB,CAkE3B;AAED
|
|
1
|
+
{"version":3,"file":"zod-bigdecimal.d.ts","sourceRoot":"","sources":["../../src/zod-bigdecimal.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAkE,uBAAuB,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACxL,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,GAAG,MAAM,QAAQ,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC;AAcrC;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,CAAC,CAAC,SAGtB,CAAC;AAEjB;;;;;;;;;;;;;;;;;;GAkBG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,yBAAyB,CAkE3B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gCAAgC;IAC/C,CAAC,YAAY,CAAC,aAAa,CAAC,IAAI,IAAI,CAAC;IACrC,CAAC,YAAY,CAAC,SA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;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB;IACnD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC","sourcesContent":["\n// ============================================================================\n// BIGDECIMAL SCHEMA HELPER\n// ============================================================================\n\nimport { DecoratorInput, SCHEMA_METADATA, CustomFieldMetadata, BigDecimalSchemaMetadata, BigDecimalSchemaOptions, ZodDecorator, ZodDecorator_ParamsType } from \"./zod-decorators.types\";\nimport { z } from \"zod\";\nimport { addChainableMethods } from \"./zod.module-override\";\nimport Big from \"big.js\";\n\n/**\n * BigDecimal value type - represents a precise decimal number as a string.\n *\n * Using strings to store decimal values avoids JavaScript's floating-point\n * precision issues. The big.js library is used for precise arithmetic operations.\n *\n * Example: \"19.99\", \"0.00000001\", \"123456789.123456789\"\n */\nexport type BigDecimalValue = string;\n\n/**\n * Validates that a string represents a valid big decimal number.\n */\nfunction isValidBigDecimal(value: string): boolean {\n try {\n new Big(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Zod schema for BigDecimalValue.\n * Validates that the string represents a valid decimal number.\n */\nexport const BigDecimalValueSchema: z.ZodString = z.string().refine(\n isValidBigDecimal,\n { message: \"Invalid decimal number format\" }\n) as z.ZodString;\n\n/**\n * z_bigdecimal() - Creates bigdecimal schemas for precise decimal numbers.\n *\n * BigDecimal values are stored as strings to avoid floating-point precision issues.\n * The big.js library is used for arithmetic operations.\n *\n * @example\n * ```typescript\n * import { z_bigdecimal } from '@wildo-ai/zod-decorators';\n *\n * const price = z_bigdecimal();\n * const quantity = z_bigdecimal({ precision: 8 });\n * const percentage = z_bigdecimal({ min: '0', max: '100' });\n *\n * // With decorators (chainable)\n * z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })\n * z_bigdecimal({ precision: 2 }).isHidden({ edit: true })\n * ```\n */\n\n/**\n * @wildo_source:part:start zod.factories.bigdecimal facet:layer:shared facet:family:schema-factory\n *\n * Creates a bigdecimal schema with metadata for detection and form component rendering.\n *\n * @param options - Configuration options for bigdecimal field\n * @returns A Zod string schema with metadata for bigdecimal detection\n *\n * @example\n * ```typescript\n * // Simple bigdecimal\n * const price = z_bigdecimal();\n * // Infers: string\n *\n * // With precision constraint\n * const btcAmount = z_bigdecimal({ precision: 8 });\n *\n * // With min/max constraints\n * const percentage = z_bigdecimal({\n * min: '0',\n * max: '100',\n * precision: 2\n * });\n * ```\n */\nexport function createBigDecimalSchema(\n options: BigDecimalSchemaOptions = {}\n): DecoratedBigDecimalSchema {\n const {\n precision,\n min,\n max,\n } = options;\n\n // Create the base schema with validation\n let bigDecimalSchema = z.string().refine(\n (value) => {\n if (!isValidBigDecimal(value)) return false;\n\n try {\n const bigValue = new Big(value);\n\n // Check precision (decimal places)\n if (precision !== undefined) {\n const decimalPlaces = value.includes('.')\n ? value.split('.')[1]?.length ?? 0\n : 0;\n if (decimalPlaces > precision) return false;\n }\n\n // Check min\n if (min !== undefined) {\n const minValue = new Big(min);\n if (bigValue.lt(minValue)) return false;\n }\n\n // Check max\n if (max !== undefined) {\n const maxValue = new Big(max);\n if (bigValue.gt(maxValue)) return false;\n }\n\n return true;\n } catch {\n return false;\n }\n },\n {\n message: precision !== undefined\n ? `Invalid decimal number (max ${precision} decimal places${min !== undefined ? `, min: ${min}` : ''}${max !== undefined ? `, max: ${max}` : ''})`\n : `Invalid decimal number${min !== undefined ? ` (min: ${min}` : ''}${max !== undefined ? `${min !== undefined ? ', ' : ' ('}max: ${max})` : min !== undefined ? ')' : ''}`\n }\n );\n\n const bigDecimalMetadata: BigDecimalSchemaMetadata = {\n isBigDecimal: true,\n ...(precision !== undefined && { precision }),\n ...(min !== undefined && { min }),\n ...(max !== undefined && { max }),\n };\n\n // Initialize or extend SCHEMA_METADATA with bigdecimal config\n const existingMetadata: CustomFieldMetadata = (bigDecimalSchema as any)[SCHEMA_METADATA] || {};\n (bigDecimalSchema as any)[SCHEMA_METADATA] = {\n ...existingMetadata,\n bigDecimalConfig: bigDecimalMetadata,\n };\n\n // Add chainable decorator methods to the bigdecimal schema instance\n // This allows: z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } }).isHidden()\n addChainableMethods(bigDecimalSchema);\n\n return bigDecimalSchema as unknown as DecoratedBigDecimalSchema;\n}\n\n/**\n * Return type for bigdecimal schemas - includes decorator methods.\n * Note: IS_FOREIGN_KEY is not applicable to bigdecimal values.\n * IS_DB_INDEXED indexes the string value directly for query optimization.\n */\nexport type DecoratedBigDecimalSchema = z.ZodString & {\n [ZodDecorator.IS_DB_INDEXED](): DecoratedBigDecimalSchema;\n [ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedBigDecimalSchema;\n [ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedBigDecimalSchema;\n [ZodDecorator.BIGDECIMAL_UI](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_UI]): DecoratedBigDecimalSchema;\n [ZodDecorator.BIGDECIMAL_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_SPEC]): DecoratedBigDecimalSchema;\n addDecorators(decorators: DecoratorInput[]): DecoratedBigDecimalSchema;\n};\n\n/**\n * Creates a bigdecimal schema for precise decimal numbers stored as strings.\n * Named z_bigdecimal to indicate it's a Zod helper function exported standalone.\n *\n * BigDecimal values are stored as strings to avoid floating-point precision issues.\n * Use big.js for arithmetic operations on these values.\n *\n * @example\n * ```typescript\n * import { z_bigdecimal } from '@wildo-ai/zod-decorators';\n *\n * // Simple usage\n * z_bigdecimal() // Any valid decimal number as string\n *\n * // With options\n * z_bigdecimal({ precision: 2 }) // Max 2 decimal places\n * z_bigdecimal({ min: '0', max: '100' }) // Range constraint\n * z_bigdecimal({ precision: 8, min: '0' }) // Crypto amount\n *\n * // With decorators (chainable)\n * z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })\n * z_bigdecimal({ precision: 2 }).isHidden({ edit: true })\n *\n * // Usage example\n * const price: BigDecimalValue = \"19.99\";\n * const btcAmount: BigDecimalValue = \"0.00000001\";\n *\n * // Arithmetic with big.js\n * import Big from 'big.js';\n * const total = new Big(price).plus(\"10.01\").toString(); // \"30.00\"\n * ```\n */\nexport function z_bigdecimal(\n options?: BigDecimalSchemaOptions\n): DecoratedBigDecimalSchema {\n return createBigDecimalSchema(options) as DecoratedBigDecimalSchema;\n}\n/** @wildo_source:part:end zod.factories.bigdecimal */\n\n// ============================================================================\n// BIGDECIMAL UTILITY FUNCTIONS\n// ============================================================================\n\n/**\n * Add two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns Sum as string\n */\nexport function bigDecimalAdd(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).plus(b).toString();\n}\n\n/**\n * Subtract two bigdecimal values.\n * @param a - Value to subtract from\n * @param b - Value to subtract\n * @returns Difference as string\n */\nexport function bigDecimalSubtract(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).minus(b).toString();\n}\n\n/**\n * Multiply two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns Product as string\n */\nexport function bigDecimalMultiply(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).times(b).toString();\n}\n\n/**\n * Divide two bigdecimal values.\n * @param a - Dividend\n * @param b - Divisor\n * @param precision - Number of decimal places for result (default: 20)\n * @returns Quotient as string\n */\nexport function bigDecimalDivide(a: BigDecimalValue, b: BigDecimalValue, precision: number = 20): BigDecimalValue {\n return new Big(a).div(b).round(precision).toString();\n}\n\n/**\n * Compare two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns -1 if a < b, 0 if a === b, 1 if a > b\n */\nexport function bigDecimalCompare(a: BigDecimalValue, b: BigDecimalValue): -1 | 0 | 1 {\n return new Big(a).cmp(b) as -1 | 0 | 1;\n}\n\n/**\n * Round a bigdecimal value to specified precision.\n * @param value - Value to round\n * @param precision - Number of decimal places\n * @param roundingMode - big.js rounding mode (default: ROUND_HALF_UP)\n * @returns Rounded value as string\n */\nexport function bigDecimalRound(\n value: BigDecimalValue,\n precision: number,\n roundingMode: Big.RoundingMode = Big.roundHalfUp\n): BigDecimalValue {\n return new Big(value).round(precision, roundingMode).toString();\n}\n\n/**\n * Format a bigdecimal value for display with fixed decimal places.\n * @param value - Value to format\n * @param decimalPlaces - Number of decimal places to show\n * @returns Formatted string with trailing zeros if needed\n */\nexport function bigDecimalFormat(value: BigDecimalValue, decimalPlaces: number): string {\n return new Big(value).toFixed(decimalPlaces);\n}\n\n/**\n * Check if a bigdecimal value is zero.\n * @param value - Value to check\n * @returns true if value equals zero\n */\nexport function bigDecimalIsZero(value: BigDecimalValue): boolean {\n return new Big(value).eq(0);\n}\n\n/**\n * Check if a bigdecimal value is positive (> 0).\n * @param value - Value to check\n * @returns true if value is greater than zero\n */\nexport function bigDecimalIsPositive(value: BigDecimalValue): boolean {\n return new Big(value).gt(0);\n}\n\n/**\n * Check if a bigdecimal value is negative (< 0).\n * @param value - Value to check\n * @returns true if value is less than zero\n */\nexport function bigDecimalIsNegative(value: BigDecimalValue): boolean {\n return new Big(value).lt(0);\n}\n\n/**\n * Get the absolute value of a bigdecimal.\n * @param value - Value\n * @returns Absolute value as string\n */\nexport function bigDecimalAbs(value: BigDecimalValue): BigDecimalValue {\n return new Big(value).abs().toString();\n}\n\n/**\n * Convert a number to bigdecimal string.\n * @param value - Number to convert\n * @returns String representation\n */\nexport function toBigDecimal(value: number): BigDecimalValue {\n return new Big(value).toString();\n}\n\n/**\n * Convert a bigdecimal string to number.\n * Warning: May lose precision for very large or very precise numbers.\n * @param value - BigDecimal string\n * @returns Number value\n */\nexport function fromBigDecimal(value: BigDecimalValue): number {\n return new Big(value).toNumber();\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"zod-bigdecimal.js","sourceRoot":"","sources":["../../src/zod-bigdecimal.ts"],"names":[],"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K,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAsB;IAClD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAsB;IACnD,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;AACnC,CAAC","sourcesContent":["\n// ============================================================================\n// BIGDECIMAL SCHEMA HELPER\n// ============================================================================\n\nimport { DecoratorInput, SCHEMA_METADATA, CustomFieldMetadata, BigDecimalSchemaMetadata, BigDecimalSchemaOptions, ZodDecorator, ZodDecorator_ParamsType } from \"./zod-decorators.types\";\nimport { z } from \"zod\";\nimport { addChainableMethods } from \"./zod.module-override\";\nimport Big from \"big.js\";\n\n/**\n * BigDecimal value type - represents a precise decimal number as a string.\n *\n * Using strings to store decimal values avoids JavaScript's floating-point\n * precision issues. The big.js library is used for precise arithmetic operations.\n *\n * Example: \"19.99\", \"0.00000001\", \"123456789.123456789\"\n */\nexport type BigDecimalValue = string;\n\n/**\n * Validates that a string represents a valid big decimal number.\n */\nfunction isValidBigDecimal(value: string): boolean {\n try {\n new Big(value);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Zod schema for BigDecimalValue.\n * Validates that the string represents a valid decimal number.\n */\nexport const BigDecimalValueSchema: z.ZodString = z.string().refine(\n isValidBigDecimal,\n { message: \"Invalid decimal number format\" }\n) as z.ZodString;\n\n/**\n * z_bigdecimal() - Creates bigdecimal schemas for precise decimal numbers.\n *\n * BigDecimal values are stored as strings to avoid floating-point precision issues.\n * The big.js library is used for arithmetic operations.\n *\n * @example\n * ```typescript\n * import { z_bigdecimal } from '@wildo-ai/zod-decorators';\n *\n * const price = z_bigdecimal();\n * const quantity = z_bigdecimal({ precision: 8 });\n * const percentage = z_bigdecimal({ min: '0', max: '100' });\n *\n * // With decorators (chainable)\n * z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })\n * z_bigdecimal({ precision: 2 }).isHidden({ edit: true })\n * ```\n */\n\n/**\n * @wildo_source:part:start zod.factories.bigdecimal facet:layer:shared facet:family:schema-factory\n *\n * Creates a bigdecimal schema with metadata for detection and form component rendering.\n *\n * @param options - Configuration options for bigdecimal field\n * @returns A Zod string schema with metadata for bigdecimal detection\n *\n * @example\n * ```typescript\n * // Simple bigdecimal\n * const price = z_bigdecimal();\n * // Infers: string\n *\n * // With precision constraint\n * const btcAmount = z_bigdecimal({ precision: 8 });\n *\n * // With min/max constraints\n * const percentage = z_bigdecimal({\n * min: '0',\n * max: '100',\n * precision: 2\n * });\n * ```\n */\nexport function createBigDecimalSchema(\n options: BigDecimalSchemaOptions = {}\n): DecoratedBigDecimalSchema {\n const {\n precision,\n min,\n max,\n } = options;\n\n // Create the base schema with validation\n let bigDecimalSchema = z.string().refine(\n (value) => {\n if (!isValidBigDecimal(value)) return false;\n\n try {\n const bigValue = new Big(value);\n\n // Check precision (decimal places)\n if (precision !== undefined) {\n const decimalPlaces = value.includes('.')\n ? value.split('.')[1]?.length ?? 0\n : 0;\n if (decimalPlaces > precision) return false;\n }\n\n // Check min\n if (min !== undefined) {\n const minValue = new Big(min);\n if (bigValue.lt(minValue)) return false;\n }\n\n // Check max\n if (max !== undefined) {\n const maxValue = new Big(max);\n if (bigValue.gt(maxValue)) return false;\n }\n\n return true;\n } catch {\n return false;\n }\n },\n {\n message: precision !== undefined\n ? `Invalid decimal number (max ${precision} decimal places${min !== undefined ? `, min: ${min}` : ''}${max !== undefined ? `, max: ${max}` : ''})`\n : `Invalid decimal number${min !== undefined ? ` (min: ${min}` : ''}${max !== undefined ? `${min !== undefined ? ', ' : ' ('}max: ${max})` : min !== undefined ? ')' : ''}`\n }\n );\n\n const bigDecimalMetadata: BigDecimalSchemaMetadata = {\n isBigDecimal: true,\n ...(precision !== undefined && { precision }),\n ...(min !== undefined && { min }),\n ...(max !== undefined && { max }),\n };\n\n // Initialize or extend SCHEMA_METADATA with bigdecimal config\n const existingMetadata: CustomFieldMetadata = (bigDecimalSchema as any)[SCHEMA_METADATA] || {};\n (bigDecimalSchema as any)[SCHEMA_METADATA] = {\n ...existingMetadata,\n bigDecimalConfig: bigDecimalMetadata,\n };\n\n // Add chainable decorator methods to the bigdecimal schema instance\n // This allows: z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } }).isHidden()\n addChainableMethods(bigDecimalSchema);\n\n return bigDecimalSchema as unknown as DecoratedBigDecimalSchema;\n}\n\n/**\n * The chainable methods of `DecoratedBigDecimalSchema`, returning `this` so a chain of several decorations keeps\n * the type it reached, whatever the receiver is (`typeof field` in a resource specification's field map).\n * A method returning the bare `DecoratedBigDecimalSchema` once won the overload inside the intersection, so that\n * field map rejected `schemaShape.x.<factory>Spec(...)` on a field decorated first. Every other decorator\n * returns the receiver's own type too (#3031), so `.isSummaryField()` leaves the field a `DecoratedBigDecimalSchema`.\n */\nexport interface DecoratedBigDecimalSchemaMethods {\n [ZodDecorator.IS_DB_INDEXED](): this;\n [ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): this;\n [ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): this;\n [ZodDecorator.BIGDECIMAL_UI](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_UI]): this;\n [ZodDecorator.BIGDECIMAL_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.BIGDECIMAL_SPEC]): this;\n addDecorators(decorators: DecoratorInput[]): this;\n}\n\n/**\n * Return type for bigdecimal schemas - includes decorator methods.\n * Note: IS_FOREIGN_KEY is not applicable to bigdecimal values.\n * IS_DB_INDEXED indexes the string value directly for query optimization.\n */\nexport type DecoratedBigDecimalSchema = z.ZodString & DecoratedBigDecimalSchemaMethods;\n\n/**\n * Creates a bigdecimal schema for precise decimal numbers stored as strings.\n * Named z_bigdecimal to indicate it's a Zod helper function exported standalone.\n *\n * BigDecimal values are stored as strings to avoid floating-point precision issues.\n * Use big.js for arithmetic operations on these values.\n *\n * @example\n * ```typescript\n * import { z_bigdecimal } from '@wildo-ai/zod-decorators';\n *\n * // Simple usage\n * z_bigdecimal() // Any valid decimal number as string\n *\n * // With options\n * z_bigdecimal({ precision: 2 }) // Max 2 decimal places\n * z_bigdecimal({ min: '0', max: '100' }) // Range constraint\n * z_bigdecimal({ precision: 8, min: '0' }) // Crypto amount\n *\n * // With decorators (chainable)\n * z_bigdecimal().bigDecimalUI({ display: { decimalPlaces: 4 } })\n * z_bigdecimal({ precision: 2 }).isHidden({ edit: true })\n *\n * // Usage example\n * const price: BigDecimalValue = \"19.99\";\n * const btcAmount: BigDecimalValue = \"0.00000001\";\n *\n * // Arithmetic with big.js\n * import Big from 'big.js';\n * const total = new Big(price).plus(\"10.01\").toString(); // \"30.00\"\n * ```\n */\nexport function z_bigdecimal(\n options?: BigDecimalSchemaOptions\n): DecoratedBigDecimalSchema {\n return createBigDecimalSchema(options) as DecoratedBigDecimalSchema;\n}\n/** @wildo_source:part:end zod.factories.bigdecimal */\n\n// ============================================================================\n// BIGDECIMAL UTILITY FUNCTIONS\n// ============================================================================\n\n/**\n * Add two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns Sum as string\n */\nexport function bigDecimalAdd(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).plus(b).toString();\n}\n\n/**\n * Subtract two bigdecimal values.\n * @param a - Value to subtract from\n * @param b - Value to subtract\n * @returns Difference as string\n */\nexport function bigDecimalSubtract(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).minus(b).toString();\n}\n\n/**\n * Multiply two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns Product as string\n */\nexport function bigDecimalMultiply(a: BigDecimalValue, b: BigDecimalValue): BigDecimalValue {\n return new Big(a).times(b).toString();\n}\n\n/**\n * Divide two bigdecimal values.\n * @param a - Dividend\n * @param b - Divisor\n * @param precision - Number of decimal places for result (default: 20)\n * @returns Quotient as string\n */\nexport function bigDecimalDivide(a: BigDecimalValue, b: BigDecimalValue, precision: number = 20): BigDecimalValue {\n return new Big(a).div(b).round(precision).toString();\n}\n\n/**\n * Compare two bigdecimal values.\n * @param a - First value\n * @param b - Second value\n * @returns -1 if a < b, 0 if a === b, 1 if a > b\n */\nexport function bigDecimalCompare(a: BigDecimalValue, b: BigDecimalValue): -1 | 0 | 1 {\n return new Big(a).cmp(b) as -1 | 0 | 1;\n}\n\n/**\n * Round a bigdecimal value to specified precision.\n * @param value - Value to round\n * @param precision - Number of decimal places\n * @param roundingMode - big.js rounding mode (default: ROUND_HALF_UP)\n * @returns Rounded value as string\n */\nexport function bigDecimalRound(\n value: BigDecimalValue,\n precision: number,\n roundingMode: Big.RoundingMode = Big.roundHalfUp\n): BigDecimalValue {\n return new Big(value).round(precision, roundingMode).toString();\n}\n\n/**\n * Format a bigdecimal value for display with fixed decimal places.\n * @param value - Value to format\n * @param decimalPlaces - Number of decimal places to show\n * @returns Formatted string with trailing zeros if needed\n */\nexport function bigDecimalFormat(value: BigDecimalValue, decimalPlaces: number): string {\n return new Big(value).toFixed(decimalPlaces);\n}\n\n/**\n * Check if a bigdecimal value is zero.\n * @param value - Value to check\n * @returns true if value equals zero\n */\nexport function bigDecimalIsZero(value: BigDecimalValue): boolean {\n return new Big(value).eq(0);\n}\n\n/**\n * Check if a bigdecimal value is positive (> 0).\n * @param value - Value to check\n * @returns true if value is greater than zero\n */\nexport function bigDecimalIsPositive(value: BigDecimalValue): boolean {\n return new Big(value).gt(0);\n}\n\n/**\n * Check if a bigdecimal value is negative (< 0).\n * @param value - Value to check\n * @returns true if value is less than zero\n */\nexport function bigDecimalIsNegative(value: BigDecimalValue): boolean {\n return new Big(value).lt(0);\n}\n\n/**\n * Get the absolute value of a bigdecimal.\n * @param value - Value\n * @returns Absolute value as string\n */\nexport function bigDecimalAbs(value: BigDecimalValue): BigDecimalValue {\n return new Big(value).abs().toString();\n}\n\n/**\n * Convert a number to bigdecimal string.\n * @param value - Number to convert\n * @returns String representation\n */\nexport function toBigDecimal(value: number): BigDecimalValue {\n return new Big(value).toString();\n}\n\n/**\n * Convert a bigdecimal string to number.\n * Warning: May lose precision for very large or very precise numbers.\n * @param value - BigDecimal string\n * @returns Number value\n */\nexport function fromBigDecimal(value: BigDecimalValue): number {\n return new Big(value).toNumber();\n}\n\n"]}
|
package/dist/esm/zod-code.d.ts
CHANGED
|
@@ -48,6 +48,22 @@ export declare const CodeValueSchema: z.ZodObject<{
|
|
|
48
48
|
* @returns A Zod object schema with metadata for code detection
|
|
49
49
|
*/
|
|
50
50
|
export declare function createCodeSchema(options?: CodeSchemaOptions): DecoratedCodeSchema;
|
|
51
|
+
/**
|
|
52
|
+
* The chainable methods of `DecoratedCodeSchema`, returning `this` so a chain of several decorations keeps
|
|
53
|
+
* the type it reached, whatever the receiver is (`typeof field` in a resource specification's field map).
|
|
54
|
+
* A method returning the bare `DecoratedCodeSchema` once won the overload inside the intersection, so that
|
|
55
|
+
* field map rejected `schemaShape.x.<factory>Spec(...)` on a field decorated first. Every other decorator
|
|
56
|
+
* returns the receiver's own type too (#3031), so `.isSummaryField()` leaves the field a `DecoratedCodeSchema`.
|
|
57
|
+
*/
|
|
58
|
+
export interface DecoratedCodeSchemaMethods {
|
|
59
|
+
[ZodDecorator.IS_DB_INDEXED](): this;
|
|
60
|
+
/** Installed at runtime with the other chainable methods; declared here so an authored `.codeUI(…)` typechecks. */
|
|
61
|
+
[ZodDecorator.CODE_UI](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_UI]): this;
|
|
62
|
+
[ZodDecorator.CODE_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_SPEC]): this;
|
|
63
|
+
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): this;
|
|
64
|
+
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): this;
|
|
65
|
+
addDecorators(decorators: DecoratorInput[]): this;
|
|
66
|
+
}
|
|
51
67
|
/**
|
|
52
68
|
* Return type for code schemas - includes decorator methods.
|
|
53
69
|
* IS_DB_INDEXED will index internal fields (content, language) for query optimization.
|
|
@@ -55,15 +71,7 @@ export declare function createCodeSchema(options?: CodeSchemaOptions): Decorated
|
|
|
55
71
|
export type DecoratedCodeSchema = z.ZodObject<{
|
|
56
72
|
content: z.ZodString;
|
|
57
73
|
language: z.ZodString;
|
|
58
|
-
}> &
|
|
59
|
-
[ZodDecorator.IS_DB_INDEXED](): DecoratedCodeSchema;
|
|
60
|
-
/** Installed at runtime with the other chainable methods; declared here so an authored `.codeUI(…)` typechecks. */
|
|
61
|
-
[ZodDecorator.CODE_UI](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_UI]): DecoratedCodeSchema;
|
|
62
|
-
[ZodDecorator.CODE_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_SPEC]): DecoratedCodeSchema;
|
|
63
|
-
[ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedCodeSchema;
|
|
64
|
-
[ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedCodeSchema;
|
|
65
|
-
addDecorators(decorators: DecoratorInput[]): DecoratedCodeSchema;
|
|
66
|
-
};
|
|
74
|
+
}> & DecoratedCodeSchemaMethods;
|
|
67
75
|
/**
|
|
68
76
|
* Creates a code schema for code snippets with language information.
|
|
69
77
|
* Named z_code to indicate it's a Zod helper function exported standalone.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zod-code.d.ts","sourceRoot":"","sources":["../../src/zod-code.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAA4D,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAC1L,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,ySAOtB,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,CAAC,CAAC,SAAS,CAAC;IAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;IAAC,QAAQ,EAAE,CAAC,CAAC,SAAS,CAAA;CAAE,CAGvF,CAAC;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,iBAAsB,GAC9B,mBAAmB,CA6CrB;AAED
|
|
1
|
+
{"version":3,"file":"zod-code.d.ts","sourceRoot":"","sources":["../../src/zod-code.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAA4D,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAC1L,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,ySAOtB,CAAC;AAEX;;GAEG;AACH,eAAO,MAAM,eAAe,EAAE,CAAC,CAAC,SA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e,CAAC,CAAC;AACvD,CAAC","sourcesContent":["\n// ============================================================================\n// CODE SCHEMA HELPER\n// ============================================================================\n\nimport { DecoratorInput, SCHEMA_METADATA, CustomFieldMetadata, CodeSchemaMetadata, CodeSchemaOptions, CodeLanguage, ZodDecorator, ZodDecorator_ParamsType } from \"./zod-decorators.types\";\nimport { z } from \"zod\";\nimport { addChainableMethods } from \"./zod.module-override\";\n\n/**\n * Code value type - represents a code snippet with language information.\n */\nexport interface CodeValue {\n /** The code content */\n content: string;\n /** Programming language for syntax highlighting and validation */\n language: string;\n}\n\n/**\n * Common programming languages for code schema language field.\n * Can be extended with any string value.\n */\nexport const CommonCodeLanguages = [\n 'javascript', 'typescript', 'python', 'java', 'csharp', 'cpp', 'c',\n 'go', 'rust', 'ruby', 'php', 'swift', 'kotlin', 'scala',\n 'html', 'css', 'scss', 'less', 'json', 'yaml', 'xml', 'markdown',\n 'sql', 'graphql', 'bash', 'shell', 'powershell',\n 'dockerfile', 'nginx', 'apache',\n 'plaintext'\n] as const;\n\n/**\n * Zod schema for CodeValue.\n */\nexport const CodeValueSchema: z.ZodObject<{ content: z.ZodString; language: z.ZodString }> = z.object({\n content: z.string().describe(\"The code itself.\"),\n language: z.string().describe(\"The language the code is written in, for highlighting and validation.\"),\n});\n\n/**\n * @wildo_source:part:start zod.factories.code facet:layer:shared facet:family:schema-factory\n *\n * z_code() - Creates code schemas for code snippets with language.\n *\n * @example\n * ```typescript\n * import { z_code } from '@wildo-ai/zod-decorators';\n *\n * const snippet = z_code();\n * const restrictedCode = z_code({\n * allowedLanguages: ['javascript', 'typescript', 'python'],\n * maxLength: 10000\n * });\n *\n * // UI props are now set via .codeUI():\n * z_code().codeUI({ defaultLanguage: 'javascript', syntaxHighlighting: true })\n * z_code().codeUI({ display: { showLineNumbers: true }, edit: { showLineNumbers: false } })\n * ```\n */\n\n/**\n * Creates a code schema with metadata for detection and form component rendering.\n *\n * @param options - Configuration options for code field\n * @returns A Zod object schema with metadata for code detection\n */\nexport function createCodeSchema(\n options: CodeSchemaOptions = {}\n): DecoratedCodeSchema {\n const {\n allowedLanguages,\n maxLength,\n minLength,\n } = options;\n\n // Create the content schema with optional length constraints\n let contentSchema = z.string();\n if (minLength !== undefined) {\n contentSchema = contentSchema.min(minLength);\n }\n if (maxLength !== undefined) {\n contentSchema = contentSchema.max(maxLength);\n }\n\n // Language is stored as a string. Allowed languages restriction is enforced\n // through codeConfig.allowedLanguages metadata - UI components read this\n // to filter available options, same pattern as money currencies.\n const languageSchema = z.string();\n\n // Create the code object schema\n const codeSchema = z.object({\n content: contentSchema.describe(\"The code itself.\"),\n language: languageSchema.describe(\"The language the code is written in, for highlighting and validation.\"),\n });\n\n const codeMetadata: CodeSchemaMetadata = {\n isCode: true,\n ...(allowedLanguages !== undefined && { allowedLanguages }),\n ...(maxLength !== undefined && { maxLength }),\n ...(minLength !== undefined && { minLength }),\n };\n\n // Initialize or extend SCHEMA_METADATA with code config\n const existingMetadata: CustomFieldMetadata = (codeSchema as any)[SCHEMA_METADATA] || {};\n (codeSchema as any)[SCHEMA_METADATA] = {\n ...existingMetadata,\n codeConfig: codeMetadata,\n };\n\n // Add chainable decorator methods to the code schema instance\n addChainableMethods(codeSchema);\n\n return codeSchema as unknown as DecoratedCodeSchema;\n}\n\n/**\n * Return type for code schemas - includes decorator methods.\n * IS_DB_INDEXED will index internal fields (content, language) for query optimization.\n */\nexport type DecoratedCodeSchema = z.ZodObject<{\n content: z.ZodString;\n language: z.ZodString;\n}> & {\n [ZodDecorator.IS_DB_INDEXED](): DecoratedCodeSchema;\n /** Installed at runtime with the other chainable methods; declared here so an authored `.codeUI(…)` typechecks. */\n [ZodDecorator.CODE_UI](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_UI]): DecoratedCodeSchema;\n [ZodDecorator.CODE_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_SPEC]): DecoratedCodeSchema;\n [ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): DecoratedCodeSchema;\n [ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): DecoratedCodeSchema;\n addDecorators(decorators: DecoratorInput[]): DecoratedCodeSchema;\n};\n\n/**\n * Creates a code schema for code snippets with language information.\n * Named z_code to indicate it's a Zod helper function exported standalone.\n *\n * Code values are stored as: { content: string, language: string }\n * - content: The actual code text\n * - language: Programming language identifier for syntax highlighting\n *\n * @example\n * ```typescript\n * import { z_code } from '@wildo-ai/zod-decorators';\n *\n * // Simple usage\n * z_code() // All languages, default 'plaintext'\n *\n * // With options (structural only)\n * z_code({ allowedLanguages: ['javascript', 'typescript'] })\n * z_code({ maxLength: 5000 })\n *\n * // UI props via .codeUI()\n * z_code().codeUI({ defaultLanguage: 'typescript' })\n * z_code().codeUI({ display: { showLineNumbers: false } })\n * ```\n */\nexport function z_code(options?: CodeSchemaOptions): DecoratedCodeSchema {\n return createCodeSchema(options);\n}\n/** @wildo_source:part:end zod.factories.code */\n\n// ============================================================================\n// UTILITY FUNCTIONS\n// ============================================================================\n\n/**\n * Get the display name for a code language.\n */\nexport function getLanguageDisplayName(language: CodeLanguage): string {\n const displayNames: Record<string, string> = {\n javascript: 'JavaScript',\n typescript: 'TypeScript',\n python: 'Python',\n java: 'Java',\n csharp: 'C#',\n cpp: 'C++',\n c: 'C',\n go: 'Go',\n rust: 'Rust',\n ruby: 'Ruby',\n php: 'PHP',\n swift: 'Swift',\n kotlin: 'Kotlin',\n scala: 'Scala',\n html: 'HTML',\n css: 'CSS',\n scss: 'SCSS',\n less: 'Less',\n json: 'JSON',\n yaml: 'YAML',\n xml: 'XML',\n markdown: 'Markdown',\n sql: 'SQL',\n graphql: 'GraphQL',\n bash: 'Bash',\n shell: 'Shell',\n powershell: 'PowerShell',\n dockerfile: 'Dockerfile',\n nginx: 'Nginx',\n apache: 'Apache',\n plaintext: 'Plain Text',\n };\n\n return displayNames[language] || language;\n}\n\n/**\n * Check if a language is in the common languages list.\n */\nexport function isCommonLanguage(language: string): language is typeof CommonCodeLanguages[number] {\n return CommonCodeLanguages.includes(language as any);\n}\n\n"]}
|
|
1
|
+
{"version":3,"file":"zod-code.js","sourceRoot":"","sources":["../../src/zod-code.ts"],"names":[],"mappings":"AACA,+EAA+E;AAC/E,qBAAqB;AACrB,+EAA+E;AAE/E,OAAO,EAAkB,eAAe,EAA4E,YAAY,EAA2B,MAAM,wBAAwB,CAAC;AAC1L,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAY5D;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,GAAG;IAClE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO;IACvD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU;IAChE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY;IAC/C,YAAY,EAAE,OAAO,EAAE,QAAQ;IAC/B,WAAW;CACH,CAAC;AAEX;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAiE,CAAC,CAAC,MAAM,CAAC;IACpG,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,kBAAkB,CAAC;IAChD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,uEAAuE,CAAC;CACvG,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAA6B,EAAE;IAE/B,MAAM,EACJ,gBAAgB,EAChB,SAAS,EACT,SAAS,GAC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OAA2B;IAChD,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC;AACD,gDAAgD;AAEhD,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,QAAsB;IAC3D,MAAM,YAAY,GAA2B;QAC3C,UAAU,EAAE,YAAY;QACxB,UAAU,EAAE,YAAY;QACxB,MAAM,EAAE,QAAQ;QAChB,IAAI,EAAE,MAAM;QACZ,MAAM,EAAE,IAAI;QACZ,GAAG,EAAE,KAAK;QACV,CAAC,EAAE,GAAG;QACN,EAAE,EAAE,IAAI;QACR,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,KAAK;QACV,KAAK,EAAE,OAAO;QACd,MAAM,EAAE,QAAQ;QAChB,KAAK,EAAE,OAAO;QACd,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,KAAK;QACV,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,MAAM;QACZ,GAAG,EAAE,KAAK;QACV,QAAQ,EAAE,UAAU;QACpB,GAAG,EAAE,KAAK;QACV,OAAO,EAAE,SAAS;QAClB,IAAI,EAAE,MAAM;QACZ,KAAK,EAAE,OAAO;QACd,UAAU,EAAE,YAAY;QACxB,UAAU,EAAE,YAAY;QACxB,KAAK,EAAE,OAAO;QACd,MAAM,EAAE,QAAQ;QAChB,SAAS,EAAE,YAAY;KACxB,CAAC;IAEF,OAAO,YAAY,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC;AAC5C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAgB;IAC/C,OAAO,mBAAmB,CAAC,QAAQ,CAAC,QAAe,CAAC,CAAC;AACvD,CAAC","sourcesContent":["\n// ============================================================================\n// CODE SCHEMA HELPER\n// ============================================================================\n\nimport { DecoratorInput, SCHEMA_METADATA, CustomFieldMetadata, CodeSchemaMetadata, CodeSchemaOptions, CodeLanguage, ZodDecorator, ZodDecorator_ParamsType } from \"./zod-decorators.types\";\nimport { z } from \"zod\";\nimport { addChainableMethods } from \"./zod.module-override\";\n\n/**\n * Code value type - represents a code snippet with language information.\n */\nexport interface CodeValue {\n /** The code content */\n content: string;\n /** Programming language for syntax highlighting and validation */\n language: string;\n}\n\n/**\n * Common programming languages for code schema language field.\n * Can be extended with any string value.\n */\nexport const CommonCodeLanguages = [\n 'javascript', 'typescript', 'python', 'java', 'csharp', 'cpp', 'c',\n 'go', 'rust', 'ruby', 'php', 'swift', 'kotlin', 'scala',\n 'html', 'css', 'scss', 'less', 'json', 'yaml', 'xml', 'markdown',\n 'sql', 'graphql', 'bash', 'shell', 'powershell',\n 'dockerfile', 'nginx', 'apache',\n 'plaintext'\n] as const;\n\n/**\n * Zod schema for CodeValue.\n */\nexport const CodeValueSchema: z.ZodObject<{ content: z.ZodString; language: z.ZodString }> = z.object({\n content: z.string().describe(\"The code itself.\"),\n language: z.string().describe(\"The language the code is written in, for highlighting and validation.\"),\n});\n\n/**\n * @wildo_source:part:start zod.factories.code facet:layer:shared facet:family:schema-factory\n *\n * z_code() - Creates code schemas for code snippets with language.\n *\n * @example\n * ```typescript\n * import { z_code } from '@wildo-ai/zod-decorators';\n *\n * const snippet = z_code();\n * const restrictedCode = z_code({\n * allowedLanguages: ['javascript', 'typescript', 'python'],\n * maxLength: 10000\n * });\n *\n * // UI props are now set via .codeUI():\n * z_code().codeUI({ defaultLanguage: 'javascript', syntaxHighlighting: true })\n * z_code().codeUI({ display: { showLineNumbers: true }, edit: { showLineNumbers: false } })\n * ```\n */\n\n/**\n * Creates a code schema with metadata for detection and form component rendering.\n *\n * @param options - Configuration options for code field\n * @returns A Zod object schema with metadata for code detection\n */\nexport function createCodeSchema(\n options: CodeSchemaOptions = {}\n): DecoratedCodeSchema {\n const {\n allowedLanguages,\n maxLength,\n minLength,\n } = options;\n\n // Create the content schema with optional length constraints\n let contentSchema = z.string();\n if (minLength !== undefined) {\n contentSchema = contentSchema.min(minLength);\n }\n if (maxLength !== undefined) {\n contentSchema = contentSchema.max(maxLength);\n }\n\n // Language is stored as a string. Allowed languages restriction is enforced\n // through codeConfig.allowedLanguages metadata - UI components read this\n // to filter available options, same pattern as money currencies.\n const languageSchema = z.string();\n\n // Create the code object schema\n const codeSchema = z.object({\n content: contentSchema.describe(\"The code itself.\"),\n language: languageSchema.describe(\"The language the code is written in, for highlighting and validation.\"),\n });\n\n const codeMetadata: CodeSchemaMetadata = {\n isCode: true,\n ...(allowedLanguages !== undefined && { allowedLanguages }),\n ...(maxLength !== undefined && { maxLength }),\n ...(minLength !== undefined && { minLength }),\n };\n\n // Initialize or extend SCHEMA_METADATA with code config\n const existingMetadata: CustomFieldMetadata = (codeSchema as any)[SCHEMA_METADATA] || {};\n (codeSchema as any)[SCHEMA_METADATA] = {\n ...existingMetadata,\n codeConfig: codeMetadata,\n };\n\n // Add chainable decorator methods to the code schema instance\n addChainableMethods(codeSchema);\n\n return codeSchema as unknown as DecoratedCodeSchema;\n}\n\n/**\n * The chainable methods of `DecoratedCodeSchema`, returning `this` so a chain of several decorations keeps\n * the type it reached, whatever the receiver is (`typeof field` in a resource specification's field map).\n * A method returning the bare `DecoratedCodeSchema` once won the overload inside the intersection, so that\n * field map rejected `schemaShape.x.<factory>Spec(...)` on a field decorated first. Every other decorator\n * returns the receiver's own type too (#3031), so `.isSummaryField()` leaves the field a `DecoratedCodeSchema`.\n */\nexport interface DecoratedCodeSchemaMethods {\n [ZodDecorator.IS_DB_INDEXED](): this;\n /** Installed at runtime with the other chainable methods; declared here so an authored `.codeUI(…)` typechecks. */\n [ZodDecorator.CODE_UI](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_UI]): this;\n [ZodDecorator.CODE_SPEC](params?: ZodDecorator_ParamsType[ZodDecorator.CODE_SPEC]): this;\n [ZodDecorator.IS_HIDDEN](params?: ZodDecorator_ParamsType[ZodDecorator.IS_HIDDEN]): this;\n [ZodDecorator.IS_NOT_EDITABLE](params?: ZodDecorator_ParamsType[ZodDecorator.IS_NOT_EDITABLE]): this;\n addDecorators(decorators: DecoratorInput[]): this;\n}\n\n/**\n * Return type for code schemas - includes decorator methods.\n * IS_DB_INDEXED will index internal fields (content, language) for query optimization.\n */\nexport type DecoratedCodeSchema = z.ZodObject<{\n content: z.ZodString;\n language: z.ZodString;\n}> & DecoratedCodeSchemaMethods;\n\n/**\n * Creates a code schema for code snippets with language information.\n * Named z_code to indicate it's a Zod helper function exported standalone.\n *\n * Code values are stored as: { content: string, language: string }\n * - content: The actual code text\n * - language: Programming language identifier for syntax highlighting\n *\n * @example\n * ```typescript\n * import { z_code } from '@wildo-ai/zod-decorators';\n *\n * // Simple usage\n * z_code() // All languages, default 'plaintext'\n *\n * // With options (structural only)\n * z_code({ allowedLanguages: ['javascript', 'typescript'] })\n * z_code({ maxLength: 5000 })\n *\n * // UI props via .codeUI()\n * z_code().codeUI({ defaultLanguage: 'typescript' })\n * z_code().codeUI({ display: { showLineNumbers: false } })\n * ```\n */\nexport function z_code(options?: CodeSchemaOptions): DecoratedCodeSchema {\n return createCodeSchema(options);\n}\n/** @wildo_source:part:end zod.factories.code */\n\n// ============================================================================\n// UTILITY FUNCTIONS\n// ============================================================================\n\n/**\n * Get the display name for a code language.\n */\nexport function getLanguageDisplayName(language: CodeLanguage): string {\n const displayNames: Record<string, string> = {\n javascript: 'JavaScript',\n typescript: 'TypeScript',\n python: 'Python',\n java: 'Java',\n csharp: 'C#',\n cpp: 'C++',\n c: 'C',\n go: 'Go',\n rust: 'Rust',\n ruby: 'Ruby',\n php: 'PHP',\n swift: 'Swift',\n kotlin: 'Kotlin',\n scala: 'Scala',\n html: 'HTML',\n css: 'CSS',\n scss: 'SCSS',\n less: 'Less',\n json: 'JSON',\n yaml: 'YAML',\n xml: 'XML',\n markdown: 'Markdown',\n sql: 'SQL',\n graphql: 'GraphQL',\n bash: 'Bash',\n shell: 'Shell',\n powershell: 'PowerShell',\n dockerfile: 'Dockerfile',\n nginx: 'Nginx',\n apache: 'Apache',\n plaintext: 'Plain Text',\n };\n\n return displayNames[language] || language;\n}\n\n/**\n * Check if a language is in the common languages list.\n */\nexport function isCommonLanguage(language: string): language is typeof CommonCodeLanguages[number] {\n return CommonCodeLanguages.includes(language as any);\n}\n\n"]}
|