@quentinadam/zod 0.1.6 → 0.1.8

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
@@ -12,7 +12,7 @@ type Result<T> = {
12
12
  } | {
13
13
  success: false;
14
14
  };
15
- export declare function getType(value: unknown): string;
15
+ export declare function inspectValue(value: unknown): string;
16
16
  export declare class Schema<T> {
17
17
  protected readonly safeParseFn: (value: unknown, context?: Context) => Result<T>;
18
18
  constructor(safeParseFn: (value: unknown, context?: Context) => Result<T>);
package/dist/zod.js CHANGED
@@ -1,10 +1,22 @@
1
- export function getType(value) {
1
+ export function inspectValue(value) {
2
2
  if (value === undefined)
3
3
  return 'undefined';
4
4
  if (value === null)
5
5
  return 'null';
6
6
  if (Array.isArray(value))
7
7
  return 'array';
8
+ if (typeof value === 'string') {
9
+ return `string ${value.length > 32 ? JSON.stringify(value.slice(0, 32) + '...') : JSON.stringify(value)}`;
10
+ }
11
+ if (typeof value === 'number') {
12
+ return `number ${value}`;
13
+ }
14
+ if (typeof value === 'bigint') {
15
+ return `bigint ${value.toString()}`;
16
+ }
17
+ if (typeof value === 'boolean') {
18
+ return `boolean ${value}`;
19
+ }
8
20
  return typeof value;
9
21
  }
10
22
  export class Schema {
@@ -70,7 +82,7 @@ export class ObjectSchema extends Schema {
70
82
  super((value, context) => {
71
83
  if (typeof value !== 'object' || value === null || Array.isArray(value)) {
72
84
  if (context !== undefined) {
73
- context.errors.push({ path: context.path, message: `Expected object, got ${getType(value)}` });
85
+ context.errors.push({ path: context.path, message: `Expected object, got ${inspectValue(value)}` });
74
86
  }
75
87
  return { success: false };
76
88
  }
@@ -121,7 +133,7 @@ function createArraySchema(schema) {
121
133
  return new Schema((value, context) => {
122
134
  if (!Array.isArray(value)) {
123
135
  if (context !== undefined) {
124
- context.errors.push({ path: context.path, message: `Expected array, got ${getType(value)}` });
136
+ context.errors.push({ path: context.path, message: `Expected array, got ${inspectValue(value)}` });
125
137
  }
126
138
  return { success: false };
127
139
  }
@@ -154,7 +166,7 @@ function createBigIntSchema() {
154
166
  return new Schema((value, context) => {
155
167
  if (typeof value !== 'bigint') {
156
168
  if (context !== undefined) {
157
- context.errors.push({ path: context.path, message: `Expected bigint, got ${getType(value)}` });
169
+ context.errors.push({ path: context.path, message: `Expected bigint, got ${inspectValue(value)}` });
158
170
  }
159
171
  return { success: false };
160
172
  }
@@ -165,7 +177,7 @@ function createBooleanSchema() {
165
177
  return new Schema((value, context) => {
166
178
  if (typeof value !== 'boolean') {
167
179
  if (context !== undefined) {
168
- context.errors.push({ path: context.path, message: `Expected boolean, got ${getType(value)}` });
180
+ context.errors.push({ path: context.path, message: `Expected boolean, got ${inspectValue(value)}` });
169
181
  }
170
182
  return { success: false };
171
183
  }
@@ -182,7 +194,7 @@ function createInstanceofSchema(schema) {
182
194
  if (context !== undefined) {
183
195
  context.errors.push({
184
196
  path: context.path,
185
- message: `Expected instance of ${schema.name}, got ${getType(value)}`,
197
+ message: `Expected instance of ${schema.name}, got ${inspectValue(value)}`,
186
198
  });
187
199
  }
188
200
  return { success: false };
@@ -200,7 +212,10 @@ function createLiteralSchema(literal) {
200
212
  return new Schema((value, context) => {
201
213
  if (value !== literal) {
202
214
  if (context !== undefined) {
203
- context.errors.push({ path: context.path, message: `Expected literal ${literal}, got ${getType(value)}` });
215
+ context.errors.push({
216
+ path: context.path,
217
+ message: `Expected literal ${JSON.stringify(literal)}, got ${inspectValue(value)}`,
218
+ });
204
219
  }
205
220
  return { success: false };
206
221
  }
@@ -223,7 +238,7 @@ function createNullSchema() {
223
238
  return new Schema((value, context) => {
224
239
  if (value !== null) {
225
240
  if (context !== undefined) {
226
- context.errors.push({ path: context.path, message: `Expected null, got ${getType(value)}` });
241
+ context.errors.push({ path: context.path, message: `Expected null, got ${inspectValue(value)}` });
227
242
  }
228
243
  return { success: false };
229
244
  }
@@ -234,7 +249,7 @@ function createNumberSchema() {
234
249
  return new Schema((value, context) => {
235
250
  if (typeof value !== 'number') {
236
251
  if (context !== undefined) {
237
- context.errors.push({ path: context.path, message: `Expected number, got ${getType(value)}` });
252
+ context.errors.push({ path: context.path, message: `Expected number, got ${inspectValue(value)}` });
238
253
  }
239
254
  return { success: false };
240
255
  }
@@ -244,7 +259,10 @@ function createNumberSchema() {
244
259
  function createRecordSchema(schema) {
245
260
  return new Schema((record, context) => {
246
261
  if (typeof record !== 'object' || record === null || Array.isArray(record)) {
247
- throw new Error(`Expected object, got ${getType(record)}`);
262
+ if (context !== undefined) {
263
+ context.errors.push({ path: context.path, message: `Expected object, got ${inspectValue(record)}` });
264
+ }
265
+ return { success: false };
248
266
  }
249
267
  const result = (() => {
250
268
  const parsedObject = {};
@@ -275,7 +293,7 @@ function createStringSchema() {
275
293
  return new Schema((value, context) => {
276
294
  if (typeof value !== 'string') {
277
295
  if (context !== undefined) {
278
- context.errors.push({ path: context.path, message: `Expected string, got ${getType(value)}` });
296
+ context.errors.push({ path: context.path, message: `Expected string, got ${inspectValue(value)}` });
279
297
  }
280
298
  return { success: false };
281
299
  }
@@ -286,7 +304,7 @@ function createTupleSchema(schema) {
286
304
  return new Schema((value, context) => {
287
305
  if (!Array.isArray(value)) {
288
306
  if (context !== undefined) {
289
- context.errors.push({ path: context.path, message: `Expected array, got ${getType(value)}` });
307
+ context.errors.push({ path: context.path, message: `Expected array, got ${inspectValue(value)}` });
290
308
  }
291
309
  return { success: false };
292
310
  }
@@ -329,7 +347,7 @@ function createUndefinedSchema() {
329
347
  return new Schema((value, context) => {
330
348
  if (value !== undefined) {
331
349
  if (context !== undefined) {
332
- context.errors.push({ path: context.path, message: `Expected undefined, got ${getType(value)}` });
350
+ context.errors.push({ path: context.path, message: `Expected undefined, got ${inspectValue(value)}` });
333
351
  }
334
352
  return { success: false };
335
353
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quentinadam/zod",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "A simple library to parse data, inspired by zod",
5
5
  "license": "MIT",
6
6
  "author": "Quentin Adam",