Closed ekellstrand closed 2 years ago
Does it work if you add strict: true
to the address definition?
See https://github.com/icebob/fastest-validator#properties-10
@ColinFrick Yes! Interesting... Adding "$$strict:true" to the address definition did not work. Adding "strict:true" to the address definition, with out the dollar signs, did work.
I'll have to check and see if I should be using "strict:true" everywhere now instead of "$$strict:true".
@ekellstrand top level is $$strict. Otherwise you wouldn’t be able to add a parameter with the name strict. See https://github.com/icebob/fastest-validator#strict-validation
OK, so that gets me going. @ColinFrick
$$strict is only evaluated for the root schema. As far as I can tell it is the same behavior as if you have nested objects, children don't inherit the strict property from their parent either.
If you always want strict: true
for object validator you can set it by default:
https://github.com/icebob/fastest-validator#default-options
https://moleculer.services/docs/0.14/validating.html#Fastest-Validator (Example with options)
I only can tell you what's in the documentation, maybe @icebob can tell you more.
@ColinFrick Thank you for pointing me at the docs. The default values are new since I set this all up a few years back. I think docs are good and I'm going to close this out as "RTFM Success".
Related question: It looks like the default options handles any nested objects. Do you know if there is any config setting to handle the top level $$strict: true
as a default for all actions? or is my existing Middleware function still the best bet for that? (I re-read both sets of docs but didn't see anything)
Hi, @ColinFrick's explanation is right, no inheritance in properties, every property is evaluated on the given level and not affect the nested levels.
If you would like to add strict: true
for every object
rule, set it as a default option as Colin mentioned. You can do it in broker options, like:
// moleculer.config.js
module.exports = {
validator: {
type: "Fastest",
options: {
useNewCustomCheckerFunction: true,
defaults: {
object: {
strict: true
}
}
}
}
};
Thanks! Closing this as RTFM.
Consider the following action definition. Take note of:
Now call this service with the following payload
$$strict seems work great for top level params. (ie, If I added "I_DONT_REMEMBER_HOW_TO_SPELL_ALIAS" at the top level, right under "alias" in my payload, validation correctly rejects it). It's only when I define an object that I see this problem.
ps. Thx for your efforts. I really enjoy working with Moleculer!
Edit: Sorry, Let me give you the software version details..
I tested with Chrome 104.0.5112.102 & Postman 9.28.2