@dzeio/schema 0.0.5 → 0.1.0

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
@@ -40,6 +40,11 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
40
40
  private invalidError;
41
41
  constructor(items?: Array<unknown> | IArguments);
42
42
  defaultValue(value: Type, strict?: boolean): this;
43
+ /**
44
+ * make sure the value is one of the `values`
45
+ * @param values the values the item MUST contains
46
+ */
47
+ in(...values: Array<Type>): this;
43
48
  attrs(...attributes: Array<string>): this;
44
49
  attr(...attributes: Array<string>): this;
45
50
  setInvalidError(err: string): this;
@@ -285,8 +290,8 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
285
290
 
286
291
  declare function isNull(value: unknown): value is undefined | null;
287
292
  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']>;
293
+ declare function parseFormData<T extends SchemaItem>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
294
+ declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
290
295
 
291
296
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
292
297
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
package/dist/Schema.d.ts CHANGED
@@ -40,6 +40,11 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
40
40
  private invalidError;
41
41
  constructor(items?: Array<unknown> | IArguments);
42
42
  defaultValue(value: Type, strict?: boolean): this;
43
+ /**
44
+ * make sure the value is one of the `values`
45
+ * @param values the values the item MUST contains
46
+ */
47
+ in(...values: Array<Type>): this;
43
48
  attrs(...attributes: Array<string>): this;
44
49
  attr(...attributes: Array<string>): this;
45
50
  setInvalidError(err: string): this;
@@ -285,8 +290,8 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
285
290
 
286
291
  declare function isNull(value: unknown): value is undefined | null;
287
292
  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']>;
293
+ declare function parseFormData<T extends SchemaItem>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
294
+ declare function parseForm<T extends SchemaItem>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
290
295
 
291
296
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
292
297
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
package/dist/Schema.js CHANGED
@@ -119,6 +119,10 @@ var _SchemaItem = class _SchemaItem {
119
119
  });
120
120
  return this;
121
121
  }
122
+ in(...values) {
123
+ this.addValidation((input) => values.includes(input));
124
+ return this;
125
+ }
122
126
  attrs(...attributes) {
123
127
  this.attributes.concat(attributes);
124
128
  return this;
@@ -205,6 +209,9 @@ var _SchemaItem = class _SchemaItem {
205
209
  __decorateClass([
206
210
  parceable()
207
211
  ], _SchemaItem.prototype, "defaultValue", 1);
212
+ __decorateClass([
213
+ parceable()
214
+ ], _SchemaItem.prototype, "in", 1);
208
215
  var SchemaItem = _SchemaItem;
209
216
 
210
217
  // src/items/array.ts
@@ -375,20 +382,19 @@ function parseQuery(model, query, opts) {
375
382
  return model.parse(record, opts);
376
383
  }
377
384
  function parseFormData(model, data, opts) {
378
- var _a;
379
385
  const record = {};
380
386
  for (const [key, value] of data) {
381
- const isArray = (_a = model.model[key].isOfType([])) != null ? _a : false;
382
- (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), isArray ? data.getAll(key) : value);
387
+ const hasMultipleValues = data.getAll(key).length > 1;
388
+ (0, import_object_util3.objectSet)(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
383
389
  }
384
390
  const handleBoolean = (value, keys) => {
385
- var _a2;
391
+ var _a;
386
392
  if (value instanceof SchemaNullable) {
387
393
  handleBoolean(value.unwrap(), keys);
388
394
  }
389
395
  if (value instanceof SchemaArray) {
390
396
  const elements = (0, import_object_util3.objectGet)(record, keys);
391
- for (let it = 0; it < ((_a2 = elements == null ? void 0 : elements.length) != null ? _a2 : 0); it++) {
397
+ for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
392
398
  handleBoolean(value.unwrap(), [...keys, it]);
393
399
  }
394
400
  }
@@ -404,7 +410,9 @@ function parseFormData(model, data, opts) {
404
410
  handleBoolean(value, [...keys, key]);
405
411
  });
406
412
  };
407
- handleSchemaForBoolean(model.model);
413
+ if (model instanceof SchemaObject) {
414
+ handleSchemaForBoolean(model.model);
415
+ }
408
416
  return model.parse(record, opts);
409
417
  }
410
418
  function parseForm(model, form, opts) {
package/dist/Schema.mjs CHANGED
@@ -76,6 +76,10 @@ var _SchemaItem = class _SchemaItem {
76
76
  });
77
77
  return this;
78
78
  }
79
+ in(...values) {
80
+ this.addValidation((input) => values.includes(input));
81
+ return this;
82
+ }
79
83
  attrs(...attributes) {
80
84
  this.attributes.concat(attributes);
81
85
  return this;
@@ -162,6 +166,9 @@ var _SchemaItem = class _SchemaItem {
162
166
  __decorateClass([
163
167
  parceable()
164
168
  ], _SchemaItem.prototype, "defaultValue", 1);
169
+ __decorateClass([
170
+ parceable()
171
+ ], _SchemaItem.prototype, "in", 1);
165
172
  var SchemaItem = _SchemaItem;
166
173
 
167
174
  // src/items/array.ts
@@ -332,20 +339,19 @@ function parseQuery(model, query, opts) {
332
339
  return model.parse(record, opts);
333
340
  }
334
341
  function parseFormData(model, data, opts) {
335
- var _a;
336
342
  const record = {};
337
343
  for (const [key, value] of data) {
338
- const isArray = (_a = model.model[key].isOfType([])) != null ? _a : false;
339
- objectSet(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), isArray ? data.getAll(key) : value);
344
+ const hasMultipleValues = data.getAll(key).length > 1;
345
+ objectSet(record, key.split(".").map((it) => /^\d+$/g.test(it) ? parseInt(it, 10) : it), hasMultipleValues ? data.getAll(key) : value);
340
346
  }
341
347
  const handleBoolean = (value, keys) => {
342
- var _a2;
348
+ var _a;
343
349
  if (value instanceof SchemaNullable) {
344
350
  handleBoolean(value.unwrap(), keys);
345
351
  }
346
352
  if (value instanceof SchemaArray) {
347
353
  const elements = objectGet(record, keys);
348
- for (let it = 0; it < ((_a2 = elements == null ? void 0 : elements.length) != null ? _a2 : 0); it++) {
354
+ for (let it = 0; it < ((_a = elements == null ? void 0 : elements.length) != null ? _a : 0); it++) {
349
355
  handleBoolean(value.unwrap(), [...keys, it]);
350
356
  }
351
357
  }
@@ -361,7 +367,9 @@ function parseFormData(model, data, opts) {
361
367
  handleBoolean(value, [...keys, key]);
362
368
  });
363
369
  };
364
- handleSchemaForBoolean(model.model);
370
+ if (model instanceof SchemaObject) {
371
+ handleSchemaForBoolean(model.model);
372
+ }
365
373
  return model.parse(record, opts);
366
374
  }
367
375
  function parseForm(model, form, opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.0.5",
3
+ "version": "0.1.0",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },