@bedrockio/yada 1.10.2 → 1.10.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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 1.10.3
2
+
3
+ - Fix for strip empty allowing required fields.
4
+ - Fix for flat null property being stripped.
5
+ - Fix for defaults not being respected with flat keys.
6
+
1
7
  ## 1.10.2
2
8
 
3
9
  - Shortcut methods for object options.
@@ -63,10 +63,7 @@ class ObjectSchema extends _TypeSchema.default {
63
63
  let errors = [];
64
64
  const result = {};
65
65
  for (let key of keys) {
66
- const value = passed[key];
67
- if (value === '' && stripEmpty) {
68
- continue;
69
- }
66
+ let value = passed[key];
70
67
  const schema = getSchema(fields, key, options);
71
68
  if (!schema) {
72
69
  if (stripUnknown) {
@@ -81,7 +78,19 @@ class ObjectSchema extends _TypeSchema.default {
81
78
  continue;
82
79
  }
83
80
  try {
84
- const validateOptions = {
81
+ let isBaseObject = false;
82
+ if (allowFlatKeys && value === undefined) {
83
+ // When allowing keys like "profile.name", "profile" must still
84
+ // be validated, but will not exist so expand the passed object
85
+ // and take the base value.
86
+ value = expandFlatSyntax(passed)[key];
87
+
88
+ // If the flat key did not exist but the expanded key did then
89
+ // we have a "base object". If both keys do not exist then the
90
+ // validation result needs to be set as it could be a default.
91
+ isBaseObject = value !== undefined;
92
+ }
93
+ const transformed = await schema.validate(value, {
85
94
  ...options,
86
95
  path: [...path, key],
87
96
  required: schema.meta.required,
@@ -94,18 +103,16 @@ class ObjectSchema extends _TypeSchema.default {
94
103
  // The original root represents the root object
95
104
  // before it was transformed.
96
105
  originalRoot: original
97
- };
98
- if (allowFlatKeys && !value) {
99
- // When allowing keys like "profile.name", "profile" will
100
- // not be passed so expand the passed object and make sure
101
- // the base validates, but do not transform the result.
102
- const expanded = expandFlatSyntax(passed);
103
- await schema.validate(expanded[key], validateOptions);
104
- } else {
105
- const transformed = await schema.validate(value, validateOptions);
106
- if (transformed !== undefined) {
107
- result[key] = transformed;
108
- }
106
+ });
107
+ if (transformed === '' && stripEmpty) {
108
+ continue;
109
+ } else if (isBaseObject) {
110
+ // We do not want to return the "profile" object in the
111
+ // result when only flat keys are passed, so continue here.
112
+ continue;
113
+ }
114
+ if (transformed !== undefined) {
115
+ result[key] = transformed;
109
116
  }
110
117
  } catch (error) {
111
118
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.10.2",
3
+ "version": "1.10.3",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/object.js CHANGED
@@ -66,11 +66,7 @@ class ObjectSchema extends TypeSchema {
66
66
  const result = {};
67
67
 
68
68
  for (let key of keys) {
69
- const value = passed[key];
70
-
71
- if (value === '' && stripEmpty) {
72
- continue;
73
- }
69
+ let value = passed[key];
74
70
 
75
71
  const schema = getSchema(fields, key, options);
76
72
 
@@ -89,7 +85,21 @@ class ObjectSchema extends TypeSchema {
89
85
  }
90
86
 
91
87
  try {
92
- const validateOptions = {
88
+ let isBaseObject = false;
89
+
90
+ if (allowFlatKeys && value === undefined) {
91
+ // When allowing keys like "profile.name", "profile" must still
92
+ // be validated, but will not exist so expand the passed object
93
+ // and take the base value.
94
+ value = expandFlatSyntax(passed)[key];
95
+
96
+ // If the flat key did not exist but the expanded key did then
97
+ // we have a "base object". If both keys do not exist then the
98
+ // validation result needs to be set as it could be a default.
99
+ isBaseObject = value !== undefined;
100
+ }
101
+
102
+ const transformed = await schema.validate(value, {
93
103
  ...options,
94
104
 
95
105
  path: [...path, key],
@@ -105,20 +115,18 @@ class ObjectSchema extends TypeSchema {
105
115
  // The original root represents the root object
106
116
  // before it was transformed.
107
117
  originalRoot: original,
108
- };
109
-
110
- if (allowFlatKeys && !value) {
111
- // When allowing keys like "profile.name", "profile" will
112
- // not be passed so expand the passed object and make sure
113
- // the base validates, but do not transform the result.
114
- const expanded = expandFlatSyntax(passed);
115
-
116
- await schema.validate(expanded[key], validateOptions);
117
- } else {
118
- const transformed = await schema.validate(value, validateOptions);
119
- if (transformed !== undefined) {
120
- result[key] = transformed;
121
- }
118
+ });
119
+
120
+ if (transformed === '' && stripEmpty) {
121
+ continue;
122
+ } else if (isBaseObject) {
123
+ // We do not want to return the "profile" object in the
124
+ // result when only flat keys are passed, so continue here.
125
+ continue;
126
+ }
127
+
128
+ if (transformed !== undefined) {
129
+ result[key] = transformed;
122
130
  }
123
131
  } catch (error) {
124
132
  const { message } = schema.meta;
@@ -1 +1 @@
1
- {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA8fA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBA9fY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAIC;IAED,sBAOC;IAED,cAyGC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAkB9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAc9B;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;;;;OAOG;IACH,mBAHc,MAAM,EAAA,gBAuBnB;IAED;;OAEG;IACH,2BAQC;IAED;;;;;;OAMG;IACH,yCAFa,IAAI,CAiBhB;IAED;;;;;;OAMG;IACH,cAEC;IAED;;;;;;;;;OASG;IACH,YAFW,SAAS,GAAC,MAAM,gBA+B1B;IAID;;OAEG;IACH,mBAIC;IAED;;OAEG;IACH,qBAIC;IAED;;OAEG;IACH,sBAIC;IAED;;OAEG;IACH,uBAIC;IAED;;;;;;;;;;;OAWG;IACH,kBALG;QAA0B,UAAU,GAA5B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,aAAa,GAA/B,OAAO;QACW,cAAc,GAAhC,OAAO;KAA0B,QAI3C;IAID,gCAmBC;CACF;mBApZgC,UAAU;uBACpB,cAAc"}
1
+ {"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAsgBA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBAtgBY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAIC;IAED,sBAOC;IAED,cAiHC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAkB9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAc9B;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;;;;OAOG;IACH,mBAHc,MAAM,EAAA,gBAuBnB;IAED;;OAEG;IACH,2BAQC;IAED;;;;;;OAMG;IACH,yCAFa,IAAI,CAiBhB;IAED;;;;;;OAMG;IACH,cAEC;IAED;;;;;;;;;OASG;IACH,YAFW,SAAS,GAAC,MAAM,gBA+B1B;IAID;;OAEG;IACH,mBAIC;IAED;;OAEG;IACH,qBAIC;IAED;;OAEG;IACH,sBAIC;IAED;;OAEG;IACH,uBAIC;IAED;;;;;;;;;;;OAWG;IACH,kBALG;QAA0B,UAAU,GAA5B,OAAO;QACW,YAAY,GAA9B,OAAO;QACW,aAAa,GAA/B,OAAO;QACW,cAAc,GAAhC,OAAO;KAA0B,QAI3C;IAID,gCAmBC;CACF;mBA5ZgC,UAAU;uBACpB,cAAc"}