@bedrockio/yada 1.1.2 → 1.1.3

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/README.md CHANGED
@@ -25,6 +25,7 @@ Concepts
25
25
  - [custom](#custom)
26
26
  - [default](#default)
27
27
  - [strip](#strip)
28
+ - [nullable](#nullable)
28
29
  - [message](#message)
29
30
  - [Validation Options](#validation-options)
30
31
  - [Error Messages](#error-messages)
@@ -553,6 +554,29 @@ const schema = yd.object({
553
554
  Arguments are identical to those passed to [custom](#custom). The field will be
554
555
  stripped out if the function returns a truthy value.
555
556
 
557
+ ### Nullable
558
+
559
+ The `nullable` field allows `null` to be passed for any value:
560
+
561
+ ```js
562
+ const schema = yd.string().nullable();
563
+ await schema.validate(null); // Allowed
564
+ ```
565
+
566
+ This is the equivalent of:
567
+
568
+ ```js
569
+ schema = yd.allow(schema, null);
570
+ ```
571
+
572
+ However it has two advantages. First it will correctly describe its
573
+ [OpenApi](#openapi) schema as `nullable`. It will also return the same type
574
+ schema, allowing chaining:
575
+
576
+ ```js
577
+ const schema = yd.string().nullable().trim();
578
+ ```
579
+
556
580
  ### Message
557
581
 
558
582
  The `message` method allows adding a custom message to schema or field. Note
@@ -86,6 +86,16 @@ class Schema {
86
86
  return this.assertEnum(set, false);
87
87
  }
88
88
 
89
+ /**
90
+ * Allow null. [Link](https://github.com/bedrockio/yada#nullable)
91
+ * @returns {this}
92
+ */
93
+ nullable() {
94
+ return this.clone({
95
+ nullable: true
96
+ });
97
+ }
98
+
89
99
  /**
90
100
  * @returns {this}
91
101
  */
@@ -133,7 +143,9 @@ class Schema {
133
143
  original: value
134
144
  };
135
145
  for (let assertion of this.assertions) {
136
- if (!assertion.required && value === undefined) {
146
+ if (value === undefined && !assertion.required) {
147
+ break;
148
+ } else if (value === null && options.nullable) {
137
149
  break;
138
150
  }
139
151
  try {
@@ -199,6 +211,7 @@ class Schema {
199
211
  ...tags,
200
212
  ...this.getAnyType(),
201
213
  ...this.getDefault(),
214
+ ...this.getNullable(),
202
215
  ...this.enumToOpenApi(),
203
216
  ...this.expandExtra(extra)
204
217
  };
@@ -226,8 +239,15 @@ class Schema {
226
239
  };
227
240
  }
228
241
  }
229
- inspect() {
230
- return JSON.stringify(this.toOpenApi(), null, 2);
242
+ getNullable() {
243
+ const {
244
+ nullable
245
+ } = this.meta;
246
+ if (nullable) {
247
+ return {
248
+ nullable: true
249
+ };
250
+ }
231
251
  }
232
252
  expandExtra(extra = {}) {
233
253
  const {
@@ -239,6 +259,9 @@ class Schema {
239
259
  }
240
260
  return rest;
241
261
  }
262
+ inspect() {
263
+ return JSON.stringify(this.toOpenApi(), null, 2);
264
+ }
242
265
 
243
266
  // Private
244
267
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/Schema.js CHANGED
@@ -82,6 +82,14 @@ export default class Schema {
82
82
  return this.assertEnum(set, false);
83
83
  }
84
84
 
85
+ /**
86
+ * Allow null. [Link](https://github.com/bedrockio/yada#nullable)
87
+ * @returns {this}
88
+ */
89
+ nullable() {
90
+ return this.clone({ nullable: true });
91
+ }
92
+
85
93
  /**
86
94
  * @returns {this}
87
95
  */
@@ -128,9 +136,12 @@ export default class Schema {
128
136
  };
129
137
 
130
138
  for (let assertion of this.assertions) {
131
- if (!assertion.required && value === undefined) {
139
+ if (value === undefined && !assertion.required) {
140
+ break;
141
+ } else if (value === null && options.nullable) {
132
142
  break;
133
143
  }
144
+
134
145
  try {
135
146
  const result = await this.runAssertion(assertion, value, options);
136
147
  if (result !== undefined) {
@@ -189,6 +200,7 @@ export default class Schema {
189
200
  ...tags,
190
201
  ...this.getAnyType(),
191
202
  ...this.getDefault(),
203
+ ...this.getNullable(),
192
204
  ...this.enumToOpenApi(),
193
205
  ...this.expandExtra(extra),
194
206
  };
@@ -214,8 +226,13 @@ export default class Schema {
214
226
  }
215
227
  }
216
228
 
217
- inspect() {
218
- return JSON.stringify(this.toOpenApi(), null, 2);
229
+ getNullable() {
230
+ const { nullable } = this.meta;
231
+ if (nullable) {
232
+ return {
233
+ nullable: true,
234
+ };
235
+ }
219
236
  }
220
237
 
221
238
  expandExtra(extra = {}) {
@@ -226,6 +243,10 @@ export default class Schema {
226
243
  return rest;
227
244
  }
228
245
 
246
+ inspect() {
247
+ return JSON.stringify(this.toOpenApi(), null, 2);
248
+ }
249
+
229
250
  // Private
230
251
 
231
252
  /**
package/types/Schema.d.ts CHANGED
@@ -34,6 +34,11 @@ export default class Schema {
34
34
  * @returns {this}
35
35
  */
36
36
  reject(...set: any[]): this;
37
+ /**
38
+ * Allow null. [Link](https://github.com/bedrockio/yada#nullable)
39
+ * @returns {this}
40
+ */
41
+ nullable(): this;
37
42
  /**
38
43
  * @returns {this}
39
44
  */
@@ -69,8 +74,11 @@ export default class Schema {
69
74
  } | {
70
75
  default: any;
71
76
  };
72
- inspect(): string;
77
+ getNullable(): {
78
+ nullable: boolean;
79
+ };
73
80
  expandExtra(extra?: {}): {};
81
+ inspect(): string;
74
82
  /**
75
83
  * @returns {this}
76
84
  */
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AA2WA,4CAEC;AAhWD;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDA0CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAWC;IAED;;MAOC;IAED;;;;MASC;IAED,kBAEC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CAiDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAGC;IAED;;;;;;;;MAoCC;CACF"}
1
+ {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAgYA,4CAEC;AArXD;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDA6CC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAYC;IAED;;MAOC;IAED;;;;MASC;IAED;;MAOC;IAED,4BAMC;IAED,kBAEC;IAID;;OAEG;IACH,kCAFa,IAAI,CAiDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAGC;IAED;;;;;;;;MAoCC;CACF"}