In this example we will add a custom keyword which checks that the value of the input is not present in the blacklist array
// These values are forbiddenconstblacklist= ['forbidden','values'];exportdefault {data() {return { options: { ajv: { keywords: { notBlackListed: {// Needs to be set to true for the validate function to be able to add custom errors errors:true,validate: (schema, value) => {// This is what Ajv wants, and it's terrible.// There is no way to add custom error messages without// modifying the validate function's `errors` propertyconstself=this.options.ajv.keywords.notBlackListed.validate;// Create an empty array to hold any errorsself.errors = [];if (this.blacklist.indexOf(value) >=0) {// This value is blacklistedself.errors.push({ message:'Value is blacklisted!', keyword:'uniqueLabel', params: { keyword:'uniqueLabel', }, }); }// Ajv needs a boolean value returned to determine if the validation was a success// If true is returned, no error is generated, even if the errors array is populatedreturnself.errors.length===0; }, }, }, }, }, model: {}, jsonSchema: { type:'object', properties: { myValue: { type:'string', notBlackListed:true, }, }, }, uiSchema: [ { component:'input', model:'myValue', fieldOptions: { on: ['change'], }, },// This component will output all the errors for myValue model { component:'div', errorHandler:true, model:'myValue', }, ], }; },};
Note that this is neither tested nor supported but in theory this would give extra Ajv features such as ajv-async and ajv-merge-patch access to the internal Ajv instance running inside vue-form-json-schema.
By adding the required plugin to the plugins section in the ajv options it is possible to load and apply the required plugin to the ajv instance that is used by vue-form-json-schema. This comes in handy when custom error messages must be added with ajv-errors.