@quentinadam/zod 0.1.8 → 0.1.10

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/zod.d.ts CHANGED
@@ -47,6 +47,9 @@ declare function createArraySchema<T>(schema: Schema<T>): Schema<T[]>;
47
47
  declare function createBigIntSchema(): Schema<bigint>;
48
48
  declare function createBooleanSchema(): Schema<boolean>;
49
49
  declare function createDateSchema(): Schema<Date>;
50
+ declare function createDiscrimatedUnionSchema<B extends string, T extends Record<B, unknown>[]>(discriminator: B, schemas: {
51
+ [K in keyof T]: ObjectSchema<T[K]>;
52
+ }): Schema<T[number]>;
50
53
  declare function createInstanceofSchema<T>(schema: {
51
54
  new (...args: any[]): T;
52
55
  }): Schema<T>;
@@ -73,4 +76,4 @@ declare function createUnionSchema<T extends unknown[]>(schemas: {
73
76
  [K in keyof T]: Schema<T[K]>;
74
77
  }): Schema<T[number]>;
75
78
  declare function createUnknownSchema(): Schema<unknown>;
76
- export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
79
+ export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createDiscrimatedUnionSchema as discriminatedUnion, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
package/dist/zod.js CHANGED
@@ -187,6 +187,44 @@ function createBooleanSchema() {
187
187
  function createDateSchema() {
188
188
  return createInstanceofSchema(Date);
189
189
  }
190
+ function createDiscrimatedUnionSchema(discriminator, schemas) {
191
+ return new Schema((value, context) => {
192
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
193
+ if (context !== undefined) {
194
+ context.errors.push({
195
+ path: context.path,
196
+ message: `Expected object, got ${inspectValue(value)}`,
197
+ });
198
+ }
199
+ return { success: false };
200
+ }
201
+ const object = value;
202
+ const discriminatorValue = object[discriminator];
203
+ const discriminatedSchemas = schemas.filter((schema) => {
204
+ return schema.shape[discriminator].safeParse(discriminatorValue).success;
205
+ });
206
+ if (discriminatedSchemas.length > 1) {
207
+ if (context !== undefined) {
208
+ context.errors.push({
209
+ path: [...context.path, discriminator],
210
+ message: `Ambiguous discriminator value ${inspectValue(discriminatorValue)}`,
211
+ });
212
+ }
213
+ return { success: false };
214
+ }
215
+ const discriminatedSchema = discriminatedSchemas[0];
216
+ if (discriminatedSchema === undefined) {
217
+ if (context !== undefined) {
218
+ context.errors.push({
219
+ path: [...context.path, discriminator],
220
+ message: `Invalid discriminator value ${inspectValue(discriminatorValue)}`,
221
+ });
222
+ }
223
+ return { success: false };
224
+ }
225
+ return discriminatedSchema.internalSafeParse(value, context);
226
+ });
227
+ }
190
228
  // deno-lint-ignore no-explicit-any
191
229
  function createInstanceofSchema(schema) {
192
230
  return new Schema((value, context) => {
@@ -375,4 +413,4 @@ function createUnknownSchema() {
375
413
  return { success: true, data: value };
376
414
  });
377
415
  }
378
- export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
416
+ export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createDiscrimatedUnionSchema as discriminatedUnion, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullableSchema as nullable, createNullishSchema as nullish, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createOptionalSchema as optional, createRecordSchema as record, createStrictObjectSchema as strictObject, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quentinadam/zod",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "A simple library to parse data, inspired by zod",
5
5
  "license": "MIT",
6
6
  "author": "Quentin Adam",