Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

construct custom format from a variable #101

Closed
m67hoff opened this issue Dec 7, 2015 · 1 comment
Closed

construct custom format from a variable #101

m67hoff opened this issue Dec 7, 2015 · 1 comment

Comments

@m67hoff
Copy link

m67hoff commented Dec 7, 2015

This is probably only a question and so maybe wrong here?
I try to verify an input json (express post) and also verify a "known shared SECRET" in the same "is-my-json-valid" step. The "SECRET" is set in a global environment config object (read from file):

var config.SECRET = "hellomeraki"
var validatorOptions = {
  formats: { 
    SECRET: /^hellomeraki$/ 
  }, 
  verbose: true, 
  greedy: true
}
validatorOptions.formats.SECRET = /^hellomeraki$/ 
var validate = validator(config.INPUTDATASCHEMA, validatorOptions)
...
if (!validate(req.body)) { 
...
}

no matter how I try I was not able to get it working:

validatorOptions.formats.SECRET = /^hellomeraki$/  // works but is static (has to edit source)
validatorOptions.formats.SECRET = /^config.SECRET$/   // no - var not replaced
validatorOptions.formats.SECRET = '/^' + config.SECRET + '$/'   // no - replaced but now string so: validatorOptions.formats.SECRET = /^ + config.SECRET + $/   // no - var not replaced

So has anyone a hint ?
BTW: changing it in the INPUTDATASCHEMA to type:'string', enum:['hellomeraki'] is not really an option, than the "user" has to edit not only the config but also the schema file.

@m67hoff
Copy link
Author

m67hoff commented Dec 7, 2015

Sorry for asking,

validatorOptions.formats.SECRET = new RegExp('^' + config.SECRET + '$')

works well. So std. RegExp. (I close this issue)

@m67hoff m67hoff closed this as completed Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant