fastest-validator
fastest-validator copied to clipboard
allow to add metas in the schema
Is it possible to add some metas in the schemas / rules ?
like :
dot: {
$$metas: {
openapiName: 'dot'
},
$$type: "object",
x: "number", // object props here
y: "number", // object props here
}
I think it can be usefull to reuse the validation params to do others things ( like here, generating openapi from params ) .
The keys starting with $$ can be just skipped from the validation, categorized as "system properties" ? (this can also allow to add other systems properties without breaking changes)
Good idea, could you create a PR?
@icebob what did you think about something like that : https://github.com/thib3113/fastest-validator/commit/d37a2dc81a5f7f5c56274ac3dcaf7ce99bceae10 ?
My idea is to remove all keys starting with $$ . To mark them as "reserved" ( for you in the future, or other plugins and things like that ? )
so, we can do something like :
const schema = {
$$description: "user",
id: { $$description: "user Id", type: "number", positive: true, integer: true },
address: {
$$description: "delivery address"
type: "object",
props: {
street: { type: "string" },
city: { type: "string" },
zip: { type: "string", length: 5 }
}
}
}
It can be good but it's not as easy as you think :)
If you search for "$$" in the code you will see that the validator uses $$root, $$async, $$type and $$strict in different places. So you can't remove them, only others.
@icebob . yes, I know .
I tryed to run the tests with this, and no tests crash (so maybe, there is something not tested) .
Else,
$$asyncseems to be used only incompile(so beforegetRuleFromSchema) / same for$$root/ seems the same for $$strict$$typeseems to be used inresolveType(ingetRuleFromSchema) only, and only on current schema ( it will callgetRuleFromSchemaonly on sub rules ), and so I removed the $$ after
Could you create a PR?