@bedrockio/yada 1.5.2 → 1.6.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.6.0
2
+
3
+ - Make `expandDotSyntax` option default and rename inverse option to
4
+ `preserveKeys`.
5
+
1
6
  ## 1.5.2
2
7
 
3
8
  - Removed boolean requried fields for further schema adherence.
@@ -33,12 +33,12 @@ class ObjectSchema extends _TypeSchema.default {
33
33
  fields,
34
34
  stripUnknown,
35
35
  stripEmpty,
36
- expandDotSyntax
36
+ preserveKeys
37
37
  } = options;
38
38
  if (obj) {
39
39
  const result = {};
40
- if (expandDotSyntax) {
41
- obj = expandDotProperties(obj);
40
+ if (!preserveKeys) {
41
+ obj = expandKeys(obj);
42
42
  }
43
43
  for (let key of Object.keys(obj)) {
44
44
  const value = obj[key];
@@ -259,6 +259,20 @@ class ObjectSchema extends _TypeSchema.default {
259
259
  return schema;
260
260
  }
261
261
 
262
+ /**
263
+ * `stripEmpty` - Removes properties that are empty strings.
264
+ * `stripUnknown` - Removes properties not in the schema.
265
+ * `preserveKeys` - Prevents expansion of "flat" keys using dot syntax.
266
+ *
267
+ * @param {Object} [options]
268
+ * @param {boolean} [options.stripEmpty]
269
+ * @param {boolean} [options.stripUnknown]
270
+ * @param {boolean} [options.preserveKeys]
271
+ */
272
+ options(options) {
273
+ return super.options(options);
274
+ }
275
+
262
276
  // Private
263
277
 
264
278
  toJSON(extra) {
@@ -283,23 +297,14 @@ class ObjectSchema extends _TypeSchema.default {
283
297
  };
284
298
  }
285
299
  }
286
- function expandDotProperties(obj) {
287
- const result = {};
288
- for (let [key, val] of Object.entries(obj || {})) {
289
- const split = key.split('.');
290
- if (split.length > 1) {
291
- let node = result;
292
- for (let i = 0; i < split.length; i++) {
293
- const token = split[i];
294
- if (i === split.length - 1) {
295
- node[token] = val;
296
- } else {
297
- node[token] = {};
298
- }
299
- node = node[token];
300
- }
301
- } else {
302
- result[key] = val;
300
+ function expandKeys(obj) {
301
+ const result = {
302
+ ...obj
303
+ };
304
+ for (let [key, value] of Object.entries(result)) {
305
+ if (key.includes('.')) {
306
+ delete result[key];
307
+ (0, _lodash.set)(result, key, value);
303
308
  }
304
309
  }
305
310
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/object.js CHANGED
@@ -23,11 +23,11 @@ class ObjectSchema extends TypeSchema {
23
23
  }
24
24
  });
25
25
  this.transform((obj, options) => {
26
- const { fields, stripUnknown, stripEmpty, expandDotSyntax } = options;
26
+ const { fields, stripUnknown, stripEmpty, preserveKeys } = options;
27
27
  if (obj) {
28
28
  const result = {};
29
- if (expandDotSyntax) {
30
- obj = expandDotProperties(obj);
29
+ if (!preserveKeys) {
30
+ obj = expandKeys(obj);
31
31
  }
32
32
  for (let key of Object.keys(obj)) {
33
33
  const value = obj[key];
@@ -250,6 +250,20 @@ class ObjectSchema extends TypeSchema {
250
250
  return schema;
251
251
  }
252
252
 
253
+ /**
254
+ * `stripEmpty` - Removes properties that are empty strings.
255
+ * `stripUnknown` - Removes properties not in the schema.
256
+ * `preserveKeys` - Prevents expansion of "flat" keys using dot syntax.
257
+ *
258
+ * @param {Object} [options]
259
+ * @param {boolean} [options.stripEmpty]
260
+ * @param {boolean} [options.stripUnknown]
261
+ * @param {boolean} [options.preserveKeys]
262
+ */
263
+ options(options) {
264
+ return super.options(options);
265
+ }
266
+
253
267
  // Private
254
268
 
255
269
  toJSON(extra) {
@@ -274,23 +288,12 @@ class ObjectSchema extends TypeSchema {
274
288
  }
275
289
  }
276
290
 
277
- function expandDotProperties(obj) {
278
- const result = {};
279
- for (let [key, val] of Object.entries(obj || {})) {
280
- const split = key.split('.');
281
- if (split.length > 1) {
282
- let node = result;
283
- for (let i = 0; i < split.length; i++) {
284
- const token = split[i];
285
- if (i === split.length - 1) {
286
- node[token] = val;
287
- } else {
288
- node[token] = {};
289
- }
290
- node = node[token];
291
- }
292
- } else {
293
- result[key] = val;
291
+ function expandKeys(obj) {
292
+ const result = { ...obj };
293
+ for (let [key, value] of Object.entries(result)) {
294
+ if (key.includes('.')) {
295
+ delete result[key];
296
+ set(result, key, value);
294
297
  }
295
298
  }
296
299
  return result;
package/types/object.d.ts CHANGED
@@ -71,6 +71,21 @@ declare class ObjectSchema extends TypeSchema {
71
71
  * @param {SchemaMap|Schema} arg Object or schema to append.
72
72
  */
73
73
  append(arg: SchemaMap | Schema): ObjectSchema;
74
+ /**
75
+ * `stripEmpty` - Removes properties that are empty strings.
76
+ * `stripUnknown` - Removes properties not in the schema.
77
+ * `preserveKeys` - Prevents expansion of "flat" keys using dot syntax.
78
+ *
79
+ * @param {Object} [options]
80
+ * @param {boolean} [options.stripEmpty]
81
+ * @param {boolean} [options.stripUnknown]
82
+ * @param {boolean} [options.preserveKeys]
83
+ */
84
+ options(options?: {
85
+ stripEmpty?: boolean;
86
+ stripUnknown?: boolean;
87
+ preserveKeys?: boolean;
88
+ }): this;
74
89
  }
75
90
  import Schema from './Schema';
76
91
  import TypeSchema from './TypeSchema';
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA4TA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBA9TY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAGC;IAED,cA8EC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAsB9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAY9B;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;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