@dzeio/schema 0.0.3 → 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;
@@ -179,17 +191,6 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
179
191
  isOfType(input: unknown): input is Type;
180
192
  }
181
193
 
182
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
183
- readonly child: Type;
184
- constructor(child: Type);
185
- unwrap(): Type;
186
- parse(input: unknown, options?: {
187
- fast?: boolean;
188
- }): ValidationResult<SchemaInfer<Type> | undefined>;
189
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
190
- private isNull;
191
- }
192
-
193
194
  declare class SchemaNumber extends SchemaItem<number> {
194
195
  /**
195
196
  * validate that the number is less or equal than {@link value}
@@ -282,6 +283,7 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
282
283
  isOfType(input: unknown): input is ItemType<T>;
283
284
  }
284
285
 
286
+ declare function isNull(value: unknown): value is undefined | null;
285
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
286
288
  declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
287
289
  declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
@@ -346,4 +348,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
346
348
  }
347
349
  declare const s: typeof Schema;
348
350
 
349
- 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, 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;
@@ -179,17 +191,6 @@ declare class SchemaLiteral<Type> extends SchemaItem<Type> {
179
191
  isOfType(input: unknown): input is Type;
180
192
  }
181
193
 
182
- declare class SchemaNullable<Type extends SchemaItem> extends SchemaItem<SchemaInfer<Type> | undefined> {
183
- readonly child: Type;
184
- constructor(child: Type);
185
- unwrap(): Type;
186
- parse(input: unknown, options?: {
187
- fast?: boolean;
188
- }): ValidationResult<SchemaInfer<Type> | undefined>;
189
- isOfType(input: unknown): input is SchemaInfer<Type> | undefined;
190
- private isNull;
191
- }
192
-
193
194
  declare class SchemaNumber extends SchemaItem<number> {
194
195
  /**
195
196
  * validate that the number is less or equal than {@link value}
@@ -282,6 +283,7 @@ declare class SchemaUnion<T extends Array<SchemaItem>> extends SchemaItem<ItemTy
282
283
  isOfType(input: unknown): input is ItemType<T>;
283
284
  }
284
285
 
286
+ declare function isNull(value: unknown): value is undefined | null;
285
287
  declare function parseQuery<T extends SchemaItem>(model: T, query: URLSearchParams, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
286
288
  declare function parseFormData<T extends SchemaObject>(model: T, data: FormData, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
287
289
  declare function parseForm<T extends SchemaObject>(model: T, form: HTMLFormElement, opts?: Parameters<T['parse']>[1]): ReturnType<T['parse']>;
@@ -346,4 +348,4 @@ declare class Schema<T extends Record<string, SchemaItem> = Record<string, Schem
346
348
  }
347
349
  declare const s: typeof Schema;
348
350
 
349
- 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, 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
@@ -43,6 +43,7 @@ __export(Schema_exports, {
43
43
  SchemaUnion: () => SchemaUnion,
44
44
  Types: () => Types,
45
45
  default: () => Schema,
46
+ isNull: () => isNull,
46
47
  parceable: () => parceable,
47
48
  parseForm: () => parseForm,
48
49
  parseFormData: () => parseFormData,
@@ -56,7 +57,35 @@ var import_object_util3 = require("@dzeio/object-util");
56
57
 
57
58
  // src/SchemaItem.ts
58
59
  var import_object_util = require("@dzeio/object-util");
59
- 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 {
60
89
  constructor(items) {
61
90
  // standard Schema V1 spec
62
91
  this["~standard"] = {
@@ -109,6 +138,15 @@ var SchemaItem = class _SchemaItem {
109
138
  this.items = Array.isArray(items) ? items : Array.from(items);
110
139
  }
111
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
+ }
112
150
  attrs(...attributes) {
113
151
  this.attributes.concat(attributes);
114
152
  return this;
@@ -192,6 +230,10 @@ var SchemaItem = class _SchemaItem {
192
230
  return this;
193
231
  }
194
232
  };
233
+ __decorateClass([
234
+ parceable()
235
+ ], _SchemaItem.prototype, "defaultValue", 1);
236
+ var SchemaItem = _SchemaItem;
195
237
 
196
238
  // src/items/array.ts
197
239
  var SchemaArray = class extends SchemaItem {
@@ -282,32 +324,6 @@ __decorateClass([
282
324
  parceable()
283
325
  ], SchemaBoolean.prototype, "parseString", 1);
284
326
 
285
- // src/items/nullable.ts
286
- var SchemaNullable = class extends SchemaItem {
287
- constructor(child) {
288
- super([child]);
289
- this.child = child;
290
- }
291
- unwrap() {
292
- return this.child;
293
- }
294
- parse(input, options) {
295
- if (this.isNull(input)) {
296
- return {
297
- valid: true,
298
- object: void 0
299
- };
300
- }
301
- return this.child.parse(input, options);
302
- }
303
- isOfType(input) {
304
- return Array.isArray(input);
305
- }
306
- isNull(value) {
307
- return typeof value === "undefined" || value === null;
308
- }
309
- };
310
-
311
327
  // src/items/object.ts
312
328
  var import_object_util2 = require("@dzeio/object-util");
313
329
  var SchemaObject = class extends SchemaItem {
@@ -350,6 +366,9 @@ var SchemaObject = class extends SchemaItem {
350
366
  };
351
367
 
352
368
  // src/helpers.ts
369
+ function isNull(value) {
370
+ return typeof value === "undefined" || value === null;
371
+ }
353
372
  function parseQuery(model, query, opts) {
354
373
  const record = {};
355
374
  for (const [key, value] of query) {
@@ -819,6 +838,7 @@ var s = Schema;
819
838
  SchemaString,
820
839
  SchemaUnion,
821
840
  Types,
841
+ isNull,
822
842
  parceable,
823
843
  parseForm,
824
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) {
@@ -777,6 +795,7 @@ export {
777
795
  SchemaUnion,
778
796
  Types,
779
797
  Schema as default,
798
+ isNull,
780
799
  parceable,
781
800
  parseForm,
782
801
  parseFormData,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dzeio/schema",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },