@dzeio/schema 0.0.3 → 0.0.5

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
@@ -39,6 +39,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
39
39
  private readonly items?;
40
40
  private invalidError;
41
41
  constructor(items?: Array<unknown> | IArguments);
42
+ defaultValue(value: Type, strict?: boolean): this;
42
43
  attrs(...attributes: Array<string>): this;
43
44
  attr(...attributes: Array<string>): this;
44
45
  setInvalidError(err: string): this;
@@ -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
@@ -39,6 +39,7 @@ declare abstract class SchemaItem<Type = any> implements StandardSchemaV1<Type>
39
39
  private readonly items?;
40
40
  private invalidError;
41
41
  constructor(items?: Array<unknown> | IArguments);
42
+ defaultValue(value: Type, strict?: boolean): this;
42
43
  attrs(...attributes: Array<string>): this;
43
44
  attr(...attributes: Array<string>): this;
44
45
  setInvalidError(err: string): this;
@@ -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,7 @@ 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
+ var _SchemaItem = class _SchemaItem {
60
61
  constructor(items) {
61
62
  // standard Schema V1 spec
62
63
  this["~standard"] = {
@@ -109,6 +110,15 @@ var SchemaItem = class _SchemaItem {
109
110
  this.items = Array.isArray(items) ? items : Array.from(items);
110
111
  }
111
112
  }
113
+ defaultValue(value, strict = true) {
114
+ this.addPreProcess((input) => {
115
+ if (strict && isNull(input) || !strict && !input) {
116
+ return value;
117
+ }
118
+ return input;
119
+ });
120
+ return this;
121
+ }
112
122
  attrs(...attributes) {
113
123
  this.attributes.concat(attributes);
114
124
  return this;
@@ -192,6 +202,10 @@ var SchemaItem = class _SchemaItem {
192
202
  return this;
193
203
  }
194
204
  };
205
+ __decorateClass([
206
+ parceable()
207
+ ], _SchemaItem.prototype, "defaultValue", 1);
208
+ var SchemaItem = _SchemaItem;
195
209
 
196
210
  // src/items/array.ts
197
211
  var SchemaArray = class extends SchemaItem {
@@ -350,6 +364,9 @@ var SchemaObject = class extends SchemaItem {
350
364
  };
351
365
 
352
366
  // src/helpers.ts
367
+ function isNull(value) {
368
+ return typeof value === "undefined" || value === null;
369
+ }
353
370
  function parseQuery(model, query, opts) {
354
371
  const record = {};
355
372
  for (const [key, value] of query) {
@@ -660,9 +677,6 @@ var SchemaUnion = class extends SchemaItem {
660
677
  // src/Schema.ts
661
678
  function parceable() {
662
679
  return (target, propertyKey, descriptor) => {
663
- if (!(target instanceof SchemaItem)) {
664
- throw new Error("the decorator is only usable on Schema");
665
- }
666
680
  if (!(propertyKey in target)) {
667
681
  throw new Error("property not set in object");
668
682
  }
@@ -819,6 +833,7 @@ var s = Schema;
819
833
  SchemaString,
820
834
  SchemaUnion,
821
835
  Types,
836
+ isNull,
822
837
  parceable,
823
838
  parseForm,
824
839
  parseFormData,
package/dist/Schema.mjs CHANGED
@@ -14,7 +14,7 @@ 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
+ var _SchemaItem = class _SchemaItem {
18
18
  constructor(items) {
19
19
  // standard Schema V1 spec
20
20
  this["~standard"] = {
@@ -67,6 +67,15 @@ var SchemaItem = class _SchemaItem {
67
67
  this.items = Array.isArray(items) ? items : Array.from(items);
68
68
  }
69
69
  }
70
+ defaultValue(value, strict = true) {
71
+ this.addPreProcess((input) => {
72
+ if (strict && isNull(input) || !strict && !input) {
73
+ return value;
74
+ }
75
+ return input;
76
+ });
77
+ return this;
78
+ }
70
79
  attrs(...attributes) {
71
80
  this.attributes.concat(attributes);
72
81
  return this;
@@ -150,6 +159,10 @@ var SchemaItem = class _SchemaItem {
150
159
  return this;
151
160
  }
152
161
  };
162
+ __decorateClass([
163
+ parceable()
164
+ ], _SchemaItem.prototype, "defaultValue", 1);
165
+ var SchemaItem = _SchemaItem;
153
166
 
154
167
  // src/items/array.ts
155
168
  var SchemaArray = class extends SchemaItem {
@@ -308,6 +321,9 @@ var SchemaObject = class extends SchemaItem {
308
321
  };
309
322
 
310
323
  // src/helpers.ts
324
+ function isNull(value) {
325
+ return typeof value === "undefined" || value === null;
326
+ }
311
327
  function parseQuery(model, query, opts) {
312
328
  const record = {};
313
329
  for (const [key, value] of query) {
@@ -618,9 +634,6 @@ var SchemaUnion = class extends SchemaItem {
618
634
  // src/Schema.ts
619
635
  function parceable() {
620
636
  return (target, propertyKey, descriptor) => {
621
- if (!(target instanceof SchemaItem)) {
622
- throw new Error("the decorator is only usable on Schema");
623
- }
624
637
  if (!(propertyKey in target)) {
625
638
  throw new Error("property not set in object");
626
639
  }
@@ -777,6 +790,7 @@ export {
777
790
  SchemaUnion,
778
791
  Types,
779
792
  Schema as default,
793
+ isNull,
780
794
  parceable,
781
795
  parseForm,
782
796
  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.5",
4
4
  "dependencies": {
5
5
  "@dzeio/object-util": "^1.8.3"
6
6
  },