Shape validation for input with a field named `constructor` will throw an error.
See original GitHub issueDescribe the bug
Schema validation for any object shape throws an error if the input being validated contains a field named constructor
.
To Reproduce
const yup = require("yup");
const schema = yup.object().shape({});
schema.validate({ constructor: "bar" }).then(res => console.log(res));
Results in this trace:
/project/node_modules/yup/lib/object.js:155
field = field.resolve(innerOptions);
TypeError: field.resolve is not a function
at /project/node_modules/yup/lib/object.js:155:23
at Array.forEach (<anonymous>)
at ObjectSchema._cast (/project/node_modules/yup/lib/object.js:145:11)
at ObjectSchema._validate (/project/node_modules/yup/lib/mixed.js:215:20)
at ObjectSchema._validate (/project/node_modules/yup/lib/object.js:187:47)
at ObjectSchema.validate (/project/node_modules/yup/lib/mixed.js:260:19)
at Object.<anonymous> (/project/yup-error-demonstration.js:4:8)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
Expected behavior
I expect constructor
to not be considered a special field name for object validation.
Platform (please complete the following information):
- Tested with node.js v10.15.3.
Additional context
If the developer is using yup to validate form or query data on the backend the only way to mitigate this error is to sanitize the fields before sending them to yup
. But a major feature of using yup
is the noUnknown
object to strip out unwanted fields, which cannot be relied upon if the input payload can be crafted to throw an error in yup.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
How to validate input through constructor in java
I believe you are getting compilation error like: Unhandled exception type Exception and you are getting this as you are throwing a Checked ......
Read more >Can Constructors Throw Exceptions in Java - Rollbar
The short answer is yes! Of course, properly implementing exceptions in your constructors is essential to getting the best results.
Read more >Working with Angular 4 Forms: Nesting and Input Validation
In this article, you will learn how you can work with forms and perform form validation with ease in your Angular application. In...
Read more >Error Handling in JavaScript: a Quick Guide | by Bret Cameron
Should I use throw, return or console.error? ... if (!input) reject(new Error('A validation error')); resolve(input); }); };isBlankAsync()
Read more >Function Argument Validation - MATLAB & Simulink
Argument validation is most useful in functions that can be called by any code and ... A validation function is a MATLAB function...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The
noUnknown
method for objects allows you to use yup to strip out those additional fields you don’t care about. It’s a killer feature actually!ah i see this is bug in lodash’s
cloneDeepWith
function