-
Notifications
You must be signed in to change notification settings - Fork 643
Description
This is a feature request to add custom format definitions for the JSON Schema validation performed by Ajv. It makes it much easier to insert custom validations on per-field basis while still making use of the JSON Schema. I could do this by overriding $validate, but it would basically require me to redo all the work Objection is already doing, which seems like waste.
With tv4 I could do this by importing tv4 into my own library and adding them there, but Ajv doesn't have a way to statically add formats, so I would need access to the instance. The easiest way would be to store the ajv instance on ModelBase, but that is probably weird from an API standpoint. It would also be trivial to add a addSchemaFormat or addCustomValidator function to ModelBase that looks like
// I'm used to TypeScript, so that's how I'm going to annotate this, I hope that's clear.
addCustomValidator(name: string, validator: (value: any) => boolean): void {
ajv.addFormat(name, validator);
}
This could reasonably well separate your api from theirs so if you change schema validators again this function can be adjusted, unless you switched to one that doesn't handle custom formats at all.
I understand if you reject this outright, since it's already possible. I only suggest it because it's a very simple change that your underlying tools already support. And my way of doing this would likely look just like what you're already doing, just so I have access to the ajv variable, which seems like overkill.
Edit
I somehow managed to submit this before I was done writing it, so I closed it and finish before re-opening, sorry about that.