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
Add a ment  | 

2 Answers 2

Reset to default 6

To use a dynamic variable, 3 things are needed:

  1. use the second Options parameter to validateSync() with the context key
  2. declare the .test() function using a function expression, not an arrow function (because yup binds the function to this)
  3. 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