const Ajv = require('ajv').default;
const standaloneCode = require('ajv/dist/standalone').default;
const hexCodeValidator = { type: 'string', format: 'hex-code' };
const demoSchema = {
$id: 'demoSchema',
type: 'object',
properties: {
test: hexCodeValidator,
},
};
const ajv = new Ajv({
code: {
source: true, // this option is required to generate standalone code
},
removeAdditional: true,
});
const hexCodeRegex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i;
ajv.addFormat('hex-code', {
validate: hexCode => hexCodeRegex.test(hexCode),
});
ajv.addSchema(demoSchema);
const moduleCode = standaloneCode(ajv);