@zipbul/baker 2.1.0 → 2.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.
@@ -9,6 +9,10 @@ export interface EmitContext {
9
9
  fail(code: string): string;
10
10
  /** Whether error collection mode is enabled (= !stopAtFirstError) */
11
11
  collectErrors: boolean;
12
+ /** Whether this emit runs inside a type gate (typeof/instanceof already verified) */
13
+ insideTypeGate?: boolean;
14
+ /** @internal Path expression for inline nested — used by makeRuleEmitCtx */
15
+ _pathExpr?: string;
12
16
  }
13
17
  export interface EmittableRule {
14
18
  (value: unknown): boolean | Promise<boolean>;
@@ -19,12 +23,43 @@ export interface EmittableRule {
19
23
  * Only set for rules that assume a specific type (e.g., isEmail → 'string').
20
24
  * @IsString itself is undefined (it includes its own typeof check).
21
25
  */
22
- readonly requiresType?: 'string' | 'number' | 'boolean' | 'date';
26
+ readonly requiresType?: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object';
23
27
  /** Expose rule parameters for external reading */
24
28
  readonly constraints?: Record<string, unknown>;
25
- /** true when using an async validate function deserialize-builder generates await code */
29
+ /** true when the rule is explicitly async and must be awaited */
26
30
  readonly isAsync?: boolean;
27
31
  }
32
+ /** @internal internal rule shape used by builders for optimization metadata */
33
+ export interface InternalRule extends EmittableRule {
34
+ readonly plan?: RulePlan;
35
+ }
36
+ export type RulePlanExpr = {
37
+ kind: 'value';
38
+ } | {
39
+ kind: 'member';
40
+ object: RulePlanExpr;
41
+ property: 'length';
42
+ } | {
43
+ kind: 'call0';
44
+ object: RulePlanExpr;
45
+ method: 'getTime';
46
+ } | {
47
+ kind: 'literal';
48
+ value: number;
49
+ };
50
+ export type RulePlanCheck = {
51
+ kind: 'compare';
52
+ left: RulePlanExpr;
53
+ op: '<' | '<=' | '>' | '>=' | '===' | '!==';
54
+ right: RulePlanExpr;
55
+ } | {
56
+ kind: 'and' | 'or';
57
+ checks: RulePlanCheck[];
58
+ };
59
+ export interface RulePlan {
60
+ cacheKey?: 'length' | 'time';
61
+ failure: RulePlanCheck;
62
+ }
28
63
  /** Arguments for user-defined message callback */
29
64
  export interface MessageArgs {
30
65
  property: string;
@@ -32,7 +67,7 @@ export interface MessageArgs {
32
67
  constraints: Record<string, unknown>;
33
68
  }
34
69
  export interface RuleDef {
35
- rule: EmittableRule;
70
+ rule: InternalRule;
36
71
  each?: boolean;
37
72
  groups?: string[];
38
73
  /** Value to include in BakerError.message on validation failure */
@@ -46,13 +81,14 @@ export interface TransformParams {
46
81
  obj: Record<string, unknown>;
47
82
  }
48
83
  export interface Transformer {
49
- deserialize(params: TransformParams): unknown;
50
- serialize(params: TransformParams): unknown;
84
+ deserialize(params: TransformParams): unknown | Promise<unknown>;
85
+ serialize(params: TransformParams): unknown | Promise<unknown>;
51
86
  }
52
87
  /** Internal — direction-specific transform function stored after @Field processing */
53
- export type TransformFunction = (params: TransformParams) => unknown;
88
+ export type TransformFunction = (params: TransformParams) => unknown | Promise<unknown>;
54
89
  export interface TransformDef {
55
90
  fn: TransformFunction;
91
+ isAsync?: boolean;
56
92
  options?: {
57
93
  groups?: string[];
58
94
  deserializeOnly?: boolean;
@@ -123,6 +159,8 @@ export interface SealedExecutors<T> {
123
159
  _deserialize(input: unknown, options?: RuntimeOptions): Result<T, BakerError[]> | ResultAsync<T, BakerError[]>;
124
160
  /** Internal executor — always succeeds. serialize assumes no validation */
125
161
  _serialize(instance: T, options?: RuntimeOptions): Record<string, unknown> | Promise<Record<string, unknown>>;
162
+ /** Internal executor — validate-only (no object creation). Returns null on success, BakerError[] on failure */
163
+ _validate(input: unknown, options?: RuntimeOptions): BakerError[] | null | Promise<BakerError[] | null>;
126
164
  /** true if the deserialize direction has async rules/transforms/nested */
127
165
  _isAsync: boolean;
128
166
  /** true if the serialize direction has async transforms/nested */
@@ -1,2 +1,4 @@
1
1
  /** minification-safe async function detection (uses Symbol.toStringTag, not constructor.name) */
2
2
  export declare function isAsyncFunction(fn: Function): boolean;
3
+ /** Promise-like detection used to enforce sync/async contract at runtime */
4
+ export declare function isPromiseLike(value: unknown): value is PromiseLike<unknown>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zipbul/baker",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Fastest decorator-based DTO validation for TypeScript. AOT code generation, 42ns per validation, 163x faster than class-validator. Zero reflect-metadata.",
5
5
  "license": "MIT",
6
6
  "author": "Junhyung Park (https://github.com/parkrevil)",
@@ -1,3 +0,0 @@
1
- // @bun
2
- import{d as v}from"./index-fnv35wrf.js";var z=new Set;function B(h,w){if(!Object.prototype.hasOwnProperty.call(h,v))h[v]=Object.create(null),z.add(h);let G=h[v];return G[w]??={validation:[],transform:[],expose:[],exclude:null,type:null,flags:{}}}var H=Symbol.for("baker:arrayOf");function T(...h){let w={rules:h};return w[H]=!0,w}function J(h){return typeof h==="object"&&h!==null&&h[H]===!0}var U=new Set(["type","discriminator","keepDiscriminatorProperty","rules","optional","nullable","name","deserializeName","serializeName","exclude","groups","when","transform","message","context","mapValue","setValue"]);function C(h){if(typeof h==="function")return!1;if(typeof h!=="object"||h===null)return!1;if(J(h))return!1;let w=Object.keys(h);if(w.length===0)return!0;return w.some((G)=>U.has(G))}function X(h){if(h.length===0)return{rules:[],options:{}};if(h.length===1&&C(h[0])){let G=h[0];return{rules:G.rules??[],options:G}}let w=h[h.length-1];if(C(w)){let G=w,j=h.slice(0,-1);if(G.rules)j=[...j,...G.rules];return{rules:j,options:G}}return{rules:h,options:{}}}function Z(h,w,G){for(let j of w)if(J(j))for(let q of j.rules){let b={rule:q,each:!0,groups:G.groups};if(G.message!==void 0)b.message=G.message;if(G.context!==void 0)b.context=G.context;h.validation.push(b)}else{let q={rule:j,groups:G.groups};if(G.message!==void 0)q.message=G.message;if(G.context!==void 0)q.context=G.context;h.validation.push(q)}}function $(h,w){if(w.name)h.expose.push({name:w.name,groups:w.groups});else if(w.deserializeName||w.serializeName){if(w.deserializeName)h.expose.push({name:w.deserializeName,deserializeOnly:!0,groups:w.groups});if(w.serializeName)h.expose.push({name:w.serializeName,serializeOnly:!0,groups:w.groups})}else if(w.groups)h.expose.push({groups:w.groups});else h.expose.push({})}function K(h,w){if(!w.transform)return;let G=Array.isArray(w.transform)?w.transform:[w.transform];for(let j of G)h.transform.push({fn:j.deserialize,options:{deserializeOnly:!0}},{fn:j.serialize,options:{serializeOnly:!0}})}function E(...h){return(w,G)=>{let j=w.constructor,b=B(j,G),{rules:Q,options:S}=X(h);if(Z(b,Q,S),S.optional)b.flags.isOptional=!0;if(S.nullable)b.flags.isNullable=!0;if(S.when)b.flags.validateIf=S.when;if(S.type)b.type={fn:S.type,discriminator:S.discriminator,keepDiscriminatorProperty:S.keepDiscriminatorProperty,collectionValue:S.mapValue??S.setValue};if($(b,S),S.exclude){if(S.exclude===!0)b.exclude={};else if(S.exclude==="deserializeOnly")b.exclude={deserializeOnly:!0};else if(S.exclude==="serializeOnly")b.exclude={serializeOnly:!0}}K(b,S)}}
3
- export{z as a,T as b,E as c};