@dzeio/schema 0.0.4 → 0.0.6

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/Schema.d.mts CHANGED
@@ -1,16 +1,5 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
 
3
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
4
- readonly child: Type;
5
- constructor(child: Type);
6
- unwrap(): Type;
7
- parse(input: unknown, options?: {
8
- fast?: boolean;
9
- }): ValidationResult<SchemaInfer<Type> | undefined>;
10
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
11
- private isNull;
12
- }
13
-
14
3
  declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
15
4
  '~standard': StandardSchemaV1.Props<Type>;
16
5
  /**
@@ -191,6 +180,17 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
191
180
  isOfType(input: unknown): input is Type;
192
181
  }
193
182
 
183
+ declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
184
+ readonly child: Type;
185
+ constructor(child: Type);
186
+ unwrap(): Type;
187
+ parse(input: unknown, options?: {
188
+ fast?: boolean;
189
+ }): ValidationResult<SchemaInfer<Type> | undefined>;
190
+ isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
191
+ private isNull;
192
+ }
193
+
194
194
  declare class SchemaNumber extends SchemaItem<number> {
195
195
  /**
196
196
  * validate that the number is less or equal than {@link value}
@@ -285,8 +285,8 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
285
285
 
286
286
  declare function isNull(value: unknown): value is undefined | null;
287
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
288
- declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
289
- declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
288
+ declare function parseFormData<T extends SchemaItem>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
289
+ declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
290
290
 
291
291
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
292
292
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
package/dist/Schema.d.ts CHANGED
@@ -1,16 +1,5 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
 
3
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
4
- readonly child: Type;
5
- constructor(child: Type);
6
- unwrap(): Type;
7
- parse(input: unknown, options?: {
8
- fast?: boolean;
9
- }): ValidationResult<SchemaInfer<Type> | undefined>;
10
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
11
- private isNull;
12
- }
13
-
14
3
  declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
15
4
  '~standard': StandardSchemaV1.Props<Type>;
16
5
  /**
@@ -191,6 +180,17 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
191
180
  isOfType(input: unknown): input is Type;
192
181
  }
193
182
 
183
+ declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
184
+ readonly child: Type;
185
+ constructor(child: Type);
186
+ unwrap(): Type;
187
+ parse(input: unknown, options?: {
188
+ fast?: boolean;
189
+ }): ValidationResult<SchemaInfer<Type> | undefined>;
190
+ isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
191
+ private isNull;
192
+ }
193
+
194
194
  declare class SchemaNumber extends SchemaItem<number> {
195
195
  /**
196
196
  * validate that the number is less or equal than {@link value}
@@ -285,8 +285,8 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
285
285
 
286
286
  declare function isNull(value: unknown): value is undefined | null;
287
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
288
- declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
289
- declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
288
+ declare function parseFormData<T extends SchemaItem>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
289
+ declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
290
290
 
291
291
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
292
292
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
package/dist/Schema.js CHANGED
@@ -57,34 +57,6 @@ var import_object_util3 = require("@dzeio/object-util");
57
57
 
58
58
  // src/SchemaItem.ts
59
59
  var import_object_util = require("@dzeio/object-util");
60
-
61
- // src/items/nullable.ts
62
- var SchemaNullable = class extends SchemaItem {
63
- constructor(child) {
64
- super([child]);
65
- this.child = child;
66
- }
67
- unwrap() {
68
- return this.child;
69
- }
70
- parse(input, options) {
71
- if (this.isNull(input)) {
72
- return {
73
- valid: true,
74
- object: void 0
75
- };
76
- }
77
- return this.child.parse(input, options);
78
- }
79
- isOfType(input) {
80
- return Array.isArray(input);
81
- }
82
- isNull(value) {
83
- return typeof value === "undefined" || value === null;
84
- }
85
- };
86
-
87
- // src/SchemaItem.ts
88
60
  var _SchemaItem = class _SchemaItem {
89
61
  constructor(items) {
90
62
  // standard Schema V1 spec
@@ -324,6 +296,32 @@ __decorateClass([
324
296
  parceable()
325
297
  ], SchemaBoolean.prototype, "parseString", 1);
326
298
 
299
+ // src/items/nullable.ts
300
+ var SchemaNullable = class extends SchemaItem {
301
+ constructor(child) {
302
+ super([child]);
303
+ this.child = child;
304
+ }
305
+ unwrap() {
306
+ return this.child;
307
+ }
308
+ parse(input, options) {
309
+ if (this.isNull(input)) {
310
+ return {
311
+ valid: true,
312
+ object: void 0
313
+ };
314
+ }
315
+ return this.child.parse(input, options);
316
+ }
317
+ isOfType(input) {
318
+ return Array.isArray(input);
319
+ }
320
+ isNull(value) {
321
+ return typeof value === "undefined" || value === null;
322
+ }
323
+ };
324
+
327
325
  // src/items/object.ts
328
326
  var import_object_util2 = require("@dzeio/object-util");
329
327
  var SchemaObject = class extends SchemaItem {
@@ -377,20 +375,19 @@ function parseQuery(model, query, opts) {
377
375
  return model.parse(record, opts);
378
376
  }
379
377
  function parseFormData(model, data, opts) {
380
- var _a;
381
378
  const record = {};
382
379
  for (const [key, value] of data) {
383
- const isArray = (_a = model.model[key].isOfType([])) != null ? _a : false;
384
- (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), isArray ? data.getAll(key) : value);
380
+ const hasMultipleValues = data.getAll(key).length > 1;
381
+ (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
385
382
  }
386
383
  const handleBoolean = (value, keys) => {
387
- var _a2;
384
+ var _a;
388
385
  if (value instanceof SchemaNullable) {
389
386
  handleBoolean(value.unwrap(), keys);
390
387
  }
391
388
  if (value instanceof SchemaArray) {
392
389
  const elements = (0, import_object_util3.objectGet)(record, keys);
393
- for (let it = 0; it < ((_a2 = elements == null ? void 0 : elements.length) != null ? _a2 : 0); it++) {
390
+ for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
394
391
  handleBoolean(value.unwrap(), [...keys, it]);
395
392
  }
396
393
  }
@@ -406,7 +403,9 @@ function parseFormData(model, data, opts) {
406
403
  handleBoolean(value, [...keys, key]);
407
404
  });
408
405
  };
409
- handleSchemaForBoolean(model.model);
406
+ if (model instanceof SchemaObject) {
407
+ handleSchemaForBoolean(model.model);
408
+ }
410
409
  return model.parse(record, opts);
411
410
  }
412
411
  function parseForm(model, form, opts) {
@@ -679,9 +678,6 @@ var SchemaUnion = class extends SchemaItem {
679
678
  // src/Schema.ts
680
679
  function parceable() {
681
680
  return (target, propertyKey, descriptor) => {
682
- if (!(target instanceof SchemaItem)) {
683
- throw new Error("the decorator is only usable on Schema");
684
- }
685
681
  if (!(propertyKey in target)) {
686
682
  throw new Error("property not set in object");
687
683
  }
package/dist/Schema.mjs CHANGED
@@ -14,34 +14,6 @@ import { objectGet, objectLoop as objectLoop2, objectSet } from "@dzeio/object-u
14
14
 
15
15
  // src/SchemaItem.ts
16
16
  import { objectClean } from "@dzeio/object-util";
17
-
18
- // src/items/nullable.ts
19
- var SchemaNullable = class extends SchemaItem {
20
- constructor(child) {
21
- super([child]);
22
- this.child = child;
23
- }
24
- unwrap() {
25
- return this.child;
26
- }
27
- parse(input, options) {
28
- if (this.isNull(input)) {
29
- return {
30
- valid: true,
31
- object: void 0
32
- };
33
- }
34
- return this.child.parse(input, options);
35
- }
36
- isOfType(input) {
37
- return Array.isArray(input);
38
- }
39
- isNull(value) {
40
- return typeof value === "undefined" || value === null;
41
- }
42
- };
43
-
44
- // src/SchemaItem.ts
45
17
  var _SchemaItem = class _SchemaItem {
46
18
  constructor(items) {
47
19
  // standard Schema V1 spec
@@ -281,6 +253,32 @@ __decorateClass([
281
253
  parceable()
282
254
  ], SchemaBoolean.prototype, "parseString", 1);
283
255
 
256
+ // src/items/nullable.ts
257
+ var SchemaNullable = class extends SchemaItem {
258
+ constructor(child) {
259
+ super([child]);
260
+ this.child = child;
261
+ }
262
+ unwrap() {
263
+ return this.child;
264
+ }
265
+ parse(input, options) {
266
+ if (this.isNull(input)) {
267
+ return {
268
+ valid: true,
269
+ object: void 0
270
+ };
271
+ }
272
+ return this.child.parse(input, options);
273
+ }
274
+ isOfType(input) {
275
+ return Array.isArray(input);
276
+ }
277
+ isNull(value) {
278
+ return typeof value === "undefined" || value === null;
279
+ }
280
+ };
281
+
284
282
  // src/items/object.ts
285
283
  import { isObject, objectClone, objectLoop } from "@dzeio/object-util";
286
284
  var SchemaObject = class extends SchemaItem {
@@ -334,20 +332,19 @@ function parseQuery(model, query, opts) {
334
332
  return model.parse(record, opts);
335
333
  }
336
334
  function parseFormData(model, data, opts) {
337
- var _a;
338
335
  const record = {};
339
336
  for (const [key, value] of data) {
340
- const isArray = (_a = model.model[key].isOfType([])) != null ? _a : false;
341
- objectSet(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), isArray ? data.getAll(key) : value);
337
+ const hasMultipleValues = data.getAll(key).length > 1;
338
+ objectSet(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
342
339
  }
343
340
  const handleBoolean = (value, keys) => {
344
- var _a2;
341
+ var _a;
345
342
  if (value instanceof SchemaNullable) {
346
343
  handleBoolean(value.unwrap(), keys);
347
344
  }
348
345
  if (value instanceof SchemaArray) {
349
346
  const elements = objectGet(record, keys);
350
- for (let it = 0; it < ((_a2 = elements == null ? void 0 : elements.length) != null ? _a2 : 0); it++) {
347
+ for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
351
348
  handleBoolean(value.unwrap(), [...keys, it]);
352
349
  }
353
350
  }
@@ -363,7 +360,9 @@ function parseFormData(model, data, opts) {
363
360
  handleBoolean(value, [...keys, key]);
364
361
  });
365
362
  };
366
- handleSchemaForBoolean(model.model);
363
+ if (model instanceof SchemaObject) {
364
+ handleSchemaForBoolean(model.model);
365
+ }
367
366
  return model.parse(record, opts);
368
367
  }
369
368
  function parseForm(model, form, opts) {
@@ -636,9 +635,6 @@ var SchemaUnion = class extends SchemaItem {
636
635
  // src/Schema.ts
637
636
  function parceable() {
638
637
  return (target, propertyKey, descriptor) => {
639
- if (!(target instanceof SchemaItem)) {
640
- throw new Error("the decorator is only usable on Schema");
641
- }
642
638
  if (!(propertyKey in target)) {
643
639
  throw new Error("property not set in object");
644
640
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },