@bedrockio/yada 1.0.24 → 1.0.26

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
@@ -21,6 +21,7 @@ Concepts
21
21
  - [Common Methods](#common-methods)
22
22
  - [Allow](#allow)
23
23
  - [Reject](#reject)
24
+ - [Append](#append)
24
25
  - [Custom](#custom)
25
26
  - [Default](#default)
26
27
  - [Strip](#strip)
@@ -318,6 +319,25 @@ const schema = yd
318
319
  .required();
319
320
  ```
320
321
 
322
+ ### Choosing Fields
323
+
324
+ Object schemas also have a `pick` and `omit` method that allows custom field
325
+ selection. These methods accept an array of field names or enumerated arguments:
326
+
327
+ ```js
328
+ const schema = yd.object({
329
+ firstName: yd.string().required(),
330
+ lastName: yd.string().required(),
331
+ dob: yd.date(),
332
+ });
333
+
334
+ // Includes "firstName" and "lastName" but omits "dob".
335
+ schema.pick(['firstName', 'lastName']);
336
+
337
+ // Excludes "firstName" and "lastName" but keeps "dob".
338
+ schema.omit('firstName', 'lastName');
339
+ ```
340
+
321
341
  ## Date
322
342
 
323
343
  Dates are similar to the basic types with the exception that in addition to date
@@ -212,6 +212,9 @@ class Schema {
212
212
  ...this.expandExtra(extra)
213
213
  };
214
214
  }
215
+ inspect() {
216
+ return JSON.stringify(this.toOpenApi(), null, 2);
217
+ }
215
218
  expandExtra(extra = {}) {
216
219
  const {
217
220
  tag,
@@ -246,8 +249,7 @@ class Schema {
246
249
  for (let el of set) {
247
250
  if (isSchema(el)) {
248
251
  try {
249
- await el.validate(val, options);
250
- return;
252
+ return await el.validate(val, options);
251
253
  } catch (error) {
252
254
  continue;
253
255
  }
@@ -140,6 +140,32 @@ class ObjectSchema extends _TypeSchema.default {
140
140
  }
141
141
  return merged;
142
142
  }
143
+
144
+ /**
145
+ * @param {...string} [names] Names to include.
146
+ */
147
+ pick(...names) {
148
+ if (Array.isArray(names[0])) {
149
+ names = names[0];
150
+ }
151
+ const fields = pick(this.meta.fields, names);
152
+ return new ObjectSchema(fields, {
153
+ ...this.meta
154
+ });
155
+ }
156
+
157
+ /**
158
+ * @param {...string} [names] Names to exclude.
159
+ */
160
+ omit(...names) {
161
+ if (Array.isArray(names[0])) {
162
+ names = names[0];
163
+ }
164
+ const fields = omit(this.meta.fields, names);
165
+ return new ObjectSchema(fields, {
166
+ ...this.meta
167
+ });
168
+ }
143
169
  toOpenApi(extra) {
144
170
  const properties = {};
145
171
  for (let [key, schema] of Object.entries(this.getFields())) {
@@ -174,6 +200,22 @@ function expandDotProperties(obj) {
174
200
  }
175
201
  return result;
176
202
  }
203
+ function pick(obj, keys) {
204
+ const result = {};
205
+ for (let key of keys) {
206
+ result[key] = obj[key];
207
+ }
208
+ return result;
209
+ }
210
+ function omit(obj, keys) {
211
+ const result = {};
212
+ for (let key of Object.keys(obj || {})) {
213
+ if (!keys.includes(key)) {
214
+ result[key] = obj[key];
215
+ }
216
+ }
217
+ return result;
218
+ }
177
219
 
178
220
  /**
179
221
  * Creates an [object schema](https://github.com/bedrockio/yada#object).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/Schema.js CHANGED
@@ -199,6 +199,10 @@ export default class Schema {
199
199
  };
200
200
  }
201
201
 
202
+ inspect() {
203
+ return JSON.stringify(this.toOpenApi(), null, 2);
204
+ }
205
+
202
206
  expandExtra(extra = {}) {
203
207
  const { tag, ...rest } = extra;
204
208
  if (typeof extra?.tag === 'function') {
@@ -228,8 +232,7 @@ export default class Schema {
228
232
  for (let el of set) {
229
233
  if (isSchema(el)) {
230
234
  try {
231
- await el.validate(val, options);
232
- return;
235
+ return await el.validate(val, options);
233
236
  } catch (error) {
234
237
  continue;
235
238
  }
package/src/object.js CHANGED
@@ -133,6 +133,34 @@ class ObjectSchema extends TypeSchema {
133
133
  return merged;
134
134
  }
135
135
 
136
+ /**
137
+ * @param {...string} [names] Names to include.
138
+ */
139
+ pick(...names) {
140
+ if (Array.isArray(names[0])) {
141
+ names = names[0];
142
+ }
143
+ const fields = pick(this.meta.fields, names);
144
+
145
+ return new ObjectSchema(fields, {
146
+ ...this.meta,
147
+ });
148
+ }
149
+
150
+ /**
151
+ * @param {...string} [names] Names to exclude.
152
+ */
153
+ omit(...names) {
154
+ if (Array.isArray(names[0])) {
155
+ names = names[0];
156
+ }
157
+ const fields = omit(this.meta.fields, names);
158
+
159
+ return new ObjectSchema(fields, {
160
+ ...this.meta,
161
+ });
162
+ }
163
+
136
164
  toOpenApi(extra) {
137
165
  const properties = {};
138
166
  for (let [key, schema] of Object.entries(this.getFields())) {
@@ -169,6 +197,24 @@ function expandDotProperties(obj) {
169
197
  return result;
170
198
  }
171
199
 
200
+ function pick(obj, keys) {
201
+ const result = {};
202
+ for (let key of keys) {
203
+ result[key] = obj[key];
204
+ }
205
+ return result;
206
+ }
207
+
208
+ function omit(obj, keys) {
209
+ const result = {};
210
+ for (let key of Object.keys(obj || {})) {
211
+ if (!keys.includes(key)) {
212
+ result[key] = obj[key];
213
+ }
214
+ }
215
+ return result;
216
+ }
217
+
172
218
  /**
173
219
  * Creates an [object schema](https://github.com/bedrockio/yada#object).
174
220
  *
package/types/Schema.d.ts CHANGED
@@ -64,6 +64,7 @@ export default class Schema {
64
64
  */
65
65
  append(schema: any): this;
66
66
  toOpenApi(extra: any): any;
67
+ inspect(): string;
67
68
  expandExtra(extra?: {}): {};
68
69
  /**
69
70
  * @returns {this}
@@ -1 +1 @@
1
- {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AA8UA,4CAEC;AArUD;;GAEG;AAEH;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAQhB;IAED;;;;OAIG;IACH,gBAHW,eAAe,GACb,IAAI,CAmBhB;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,iDAqCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAUC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CAgChB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAUC;IAED;;;;;;;;MAoCC;CACF;8BAhUY,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAAU,CAAC"}
1
+ {"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAiVA,4CAEC;AAxUD;;GAEG;AAEH;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAQhB;IAED;;;;OAIG;IACH,gBAHW,eAAe,GACb,IAAI,CAmBhB;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,iDAqCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAUC;IAED,kBAEC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CA+BhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAUC;IAED;;;;;;;;MAoCC;CACF;8BAnUY,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAAU,CAAC"}
package/types/object.d.ts CHANGED
@@ -25,6 +25,14 @@ declare class ObjectSchema extends TypeSchema {
25
25
  * @param {SchemaMap|Schema} arg Object or schema to append.
26
26
  */
27
27
  append(arg: SchemaMap | Schema): ObjectSchema;
28
+ /**
29
+ * @param {...string} [names] Names to include.
30
+ */
31
+ pick(...names?: string[]): ObjectSchema;
32
+ /**
33
+ * @param {...string} [names] Names to exclude.
34
+ */
35
+ omit(...names?: string[]): ObjectSchema;
28
36
  }
29
37
  import Schema from "./Schema";
30
38
  import TypeSchema from "./TypeSchema";
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA2KA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;;;;AA9KD;;GAEG;AAEH;IAUE;;OAEG;IACH,cAkEC;IAED;;OAEG;IACH,kBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;CAcF"}
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAyNA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;;;;AA5ND;;GAEG;AAEH;IAUE;;OAEG;IACH,cAkEC;IAED;;OAEG;IACH,kBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;CAcF"}
package/types/foo.d.ts DELETED
@@ -1,5 +0,0 @@
1
- /**
2
- * @param {...number} [bar] wut
3
- */
4
- declare function foo(...bar?: number[]): void;
5
- //# sourceMappingURL=foo.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"foo.d.ts","sourceRoot":"","sources":["../src/foo.js"],"names":[],"mappings":"AAAA;;GAEG;AACH,8BAFc,MAAM,UAInB"}