@dzeio/schema 0.0.2 → 0.0.4

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,5 +1,16 @@
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
+
3
14
  declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
4
15
  '~standard': StandardSchemaV1.Props<Type>;
5
16
  /**
@@ -39,6 +50,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
39
50
  private readonly items?;
40
51
  private invalidError;
41
52
  constructor(items?: Array<unknown> | IArguments);
53
+ defaultValue(value: Type, strict?: boolean): this;
42
54
  attrs(...attributes: Array<string>): this;
43
55
  attr(...attributes: Array<string>): this;
44
56
  setInvalidError(err: string): this;
@@ -121,6 +133,10 @@ type ModelInfer$1<M extends Model> = {
121
133
  [key in keyof M]: SchemaInfer<M[key]>
122
134
  }
123
135
 
136
+ declare class SchemaAny extends SchemaItem {
137
+ isOfType(input: unknown): input is any;
138
+ }
139
+
124
140
  declare class SchemaDate extends SchemaItem<Date> {
125
141
  isOfType(input: unknown): input is Date;
126
142
  }
@@ -175,17 +191,6 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
175
191
  isOfType(input: unknown): input is Type;
176
192
  }
177
193
 
178
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
179
- readonly child: Type;
180
- constructor(child: Type);
181
- unwrap(): Type;
182
- parse(input: unknown, options?: {
183
- fast?: boolean;
184
- }): ValidationResult<SchemaInfer<Type> | undefined>;
185
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
186
- private isNull;
187
- }
188
-
189
194
  declare class SchemaNumber extends SchemaItem<number> {
190
195
  /**
191
196
  * validate that the number is less or equal than {@link value}
@@ -278,6 +283,7 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
278
283
  isOfType(input: unknown): input is ItemType<T>;
279
284
  }
280
285
 
286
+ declare function isNull(value: unknown): value is undefined | null;
281
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
282
288
  declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
283
289
  declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
@@ -285,6 +291,7 @@ declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormEleme
285
291
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
286
292
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
287
293
  declare const Types: {
294
+ readonly Any: typeof SchemaAny;
288
295
  readonly Array: typeof SchemaArray;
289
296
  readonly Boolean: typeof SchemaBoolean;
290
297
  readonly Date: typeof SchemaDate;
@@ -300,6 +307,7 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
300
307
  private static registeredModules;
301
308
  static register(module: SchemaItemStatic): void;
302
309
  static getModule(name: string): SchemaItemStatic | undefined;
310
+ static any(): SchemaAny;
303
311
  static array<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaArray<Type>>): SchemaArray<Type>;
304
312
  static boolean(...inputs: ConstructorParameters<typeof SchemaBoolean>): SchemaBoolean;
305
313
  static date(...inputs: ConstructorParameters<typeof SchemaDate>): SchemaDate;
@@ -340,4 +348,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
340
348
  }
341
349
  declare const s: typeof Schema;
342
350
 
343
- export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, type ValidationResultOld, Schema as default, parceable, parseForm, parseFormData, parseQuery, s };
351
+ export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaAny, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, type ValidationResultOld, Schema as default, isNull, parceable, parseForm, parseFormData, parseQuery, s };
package/dist/Schema.d.ts CHANGED
@@ -1,5 +1,16 @@
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
+
3
14
  declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type> {
4
15
  '~standard': StandardSchemaV1.Props<Type>;
5
16
  /**
@@ -39,6 +50,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
39
50
  private readonly items?;
40
51
  private invalidError;
41
52
  constructor(items?: Array<unknown> | IArguments);
53
+ defaultValue(value: Type, strict?: boolean): this;
42
54
  attrs(...attributes: Array<string>): this;
43
55
  attr(...attributes: Array<string>): this;
44
56
  setInvalidError(err: string): this;
@@ -121,6 +133,10 @@ type ModelInfer$1<M extends Model> = {
121
133
  [key in keyof M]: SchemaInfer<M[key]>
122
134
  }
123
135
 
136
+ declare class SchemaAny extends SchemaItem {
137
+ isOfType(input: unknown): input is any;
138
+ }
139
+
124
140
  declare class SchemaDate extends SchemaItem<Date> {
125
141
  isOfType(input: unknown): input is Date;
126
142
  }
@@ -175,17 +191,6 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
175
191
  isOfType(input: unknown): input is Type;
176
192
  }
177
193
 
178
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
179
- readonly child: Type;
180
- constructor(child: Type);
181
- unwrap(): Type;
182
- parse(input: unknown, options?: {
183
- fast?: boolean;
184
- }): ValidationResult<SchemaInfer<Type> | undefined>;
185
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
186
- private isNull;
187
- }
188
-
189
194
  declare class SchemaNumber extends SchemaItem<number> {
190
195
  /**
191
196
  * validate that the number is less or equal than {@link value}
@@ -278,6 +283,7 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
278
283
  isOfType(input: unknown): input is ItemType<T>;
279
284
  }
280
285
 
286
+ declare function isNull(value: unknown): value is undefined | null;
281
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
282
288
  declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
283
289
  declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
@@ -285,6 +291,7 @@ declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormEleme
285
291
  declare function parceable(): (target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
286
292
  type SchemaItemStatic = new (...args: Array<any>) => SchemaItem;
287
293
  declare const Types: {
294
+ readonly Any: typeof SchemaAny;
288
295
  readonly Array: typeof SchemaArray;
289
296
  readonly Boolean: typeof SchemaBoolean;
290
297
  readonly Date: typeof SchemaDate;
@@ -300,6 +307,7 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
300
307
  private static registeredModules;
301
308
  static register(module: SchemaItemStatic): void;
302
309
  static getModule(name: string): SchemaItemStatic | undefined;
310
+ static any(): SchemaAny;
303
311
  static array<Type extends SchemaItem>(...inputs: ConstructorParameters<typeof SchemaArray<Type>>): SchemaArray<Type>;
304
312
  static boolean(...inputs: ConstructorParameters<typeof SchemaBoolean>): SchemaBoolean;
305
313
  static date(...inputs: ConstructorParameters<typeof SchemaDate>): SchemaDate;
@@ -340,4 +348,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
340
348
  }
341
349
  declare const s: typeof Schema;
342
350
 
343
- export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, type ValidationResultOld, Schema as default, parceable, parseForm, parseFormData, parseQuery, s };
351
+ export { type Infer, type Model, type ModelInfer$1 as ModelInfer, SchemaAny, SchemaArray, SchemaBoolean, SchemaDate, SchemaEnum, type SchemaInfer, SchemaItem, type SchemaItemJSON, type SchemaJSON, SchemaLiteral, SchemaNullable, SchemaNumber, SchemaObject, SchemaRecord, SchemaString, SchemaUnion, Types, type ValidationError, type ValidationResult, type ValidationResultOld, Schema as default, isNull, parceable, parseForm, parseFormData, parseQuery, s };
package/dist/Schema.js CHANGED
@@ -28,6 +28,7 @@ var __decorateClass = (decorators, target, key, kind) => {
28
28
  // src/Schema.ts
29
29
  var Schema_exports = {};
30
30
  __export(Schema_exports, {
31
+ SchemaAny: () => SchemaAny,
31
32
  SchemaArray: () => SchemaArray,
32
33
  SchemaBoolean: () => SchemaBoolean,
33
34
  SchemaDate: () => SchemaDate,
@@ -42,6 +43,7 @@ __export(Schema_exports, {
42
43
  SchemaUnion: () => SchemaUnion,
43
44
  Types: () => Types,
44
45
  default: () => Schema,
46
+ isNull: () => isNull,
45
47
  parceable: () => parceable,
46
48
  parseForm: () => parseForm,
47
49
  parseFormData: () => parseFormData,
@@ -55,7 +57,35 @@ var import_object_util3 = require("@dzeio/object-util");
55
57
 
56
58
  // src/SchemaItem.ts
57
59
  var import_object_util = require("@dzeio/object-util");
58
- var SchemaItem = class _SchemaItem {
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
+ var _SchemaItem = class _SchemaItem {
59
89
  constructor(items) {
60
90
  // standard Schema V1 spec
61
91
  this["~standard"] = {
@@ -108,6 +138,15 @@ var SchemaItem = class _SchemaItem {
108
138
  this.items = Array.isArray(items) ? items : Array.from(items);
109
139
  }
110
140
  }
141
+ defaultValue(value, strict = true) {
142
+ this.addPreProcess((input) => {
143
+ if (strict && isNull(input) || !strict && !input) {
144
+ return value;
145
+ }
146
+ return input;
147
+ });
148
+ return this;
149
+ }
111
150
  attrs(...attributes) {
112
151
  this.attributes.concat(attributes);
113
152
  return this;
@@ -191,6 +230,10 @@ var SchemaItem = class _SchemaItem {
191
230
  return this;
192
231
  }
193
232
  };
233
+ __decorateClass([
234
+ parceable()
235
+ ], _SchemaItem.prototype, "defaultValue", 1);
236
+ var SchemaItem = _SchemaItem;
194
237
 
195
238
  // src/items/array.ts
196
239
  var SchemaArray = class extends SchemaItem {
@@ -281,32 +324,6 @@ __decorateClass([
281
324
  parceable()
282
325
  ], SchemaBoolean.prototype, "parseString", 1);
283
326
 
284
- // src/items/nullable.ts
285
- var SchemaNullable = class extends SchemaItem {
286
- constructor(child) {
287
- super([child]);
288
- this.child = child;
289
- }
290
- unwrap() {
291
- return this.child;
292
- }
293
- parse(input, options) {
294
- if (this.isNull(input)) {
295
- return {
296
- valid: true,
297
- object: void 0
298
- };
299
- }
300
- return this.child.parse(input, options);
301
- }
302
- isOfType(input) {
303
- return Array.isArray(input);
304
- }
305
- isNull(value) {
306
- return typeof value === "undefined" || value === null;
307
- }
308
- };
309
-
310
327
  // src/items/object.ts
311
328
  var import_object_util2 = require("@dzeio/object-util");
312
329
  var SchemaObject = class extends SchemaItem {
@@ -349,6 +366,9 @@ var SchemaObject = class extends SchemaItem {
349
366
  };
350
367
 
351
368
  // src/helpers.ts
369
+ function isNull(value) {
370
+ return typeof value === "undefined" || value === null;
371
+ }
352
372
  function parseQuery(model, query, opts) {
353
373
  const record = {};
354
374
  for (const [key, value] of query) {
@@ -393,6 +413,14 @@ function parseForm(model, form, opts) {
393
413
  return parseFormData(model, new FormData(form), opts);
394
414
  }
395
415
 
416
+ // src/items/any.ts
417
+ var SchemaAny = class extends SchemaItem {
418
+ // @ts-expect-error input while not used is necessary for the `override to work`
419
+ isOfType(input) {
420
+ return true;
421
+ }
422
+ };
423
+
396
424
  // src/items/date.ts
397
425
  var SchemaDate = class extends SchemaItem {
398
426
  isOfType(input) {
@@ -667,6 +695,7 @@ function parceable() {
667
695
  };
668
696
  }
669
697
  var Types = {
698
+ Any: SchemaAny,
670
699
  Array: SchemaArray,
671
700
  Boolean: SchemaBoolean,
672
701
  Date: SchemaDate,
@@ -685,6 +714,9 @@ var _Schema = class _Schema extends SchemaObject {
685
714
  static getModule(name) {
686
715
  return this.registeredModules.find((it) => it.name === name);
687
716
  }
717
+ static any() {
718
+ return new SchemaAny();
719
+ }
688
720
  static array(...inputs) {
689
721
  return new SchemaArray(...inputs);
690
722
  }
@@ -792,6 +824,7 @@ var Schema = _Schema;
792
824
  var s = Schema;
793
825
  // Annotate the CommonJS export names for ESM import in node:
794
826
  0 && (module.exports = {
827
+ SchemaAny,
795
828
  SchemaArray,
796
829
  SchemaBoolean,
797
830
  SchemaDate,
@@ -805,6 +838,7 @@ var s = Schema;
805
838
  SchemaString,
806
839
  SchemaUnion,
807
840
  Types,
841
+ isNull,
808
842
  parceable,
809
843
  parseForm,
810
844
  parseFormData,
package/dist/Schema.mjs CHANGED
@@ -14,7 +14,35 @@ 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
- var SchemaItem = class _SchemaItem {
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
+ var _SchemaItem = class _SchemaItem {
18
46
  constructor(items) {
19
47
  // standard Schema V1 spec
20
48
  this["~standard"] = {
@@ -67,6 +95,15 @@ var SchemaItem = class _SchemaItem {
67
95
  this.items = Array.isArray(items) ? items : Array.from(items);
68
96
  }
69
97
  }
98
+ defaultValue(value, strict = true) {
99
+ this.addPreProcess((input) => {
100
+ if (strict && isNull(input) || !strict && !input) {
101
+ return value;
102
+ }
103
+ return input;
104
+ });
105
+ return this;
106
+ }
70
107
  attrs(...attributes) {
71
108
  this.attributes.concat(attributes);
72
109
  return this;
@@ -150,6 +187,10 @@ var SchemaItem = class _SchemaItem {
150
187
  return this;
151
188
  }
152
189
  };
190
+ __decorateClass([
191
+ parceable()
192
+ ], _SchemaItem.prototype, "defaultValue", 1);
193
+ var SchemaItem = _SchemaItem;
153
194
 
154
195
  // src/items/array.ts
155
196
  var SchemaArray = class extends SchemaItem {
@@ -240,32 +281,6 @@ __decorateClass([
240
281
  parceable()
241
282
  ], SchemaBoolean.prototype, "parseString", 1);
242
283
 
243
- // src/items/nullable.ts
244
- var SchemaNullable = class extends SchemaItem {
245
- constructor(child) {
246
- super([child]);
247
- this.child = child;
248
- }
249
- unwrap() {
250
- return this.child;
251
- }
252
- parse(input, options) {
253
- if (this.isNull(input)) {
254
- return {
255
- valid: true,
256
- object: void 0
257
- };
258
- }
259
- return this.child.parse(input, options);
260
- }
261
- isOfType(input) {
262
- return Array.isArray(input);
263
- }
264
- isNull(value) {
265
- return typeof value === "undefined" || value === null;
266
- }
267
- };
268
-
269
284
  // src/items/object.ts
270
285
  import { isObject, objectClone, objectLoop } from "@dzeio/object-util";
271
286
  var SchemaObject = class extends SchemaItem {
@@ -308,6 +323,9 @@ var SchemaObject = class extends SchemaItem {
308
323
  };
309
324
 
310
325
  // src/helpers.ts
326
+ function isNull(value) {
327
+ return typeof value === "undefined" || value === null;
328
+ }
311
329
  function parseQuery(model, query, opts) {
312
330
  const record = {};
313
331
  for (const [key, value] of query) {
@@ -352,6 +370,14 @@ function parseForm(model, form, opts) {
352
370
  return parseFormData(model, new FormData(form), opts);
353
371
  }
354
372
 
373
+ // src/items/any.ts
374
+ var SchemaAny = class extends SchemaItem {
375
+ // @ts-expect-error input while not used is necessary for the `override to work`
376
+ isOfType(input) {
377
+ return true;
378
+ }
379
+ };
380
+
355
381
  // src/items/date.ts
356
382
  var SchemaDate = class extends SchemaItem {
357
383
  isOfType(input) {
@@ -626,6 +652,7 @@ function parceable() {
626
652
  };
627
653
  }
628
654
  var Types = {
655
+ Any: SchemaAny,
629
656
  Array: SchemaArray,
630
657
  Boolean: SchemaBoolean,
631
658
  Date: SchemaDate,
@@ -644,6 +671,9 @@ var _Schema = class _Schema extends SchemaObject {
644
671
  static getModule(name) {
645
672
  return this.registeredModules.find((it) => it.name === name);
646
673
  }
674
+ static any() {
675
+ return new SchemaAny();
676
+ }
647
677
  static array(...inputs) {
648
678
  return new SchemaArray(...inputs);
649
679
  }
@@ -750,6 +780,7 @@ _Schema.registeredModules = [
750
780
  var Schema = _Schema;
751
781
  var s = Schema;
752
782
  export {
783
+ SchemaAny,
753
784
  SchemaArray,
754
785
  SchemaBoolean,
755
786
  SchemaDate,
@@ -764,6 +795,7 @@ export {
764
795
  SchemaUnion,
765
796
  Types,
766
797
  Schema as default,
798
+ isNull,
767
799
  parceable,
768
800
  parseForm,
769
801
  parseFormData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },