zod-compiler 0.1.0 → 0.1.1

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/index.d.ts CHANGED
@@ -465,6 +465,9 @@ interface StandaloneOutput {
465
465
  /** `true` if this parser has any dependencies. */
466
466
  readonly hasDependencies: boolean;
467
467
  }
468
+ interface CompiledNonStandaloneParser<TZod extends z.ZodTypeAny> extends CompiledParser<TZod['_output']> {
469
+ schema: TZod;
470
+ }
468
471
  /**
469
472
  * Compile a Zod schema for use out-of-source.
470
473
  *
@@ -499,8 +502,8 @@ declare function compile<TZod extends z.ZodTypeAny>(schema: TZod, options: Compi
499
502
  * console.log(fastSchema.safeParse('Hello, world!')); // { success: true, data: ... }
500
503
  * ```
501
504
  */
502
- declare function compile<TZod extends z.ZodTypeAny>(schema: TZod, options?: CompileOptions<false>): CompiledParser<TZod['_output']>;
503
- declare function compile<TZod extends z.ZodTypeAny>(schema: TZod, options?: CompileOptions<boolean>): StandaloneOutput | CompiledParser<TZod['_output']>;
505
+ declare function compile<TZod extends z.ZodTypeAny>(schema: TZod, options?: CompileOptions<false>): CompiledNonStandaloneParser<TZod>;
506
+ declare function compile<TZod extends z.ZodTypeAny>(schema: TZod, options?: CompileOptions<boolean>): StandaloneOutput | CompiledNonStandaloneParser<TZod>;
504
507
  interface TypesOptions {
505
508
  /**
506
509
  * Output the schema as a type alias in the form of `export type Schema = ...` (where `Schema` is configurable via
@@ -559,6 +562,7 @@ declare function types<TZod extends z.ZodTypeAny>(schema: TZod, options?: TypesO
559
562
  type zc_AbstractCompiledType<TZod extends z.ZodType> = AbstractCompiledType<TZod>;
560
563
  declare const zc_AbstractCompiledType: typeof AbstractCompiledType;
561
564
  type zc_CompileOptions<Standalone extends boolean = false> = CompileOptions<Standalone>;
565
+ type zc_CompiledNonStandaloneParser<TZod extends z.ZodTypeAny> = CompiledNonStandaloneParser<TZod>;
562
566
  declare const zc_CompiledParser: typeof CompiledParser;
563
567
  type zc_DenormalizedError = DenormalizedError;
564
568
  type zc_ErrorMapCtx = ErrorMapCtx;
@@ -663,8 +667,8 @@ type zc_typeToFlattenedError<T, U = string> = typeToFlattenedError<T, U>;
663
667
  declare const zc_types: typeof types;
664
668
  declare namespace zc {
665
669
  export { zc_AbstractCompiledType as AbstractCompiledType, zc_CompiledParser as CompiledParser, zc_InliningMode as InliningMode, zc_ZcAny as ZcAny, zc_ZcArray as ZcArray, zc_ZcBigInt as ZcBigInt, zc_ZcBoolean as ZcBoolean, zc_ZcBranded as ZcBranded, zc_ZcCatch as ZcCatch, zc_ZcDate as ZcDate, zc_ZcDefault as ZcDefault, zc_ZcDiscriminatedUnion as ZcDiscriminatedUnion, zc_ZcEnum as ZcEnum, zc_ZcError as ZcError, zc_ZcIntersection as ZcIntersection, zc_ZcIssueCode as ZcIssueCode, zc_ZcLiteral as ZcLiteral, zc_ZcMap as ZcMap, zc_ZcNaN as ZcNaN, zc_ZcNativeEnum as ZcNativeEnum, zc_ZcNever as ZcNever, zc_ZcNull as ZcNull, zc_ZcNullable as ZcNullable, zc_ZcNumber as ZcNumber, zc_ZcObject as ZcObject, zc_ZcOptional as ZcOptional, zc_ZcReadonly as ZcReadonly, zc_ZcRecord as ZcRecord, zc_ZcSet as ZcSet, zc_ZcString as ZcString, zc_ZcSymbol as ZcSymbol, zc_ZcTuple as ZcTuple, zc_ZcUndefined as ZcUndefined, zc_ZcUnion as ZcUnion, zc_ZcUnknown as ZcUnknown, zc_ZcVoid as ZcVoid, zc_compilable as compilable, zc_compile as compile, zc_types as types };
666
- export type { zc_CompileOptions as CompileOptions, zc_DenormalizedError as DenormalizedError, zc_ErrorMapCtx as ErrorMapCtx, zc_IssueData as IssueData, zc_Primitive as Primitive, zc_Scalars as Scalars, zc_StandaloneOutput as StandaloneOutput, zc_StringValidation as StringValidation, zc_TypesOptions as TypesOptions, zc_ZcCustomIssue as ZcCustomIssue, zc_ZcErrorMap as ZcErrorMap, zc_ZcFormattedError as ZcFormattedError, zc_ZcInvalidArgumentsIssue as ZcInvalidArgumentsIssue, zc_ZcInvalidDateIssue as ZcInvalidDateIssue, zc_ZcInvalidEnumValueIssue as ZcInvalidEnumValueIssue, zc_ZcInvalidIntersectionTypesIssue as ZcInvalidIntersectionTypesIssue, zc_ZcInvalidLiteralIssue as ZcInvalidLiteralIssue, zc_ZcInvalidReturnTypeIssue as ZcInvalidReturnTypeIssue, zc_ZcInvalidStringIssue as ZcInvalidStringIssue, zc_ZcInvalidTypeIssue as ZcInvalidTypeIssue, zc_ZcInvalidUnionDiscriminatorIssue as ZcInvalidUnionDiscriminatorIssue, zc_ZcInvalidUnionIssue as ZcInvalidUnionIssue, zc_ZcIssue as ZcIssue, zc_ZcIssueBase as ZcIssueBase, zc_ZcIssueOptionalMessage as ZcIssueOptionalMessage, zc_ZcNotFiniteIssue as ZcNotFiniteIssue, zc_ZcNotMultipleOfIssue as ZcNotMultipleOfIssue, zc_ZcTooBigIssue as ZcTooBigIssue, zc_ZcTooSmallIssue as ZcTooSmallIssue, zc_ZcUnrecognizedKeysIssue as ZcUnrecognizedKeysIssue, zc_typeToFlattenedError as typeToFlattenedError };
670
+ export type { zc_CompileOptions as CompileOptions, zc_CompiledNonStandaloneParser as CompiledNonStandaloneParser, zc_DenormalizedError as DenormalizedError, zc_ErrorMapCtx as ErrorMapCtx, zc_IssueData as IssueData, zc_Primitive as Primitive, zc_Scalars as Scalars, zc_StandaloneOutput as StandaloneOutput, zc_StringValidation as StringValidation, zc_TypesOptions as TypesOptions, zc_ZcCustomIssue as ZcCustomIssue, zc_ZcErrorMap as ZcErrorMap, zc_ZcFormattedError as ZcFormattedError, zc_ZcInvalidArgumentsIssue as ZcInvalidArgumentsIssue, zc_ZcInvalidDateIssue as ZcInvalidDateIssue, zc_ZcInvalidEnumValueIssue as ZcInvalidEnumValueIssue, zc_ZcInvalidIntersectionTypesIssue as ZcInvalidIntersectionTypesIssue, zc_ZcInvalidLiteralIssue as ZcInvalidLiteralIssue, zc_ZcInvalidReturnTypeIssue as ZcInvalidReturnTypeIssue, zc_ZcInvalidStringIssue as ZcInvalidStringIssue, zc_ZcInvalidTypeIssue as ZcInvalidTypeIssue, zc_ZcInvalidUnionDiscriminatorIssue as ZcInvalidUnionDiscriminatorIssue, zc_ZcInvalidUnionIssue as ZcInvalidUnionIssue, zc_ZcIssue as ZcIssue, zc_ZcIssueBase as ZcIssueBase, zc_ZcIssueOptionalMessage as ZcIssueOptionalMessage, zc_ZcNotFiniteIssue as ZcNotFiniteIssue, zc_ZcNotMultipleOfIssue as ZcNotMultipleOfIssue, zc_ZcTooBigIssue as ZcTooBigIssue, zc_ZcTooSmallIssue as ZcTooSmallIssue, zc_ZcUnrecognizedKeysIssue as ZcUnrecognizedKeysIssue, zc_typeToFlattenedError as typeToFlattenedError };
667
671
  }
668
672
 
669
673
  export { AbstractCompiledType, InliningMode, ZcAny, ZcArray, ZcBigInt, ZcBoolean, ZcBranded, ZcCatch, ZcDate, ZcDefault, ZcDiscriminatedUnion, ZcEnum, ZcError, ZcIntersection, ZcIssueCode, ZcLiteral, ZcMap, ZcNaN, ZcNativeEnum, ZcNever, ZcNull, ZcNullable, ZcNumber, ZcObject, ZcOptional, ZcReadonly, ZcRecord, ZcSet, ZcString, ZcSymbol, ZcTuple, ZcUndefined, ZcUnion, ZcUnknown, ZcVoid, compilable, compile, zc as default, types };
670
- export type { CompileOptions, DenormalizedError, ErrorMapCtx, IssueData, Primitive, Scalars, StandaloneOutput, StringValidation, TypesOptions, ZcCustomIssue, ZcErrorMap, ZcFormattedError, ZcInvalidArgumentsIssue, ZcInvalidDateIssue, ZcInvalidEnumValueIssue, ZcInvalidIntersectionTypesIssue, ZcInvalidLiteralIssue, ZcInvalidReturnTypeIssue, ZcInvalidStringIssue, ZcInvalidTypeIssue, ZcInvalidUnionDiscriminatorIssue, ZcInvalidUnionIssue, ZcIssue, ZcIssueBase, ZcIssueOptionalMessage, ZcNotFiniteIssue, ZcNotMultipleOfIssue, ZcTooBigIssue, ZcTooSmallIssue, ZcUnrecognizedKeysIssue, typeToFlattenedError };
674
+ export type { CompileOptions, CompiledNonStandaloneParser, DenormalizedError, ErrorMapCtx, IssueData, Primitive, Scalars, StandaloneOutput, StringValidation, TypesOptions, ZcCustomIssue, ZcErrorMap, ZcFormattedError, ZcInvalidArgumentsIssue, ZcInvalidDateIssue, ZcInvalidEnumValueIssue, ZcInvalidIntersectionTypesIssue, ZcInvalidLiteralIssue, ZcInvalidReturnTypeIssue, ZcInvalidStringIssue, ZcInvalidTypeIssue, ZcInvalidUnionDiscriminatorIssue, ZcInvalidUnionIssue, ZcIssue, ZcIssueBase, ZcIssueOptionalMessage, ZcNotFiniteIssue, ZcNotMultipleOfIssue, ZcTooBigIssue, ZcTooSmallIssue, ZcUnrecognizedKeysIssue, typeToFlattenedError };
package/dist/index.js CHANGED
@@ -2473,7 +2473,10 @@ function compile(schema, options) {
2473
2473
  sourceFile.identifiers = new Map();
2474
2474
  const source = print(sourceFile);
2475
2475
  const parser = new Function('input', 'ctx', source);
2476
- return standalone__default.default(parser, dependencies.dependencies);
2476
+ return {
2477
+ ...standalone__default.default(parser, dependencies.dependencies),
2478
+ schema
2479
+ };
2477
2480
  }
2478
2481
  /**
2479
2482
  * Exports the `schema` to a TypeScript type definition.
package/dist/index.mjs CHANGED
@@ -2465,7 +2465,10 @@ function compile(schema, options) {
2465
2465
  sourceFile.identifiers = new Map();
2466
2466
  const source = print(sourceFile);
2467
2467
  const parser = new Function('input', 'ctx', source);
2468
- return standalone(parser, dependencies.dependencies);
2468
+ return {
2469
+ ...standalone(parser, dependencies.dependencies),
2470
+ schema
2471
+ };
2469
2472
  }
2470
2473
  /**
2471
2474
  * Exports the `schema` to a TypeScript type definition.
@@ -246,6 +246,98 @@ declare namespace regex {
246
246
  };
247
247
  }
248
248
 
249
+ /**
250
+ * The Standard Schema interface.
251
+ */
252
+ type StandardSchemaV1<Input = unknown, Output = Input> = {
253
+ /**
254
+ * The Standard Schema properties.
255
+ */
256
+ readonly '~standard': StandardSchemaV1Props<Input, Output>;
257
+ };
258
+ /**
259
+ * The Standard Schema properties interface.
260
+ */
261
+ interface StandardSchemaV1Props<Input = unknown, Output = Input> {
262
+ /**
263
+ * The version number of the standard.
264
+ */
265
+ readonly version: 1;
266
+ /**
267
+ * The vendor name of the schema library.
268
+ */
269
+ readonly vendor: string;
270
+ /**
271
+ * Validates unknown input values.
272
+ */
273
+ readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>;
274
+ /**
275
+ * Inferred types associated with the schema.
276
+ */
277
+ readonly types?: StandardSchemaV1Types<Input, Output> | undefined;
278
+ }
279
+ /**
280
+ * The result interface of the validate function.
281
+ */
282
+ type StandardSchemaV1Result<Output> = StandardSchemaV1SuccessResult<Output> | StandardSchemaV1FailureResult;
283
+ /**
284
+ * The result interface if validation succeeds.
285
+ */
286
+ interface StandardSchemaV1SuccessResult<Output> {
287
+ /**
288
+ * The typed output value.
289
+ */
290
+ readonly value: Output;
291
+ /**
292
+ * The non-existent issues.
293
+ */
294
+ readonly issues?: undefined;
295
+ }
296
+ /**
297
+ * The result interface if validation fails.
298
+ */
299
+ interface StandardSchemaV1FailureResult {
300
+ /**
301
+ * The issues of failed validation.
302
+ */
303
+ readonly issues: ReadonlyArray<StandardSchemaV1Issue>;
304
+ }
305
+ /**
306
+ * The issue interface of the failure output.
307
+ */
308
+ interface StandardSchemaV1Issue {
309
+ /**
310
+ * The error message of the issue.
311
+ */
312
+ readonly message: string;
313
+ /**
314
+ * The path of the issue, if any.
315
+ */
316
+ readonly path?: ReadonlyArray<PropertyKey | StandardSchemaV1PathSegment> | undefined;
317
+ }
318
+ /**
319
+ * The path segment interface of the issue.
320
+ */
321
+ interface StandardSchemaV1PathSegment {
322
+ /**
323
+ * The key representing a path segment.
324
+ */
325
+ readonly key: PropertyKey;
326
+ }
327
+ /**
328
+ * The Standard Schema types interface.
329
+ */
330
+ interface StandardSchemaV1Types<Input = unknown, Output = Input> {
331
+ /**
332
+ * The input type of the schema.
333
+ */
334
+ readonly input: Input;
335
+ /**
336
+ * The output type of the schema.
337
+ */
338
+ readonly output: Output;
339
+ }
340
+
249
341
  declare const errorMap: ZcErrorMap;
250
342
 
251
343
  declare function setErrorMap(map: ZcErrorMap): void;
@@ -283,7 +375,7 @@ interface ParseParams {
283
375
  path?: (string | number)[];
284
376
  errorMap?: ZcErrorMap;
285
377
  }
286
- interface CompiledParser<T> {
378
+ interface CompiledParser<T> extends StandardSchemaV1<unknown, T> {
287
379
  parse(data: unknown, params?: ParseParams): T;
288
380
  safeParse(data: unknown, params?: ParseParams): SafeParseSuccess<T> | SafeParseError<T>;
289
381
  }
@@ -509,7 +509,24 @@ function standalone(parser, dependencies = []) {
509
509
  error: new ZcError(ctx.issues)
510
510
  };
511
511
  }
512
- }
512
+ },
513
+ '~standard': Object.freeze({
514
+ version: 1,
515
+ vendor: 'zod-compiler',
516
+ validate (value) {
517
+ const ctx = createContext(dependencies, undefined);
518
+ const status = typedParser(value, ctx);
519
+ if (status === 0) {
520
+ return {
521
+ value: ctx.output
522
+ };
523
+ } else {
524
+ return {
525
+ issues: ctx.issues
526
+ };
527
+ }
528
+ }
529
+ })
513
530
  };
514
531
  }
515
532
 
@@ -507,7 +507,24 @@ function standalone(parser, dependencies = []) {
507
507
  error: new ZcError(ctx.issues)
508
508
  };
509
509
  }
510
- }
510
+ },
511
+ '~standard': Object.freeze({
512
+ version: 1,
513
+ vendor: 'zod-compiler',
514
+ validate (value) {
515
+ const ctx = createContext(dependencies, undefined);
516
+ const status = typedParser(value, ctx);
517
+ if (status === 0) {
518
+ return {
519
+ value: ctx.output
520
+ };
521
+ } else {
522
+ return {
523
+ issues: ctx.issues
524
+ };
525
+ }
526
+ }
527
+ })
511
528
  };
512
529
  }
513
530
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-compiler",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Compile Zod schemas to fast parsers or TypeScript types",
5
5
  "author": "Carson M. <carson@pyke.io>",
6
6
  "license": "MIT OR Apache-2.0",