admin管理员组文章数量:1290929
I want to statically create a yup schema (the schema is defined once) that takes a dynamic variable each time it's called (the variable can be different with each call). Is this possible?
e.g.,
// file: schema.js
// create the schema once
yup = require('yup');
const schema = yup.mixed().test(
'my-test-name',
'cannot be an existing value',
value => !myArray.includes(value) // How to reference myArray here?
// As written, it results in "ReferenceError: myArray is not defined"
);
module.exports = schema;
// other file that imports the schema:
schema = require('./schema.js');
let myArray = ['blue', 'green'];
schema.validateSync('yellow'); // should pass validation, because 'yellow' not in myArray
myArray = ['orange', 'yellow'];
schema.validateSync('yellow'); // should fail validation, because 'yellow' is in myArray
(I realize it's possible to dynamically create a schema each time with a variable in that scope. However, I'm working in a codebase with many statically-defined yup schemas, with a function mapping the schemas to their corresponding fields. I'm hoping for a way to be able to be able to use dynamic variables for just a couple of those schemas that need them, and not have to modify every static schema to be dynamic.)
I want to statically create a yup schema (the schema is defined once) that takes a dynamic variable each time it's called (the variable can be different with each call). Is this possible?
e.g.,
// file: schema.js
// create the schema once
yup = require('yup');
const schema = yup.mixed().test(
'my-test-name',
'cannot be an existing value',
value => !myArray.includes(value) // How to reference myArray here?
// As written, it results in "ReferenceError: myArray is not defined"
);
module.exports = schema;
// other file that imports the schema:
schema = require('./schema.js');
let myArray = ['blue', 'green'];
schema.validateSync('yellow'); // should pass validation, because 'yellow' not in myArray
myArray = ['orange', 'yellow'];
schema.validateSync('yellow'); // should fail validation, because 'yellow' is in myArray
(I realize it's possible to dynamically create a schema each time with a variable in that scope. However, I'm working in a codebase with many statically-defined yup schemas, with a function mapping the schemas to their corresponding fields. I'm hoping for a way to be able to be able to use dynamic variables for just a couple of those schemas that need them, and not have to modify every static schema to be dynamic.)
Share Improve this question edited Oct 11, 2018 at 4:30 Rob Bednark asked Oct 10, 2018 at 5:51 Rob BednarkRob Bednark 28.2k27 gold badges88 silver badges129 bronze badges 1- did you have any issue like this one stackoverflow./questions/70316058/… ? – PanosCool Commented Dec 12, 2021 at 11:52
2 Answers
Reset to default 6To use a dynamic variable, 3 things are needed:
- use the second
Options
parameter tovalidateSync()
with thecontext
key - declare the
.test()
function using a function expression, not an arrow function (because yup binds the function tothis
) - inside the test function, reference the dynamic variable with
this.options.context.variableName
e.g.,
const yup = require('yup');
// statically declare the schema
const schema = yup.mixed().test(
'my-test-name',
'cannot be an existing value', // error message
function test(value) {
// NOTE: this must not be an arrow function, because yup binds it to it's "this"
// Note the use of this.options.context to reference the dynamic variable
return !this.options.context.myArray.includes(value)
}
);
// Note the use of passing a { context: ... } as the second "options" parameter to validateSync()
ret = schema.validateSync('yellow', { context: { myArray: ['blue', 'green'] } } );
console.assert(ret === 'yellow'); // passes validation
let errorMessage;
try {
schema.validateSync('blue', { context: { myArray: ['blue', 'green'] } } );
}
catch(error) {
errorMessage = error.message;
}
console.assert(errorMessage === 'cannot be an existing value');
- https://github./jquense/yup#mixedtestname-string-message-string--function-test-function-schema
- https://github./jquense/yup#mixedvalidatevalue-any-options-object-promiseany-validationerror
Try exporting a function that creates a dynamic schema. Please see below.
// file: schema.js
// create the schema once
yup = require('yup');
// export as a function
module.exports = myArray => {
return yup.mixed().test(
'my-test-name',
'cannot be an existing value',
value => !myArray.includes(value)
);
};
// other file that imports the schema:
schema = require('./schema.js');
let myArray = ['blue', 'green'];
let blueGreenSchema = schema(myArray);
blueGreenSchema.validateSync('yellow');
myArray = ['orange', 'yellow'];
let orangeYellowSchema = schema(myArray);
orangeYellowSchema.validateSync('yellow');
本文标签: javascriptHow to use a dynamic variable in a static yup schemaStack Overflow
版权声明:本文标题:javascript - How to use a dynamic variable in a static yup schema? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523183a2383310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论