Skip to content

Commit 210dc05

Browse files
authored
Fix deep nested dynamic types validation (#138)
* Fix deep nested dynamic types validation * Address PR comments
1 parent 1da1204 commit 210dc05

File tree

7 files changed

+124
-10
lines changed

7 files changed

+124
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.1 - 2020-06-15
2+
3+
Fix:
4+
5+
- Fix deep nested dynamic types validation [[#132](https://github.com/talyssonoc/structure/issues/132)]
6+
17
## 2.0.0 - 2020-03-31
28

39
Refactors:

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"packages": ["packages/*"],
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"npmClient": "yarn",
55
"useWorkspaces": true
66
}

packages/jest-structure/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jest-structure",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "Jest assertions to use with Structure",
55
"main": "index.js",
66
"author": "Talysson <[email protected]>",
@@ -15,7 +15,7 @@
1515
"node": ">=10.13.0"
1616
},
1717
"devDependencies": {
18-
"structure": "2.0.0"
18+
"structure": "2.0.1"
1919
},
2020
"peerDependencies": {
2121
"jest": "^25.1.0"

packages/structure/dist/structure.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/structure/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "structure",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A simple schema/attributes library built on top of modern JavaScript",
55
"main": "src/index.js",
66
"browser": "dist/structure.js",
@@ -52,7 +52,7 @@
5252
"babel-loader": "^8.1.0",
5353
"coveralls": "^3.1.0",
5454
"electron": "^9.0.0",
55-
"jest-structure": "2.0.0",
55+
"jest-structure": "2.0.1",
5656
"webpack": "^4.41.2",
5757
"webpack-cli": "^3.3.9"
5858
}

packages/structure/src/validation/validations/nested.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,32 @@ function getNestedValidations(typeSchema) {
4545
return joiSchema;
4646
}
4747

48-
exports.resolveDynamicLinks = function resolveDynamicLinks({ schema, joiValidation }) {
48+
const resolveDynamicLinks = function resolveDynamicLinks({ schema, joiValidation }) {
4949
return schema.attributeDefinitions.reduce((joiValidation, attributeDefinition) => {
5050
if (!attributeDefinition.hasDynamicType) {
5151
return joiValidation;
5252
}
5353

5454
const type = attributeDefinition.resolveType();
55+
const nestedSchema = type[SCHEMA];
5556

56-
if (!type[SCHEMA]) {
57+
// warning: uses Joi internals
58+
// https://github.com/hapijs/joi/blob/v16.1.8/lib/types/any.js#L72 ⤵
59+
// https://github.com/hapijs/joi/blob/v16.1.8/lib/base.js#L699 ⤵
60+
// https://github.com/hapijs/joi/blob/v16.1.8/lib/modify.js#L149
61+
if (!nestedSchema || joiValidation._ids._get(nestedSchema.identifier)) {
5762
return joiValidation;
5863
}
5964

60-
const attributeValidation = type[SCHEMA].validation;
65+
const attributeValidation = nestedSchema.validation;
6166

62-
return joiValidation.shared(attributeValidation.joiValidation);
67+
const sharedValidation = joiValidation.shared(attributeValidation.joiValidation);
68+
69+
return resolveDynamicLinks({
70+
schema: nestedSchema,
71+
joiValidation: sharedValidation,
72+
});
6373
}, joiValidation);
6474
};
75+
76+
exports.resolveDynamicLinks = resolveDynamicLinks;

packages/structure/test/unit/validation/nestedStructure.spec.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,5 +421,101 @@ describe('validation', () => {
421421
});
422422
});
423423
});
424+
425+
describe('when nesting is deep', () => {
426+
it('validates properly with manual validation', () => {
427+
const Vehicle = attributes({
428+
year: {
429+
type: Number,
430+
required: true,
431+
},
432+
})(class Vehicle {});
433+
434+
const UserPersonalInformation = attributes(
435+
{
436+
name: String,
437+
vehicle: 'Vehicle',
438+
},
439+
{
440+
dynamics: {
441+
Vehicle: () => Vehicle,
442+
},
443+
}
444+
)(class UserPersonalInformation {});
445+
446+
const AutoRiskProfile = attributes(
447+
{
448+
userPersonalInformation: {
449+
type: 'UserPersonalInformation',
450+
required: true,
451+
},
452+
},
453+
{
454+
dynamics: {
455+
UserPersonalInformation: () => UserPersonalInformation,
456+
},
457+
}
458+
)(class AutoRiskProfile {});
459+
460+
expect(() => {
461+
const autoRiskProfile = new AutoRiskProfile({
462+
userPersonalInformation: new UserPersonalInformation({
463+
name: 'a',
464+
vehicle: new Vehicle({
465+
year: 2018,
466+
}),
467+
}),
468+
});
469+
470+
autoRiskProfile.validate();
471+
}).not.toThrow();
472+
});
473+
474+
it('validates properly with strict mode', () => {
475+
const Vehicle = attributes({
476+
year: {
477+
type: Number,
478+
required: true,
479+
},
480+
})(class Vehicle {});
481+
482+
const UserPersonalInformation = attributes(
483+
{
484+
name: String,
485+
vehicle: 'Vehicle',
486+
},
487+
{
488+
dynamics: {
489+
Vehicle: () => Vehicle,
490+
},
491+
}
492+
)(class UserPersonalInformation {});
493+
494+
const AutoRiskProfile = attributes(
495+
{
496+
userPersonalInformation: {
497+
type: 'UserPersonalInformation',
498+
required: true,
499+
},
500+
},
501+
{
502+
dynamics: {
503+
UserPersonalInformation: () => UserPersonalInformation,
504+
},
505+
}
506+
)(class AutoRiskProfile {});
507+
508+
expect(() => {
509+
AutoRiskProfile.buildStrict({
510+
userPersonalInformation: new UserPersonalInformation({
511+
name: 'a',
512+
vehicle: new Vehicle({
513+
year: 2018,
514+
}),
515+
}),
516+
});
517+
}).not.toThrow();
518+
});
519+
});
424520
});
425521
});

0 commit comments

Comments
 (0)