Rob*_*ark 5 javascript node.js yup
我想静态创建一个 yup 模式(该模式定义一次),每次调用时都会采用一个动态变量(每次调用的变量可以不同)。这可能吗?
例如,
// 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
Run Code Online (Sandbox Code Playgroud)
(我意识到每次都可以使用该范围内的变量动态创建一个模式。但是,我正在使用许多静态定义的 yup 模式的代码库,并使用将模式映射到其相应字段的函数。我希望有一种方法能够将动态变量仅用于需要它们的几个模式,而不必将每个静态模式修改为动态的。)
要使用动态变量,需要做三件事:
Options参数与validateSync()密钥context.test()而不是箭头函数声明函数(因为 yup 将函数绑定到this)this.options.context.variableName例如,
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');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15760 次 |
| 最近记录: |