@dzeio/schema 0.0.6 → 0.2.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;
@@ -127,6 +132,7 @@ declare class SchemaAny extends SchemaItem {
127
132
  }
128
133
 
129
134
  declare class SchemaDate extends SchemaItem<Date> {
135
+ parseString(format?: 'iso8601' | 'yy-mm-dd' | 'jj/mm/yy'): void;
130
136
  isOfType(input: unknown): input is Date;
131
137
  }
132
138
 
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;
@@ -127,6 +132,7 @@ declare class SchemaAny extends SchemaItem {
127
132
  }
128
133
 
129
134
  declare class SchemaDate extends SchemaItem<Date> {
135
+ parseString(format?: 'iso8601' | 'yy-mm-dd' | 'jj/mm/yy'): void;
130
136
  isOfType(input: unknown): input is Date;
131
137
  }
132
138
 
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
@@ -422,10 +429,34 @@ var SchemaAny = class extends SchemaItem {
422
429
 
423
430
  // src/items/date.ts
424
431
  var SchemaDate = class extends SchemaItem {
432
+ parseString(format = "iso8601") {
433
+ switch (format) {
434
+ case "yy-mm-dd":
435
+ case "iso8601": {
436
+ this.addPreProcess((it) => typeof it === "string" ? new Date(it) : it);
437
+ break;
438
+ }
439
+ case "jj/mm/yy": {
440
+ this.addPreProcess((input) => {
441
+ if (typeof input !== "string") {
442
+ return input;
443
+ }
444
+ const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
445
+ if (splitted.length !== 3) {
446
+ return input;
447
+ }
448
+ return new Date(splitted[2], splitted[1] - 1, splitted[0]);
449
+ });
450
+ }
451
+ }
452
+ }
425
453
  isOfType(input) {
426
454
  return input instanceof Date;
427
455
  }
428
456
  };
457
+ __decorateClass([
458
+ parceable()
459
+ ], SchemaDate.prototype, "parseString", 1);
429
460
 
430
461
  // src/items/record.ts
431
462
  var import_object_util4 = require("@dzeio/object-util");
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
@@ -379,10 +386,34 @@ var SchemaAny = class extends SchemaItem {
379
386
 
380
387
  // src/items/date.ts
381
388
  var SchemaDate = class extends SchemaItem {
389
+ parseString(format = "iso8601") {
390
+ switch (format) {
391
+ case "yy-mm-dd":
392
+ case "iso8601": {
393
+ this.addPreProcess((it) => typeof it === "string" ? new Date(it) : it);
394
+ break;
395
+ }
396
+ case "jj/mm/yy": {
397
+ this.addPreProcess((input) => {
398
+ if (typeof input !== "string") {
399
+ return input;
400
+ }
401
+ const splitted = input.split("/").map((it) => Number.parseInt(it, 10));
402
+ if (splitted.length !== 3) {
403
+ return input;
404
+ }
405
+ return new Date(splitted[2], splitted[1] - 1, splitted[0]);
406
+ });
407
+ }
408
+ }
409
+ }
382
410
  isOfType(input) {
383
411
  return input instanceof Date;
384
412
  }
385
413
  };
414
+ __decorateClass([
415
+ parceable()
416
+ ], SchemaDate.prototype, "parseString", 1);
386
417
 
387
418
  // src/items/record.ts
388
419
  import { isObject as isObject2, objectLoop as objectLoop3 } from "@dzeio/object-util";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.0.6",
3
+ "version": "0.2.0",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },