@quentinadam/zod 0.1.1 → 0.1.3

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.
Files changed (3) hide show
  1. package/dist/zod.d.ts +14 -13
  2. package/dist/zod.js +57 -50
  3. package/package.json +1 -1
package/dist/zod.d.ts CHANGED
@@ -31,27 +31,28 @@ export declare class ObjectSchema<T extends Record<string, unknown>> extends Sch
31
31
  [K in keyof T]: T[K] | undefined;
32
32
  }>;
33
33
  }
34
- declare function createStringSchema(): Schema<string>;
35
- declare function createNumberSchema(): Schema<number>;
34
+ declare function createArraySchema<T>(schema: Schema<T>): Schema<T[]>;
36
35
  declare function createBigIntSchema(): Schema<bigint>;
37
- declare function createUnknownSchema(): Schema<unknown>;
38
36
  declare function createBooleanSchema(): Schema<boolean>;
39
- declare function createNullSchema(): Schema<null>;
40
- declare function createUndefinedSchema(): Schema<undefined>;
41
37
  declare function createDateSchema(): Schema<Date>;
42
- declare function createTupleSchema<T extends unknown[]>(schema: {
43
- [K in keyof T]: Schema<T[K]>;
38
+ declare function createInstanceofSchema<T>(schema: {
39
+ new (...args: any[]): T;
44
40
  }): Schema<T>;
41
+ declare function createLazySchema<T>(fn: () => Schema<T>): Schema<T>;
42
+ declare function createLiteralSchema<T extends string | number | boolean | null>(litteral: T): Schema<T>;
45
43
  declare function createObjectSchema<T extends Record<string, unknown>>(schema: {
46
44
  [K in keyof T]: Schema<T[K]>;
47
45
  }): ObjectSchema<T>;
48
- declare function createInstanceofSchema<T>(schema: {
49
- new (...args: unknown[]): T;
50
- }): Schema<T>;
51
- declare function createArraySchema<T>(schema: Schema<T>): Schema<T[]>;
46
+ declare function createNullSchema(): Schema<null>;
47
+ declare function createNumberSchema(): Schema<number>;
52
48
  declare function createRecordSchema<T>(schema: Schema<T>): Schema<Record<string, T>>;
49
+ declare function createStringSchema(): Schema<string>;
50
+ declare function createTupleSchema<T extends unknown[]>(schema: {
51
+ [K in keyof T]: Schema<T[K]>;
52
+ }): Schema<T>;
53
+ declare function createUndefinedSchema(): Schema<undefined>;
53
54
  declare function createUnionSchema<T extends unknown[]>(schemas: {
54
55
  [K in keyof T]: Schema<T[K]>;
55
56
  }): Schema<T[number]>;
56
- declare function createLiteralSchema<T extends string | number | boolean | null>(value: T): Schema<T>;
57
- export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
57
+ declare function createUnknownSchema(): Schema<unknown>;
58
+ export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
package/dist/zod.js CHANGED
@@ -76,147 +76,157 @@ export class ObjectSchema extends Schema {
76
76
  return new ObjectSchema(schema, this.#strict);
77
77
  }
78
78
  }
79
- function createStringSchema() {
80
- const inputType = 'string';
79
+ function createArraySchema(schema) {
80
+ const inputType = `Array<${schema.inputType}>`;
81
81
  return new Schema({
82
82
  inputType,
83
83
  parseFn: (value) => {
84
- if (typeof value !== 'string') {
84
+ if (!Array.isArray(value)) {
85
85
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
86
86
  }
87
- return value;
87
+ return value.map((item) => schema.parse(item));
88
88
  },
89
89
  });
90
90
  }
91
- function createNumberSchema() {
92
- const inputType = 'number';
91
+ function createBigIntSchema() {
92
+ const inputType = 'bigint';
93
93
  return new Schema({
94
94
  inputType,
95
95
  parseFn: (value) => {
96
- if (typeof value !== 'number') {
96
+ if (typeof value !== 'bigint') {
97
97
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
98
98
  }
99
99
  return value;
100
100
  },
101
101
  });
102
102
  }
103
- function createBigIntSchema() {
104
- const inputType = 'bigint';
103
+ function createBooleanSchema() {
104
+ const inputType = 'boolean';
105
105
  return new Schema({
106
106
  inputType,
107
107
  parseFn: (value) => {
108
- if (typeof value !== 'bigint') {
108
+ if (typeof value !== 'boolean') {
109
109
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
110
110
  }
111
111
  return value;
112
112
  },
113
113
  });
114
114
  }
115
- function createUnknownSchema() {
116
- const inputType = 'unknown';
115
+ function createDateSchema() {
116
+ const inputType = 'Date';
117
117
  return new Schema({
118
118
  inputType,
119
119
  parseFn: (value) => {
120
+ if (!(value instanceof Date)) {
121
+ throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
122
+ }
120
123
  return value;
121
124
  },
122
125
  });
123
126
  }
124
- function createBooleanSchema() {
125
- const inputType = 'boolean';
127
+ // deno-lint-ignore no-explicit-any
128
+ function createInstanceofSchema(schema) {
129
+ const inputType = schema.name;
126
130
  return new Schema({
127
131
  inputType,
128
132
  parseFn: (value) => {
129
- if (typeof value !== 'boolean') {
133
+ if (!(value instanceof schema)) {
130
134
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
131
135
  }
132
136
  return value;
133
137
  },
134
138
  });
135
139
  }
136
- function createNullSchema() {
137
- const inputType = 'null';
140
+ function createLazySchema(fn) {
141
+ return new Schema({
142
+ inputType: '(lazy)',
143
+ parseFn: (value) => fn().parse(value),
144
+ });
145
+ }
146
+ function createLiteralSchema(litteral) {
147
+ const inputType = JSON.stringify(litteral);
138
148
  return new Schema({
139
149
  inputType,
140
150
  parseFn: (value) => {
141
- if (value !== null) {
151
+ if (value !== litteral) {
142
152
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
143
153
  }
144
- return value;
154
+ return litteral;
145
155
  },
146
156
  });
147
157
  }
148
- function createUndefinedSchema() {
149
- const inputType = 'undefined';
158
+ function createObjectSchema(schema) {
159
+ return new ObjectSchema(schema);
160
+ }
161
+ function createNullSchema() {
162
+ const inputType = 'null';
150
163
  return new Schema({
151
164
  inputType,
152
165
  parseFn: (value) => {
153
- if (value !== undefined) {
166
+ if (value !== null) {
154
167
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
155
168
  }
156
169
  return value;
157
170
  },
158
171
  });
159
172
  }
160
- function createDateSchema() {
161
- const inputType = 'Date';
173
+ function createNumberSchema() {
174
+ const inputType = 'number';
162
175
  return new Schema({
163
176
  inputType,
164
177
  parseFn: (value) => {
165
- if (!(value instanceof Date)) {
178
+ if (typeof value !== 'number') {
166
179
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
167
180
  }
168
181
  return value;
169
182
  },
170
183
  });
171
184
  }
172
- function createTupleSchema(schema) {
173
- const inputType = `[ ${schema.map((schema) => schema.inputType).join(', ')} ]`;
185
+ function createRecordSchema(schema) {
186
+ const inputType = `Record<string, ${schema.inputType}>`;
174
187
  return new Schema({
175
188
  inputType,
176
189
  parseFn: (value) => {
177
- if (!Array.isArray(value) || value.length !== schema.length) {
190
+ if (typeof value !== 'object' || value === null) {
178
191
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
179
192
  }
180
- return schema.map((schema, index) => schema.parse(value[index]));
193
+ return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, schema.parse(value)]));
181
194
  },
182
195
  });
183
196
  }
184
- function createObjectSchema(schema) {
185
- return new ObjectSchema(schema);
186
- }
187
- function createInstanceofSchema(schema) {
188
- const inputType = schema.name;
197
+ function createStringSchema() {
198
+ const inputType = 'string';
189
199
  return new Schema({
190
200
  inputType,
191
201
  parseFn: (value) => {
192
- if (!(value instanceof schema)) {
202
+ if (typeof value !== 'string') {
193
203
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
194
204
  }
195
205
  return value;
196
206
  },
197
207
  });
198
208
  }
199
- function createArraySchema(schema) {
200
- const inputType = `Array<${schema.inputType}>`;
209
+ function createTupleSchema(schema) {
210
+ const inputType = `[ ${schema.map((schema) => schema.inputType).join(', ')} ]`;
201
211
  return new Schema({
202
212
  inputType,
203
213
  parseFn: (value) => {
204
- if (!Array.isArray(value)) {
214
+ if (!Array.isArray(value) || value.length !== schema.length) {
205
215
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
206
216
  }
207
- return value.map((item) => schema.parse(item));
217
+ return schema.map((schema, index) => schema.parse(value[index]));
208
218
  },
209
219
  });
210
220
  }
211
- function createRecordSchema(schema) {
212
- const inputType = `Record<string, ${schema.inputType}>`;
221
+ function createUndefinedSchema() {
222
+ const inputType = 'undefined';
213
223
  return new Schema({
214
224
  inputType,
215
225
  parseFn: (value) => {
216
- if (typeof value !== 'object' || value === null) {
226
+ if (value !== undefined) {
217
227
  throw new Error(`Expected ${inputType}, got ${JSON.stringify(value)}`);
218
228
  }
219
- return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, schema.parse(value)]));
229
+ return value;
220
230
  },
221
231
  });
222
232
  }
@@ -237,16 +247,13 @@ function createUnionSchema(schemas) {
237
247
  },
238
248
  });
239
249
  }
240
- function createLiteralSchema(value) {
241
- const inputType = JSON.stringify(value);
250
+ function createUnknownSchema() {
251
+ const inputType = 'unknown';
242
252
  return new Schema({
243
253
  inputType,
244
- parseFn: (input) => {
245
- if (input !== value) {
246
- throw new Error(`Expected ${inputType}, got ${input}`);
247
- }
254
+ parseFn: (value) => {
248
255
  return value;
249
256
  },
250
257
  });
251
258
  }
252
- export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, createStringSchema as string, createTupleSchema as tuple, createUndefinedSchema as undefined, createUnionSchema as union, createUnknownSchema as unknown, };
259
+ export { createArraySchema as array, createBigIntSchema as bigint, createBooleanSchema as boolean, createDateSchema as date, createInstanceofSchema as instanceof, createLazySchema as lazy, createLiteralSchema as literal, createNullSchema as null, createNumberSchema as number, createObjectSchema as object, createRecordSchema as record, 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.1",
3
+ "version": "0.1.3",
4
4
  "description": "A simple library to parse data, inspired by zod",
5
5
  "license": "MIT",
6
6
  "author": "Quentin Adam",