Skip to content

Instantly share code, notes, and snippets.

@robintan
Last active August 26, 2021 15:53
Show Gist options
  • Save robintan/b72b6c483d7dadd227fe1b67243e19be to your computer and use it in GitHub Desktop.
Save robintan/b72b6c483d7dadd227fe1b67243e19be to your computer and use it in GitHub Desktop.
Fastify - AJV custom validator
// Step 3.
const isGeoLocation = [
function validate(data) {
const inRange = (number, min, max) => number >= min && number <= max;
const [ lat, lng ] = data.split(',');
return inRange(Number(lat), -90, 90) && inRange(Number(lng), -180, 180);
},
function errorMessage(schema, parentSchema, data) {
return `${data} is not a valid geolocation point. [lat,lng] format is required.`
},
];
module.exports = [{
method : 'GET',
url : '/location',
handler : async (req, reply) => ({ message : 'Hello World', success : true }),
schema : {
querystring : {
type : 'object',
properties : {
geoloc : {
type : 'string',
validator : isGeoLocation, // validator is the keyword registered
},
},
required: ['geoloc'],
},
}
}];
const fastify = require('fastify')({
logger: true,
});
// Step 2. Use the modified ajv instance to support custom validator
const schemaCompiler = require('./validators');
fastify.setSchemaCompiler(schemaCompiler);
// Define your route here...
const routes = require('./geolocation');
routes.forEach(fastify.route.bind(fastify));
const start = async () => {
try {
await fastify.listen(3000);
fastify.log.info(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
}
start();
// Step 1, create new AJV instance and register new keyword which will vaidate all type of schema
const Ajv = require('ajv');
const ajv = new Ajv({
removeAdditional: true,
useDefaults: true,
coerceTypes: true
});
ajv.addKeyword('validator', {
compile : (schema, parentSchema) => function validate(data) {
if (typeof schema === 'function') {
const valid = schema(data);
if (!valid) {
validate.errors = [{
keyword: 'validate',
message: `: ${data} should pass custom validation`,
params: { keyword: 'validate' },
}];
}
return valid;
}
else if (typeof schema === 'object' &&
Array.isArray(schema) &&
schema.every(f => typeof f === 'function')) {
const [ f, errorMessage ] = schema;
const valid = f(data);
if (!valid) {
validate.errors = [{
keyword: 'validate',
message: ': ' + errorMessage(schema, parentSchema, data),
params: { keyword: 'validate' },
}];
}
return valid;
}
else {
throw new Error('Invalid definition for custom validator');
}
},
errors : true,
});
module.exports = ajv.compile.bind(ajv);
@Ozerich
Copy link

Ozerich commented Aug 26, 2021

I'm trying to do the same but on fastify 3, that deprecated setSchemaCompiler. Do you have any suggestion on how to do the same on fastify 3?

fastify.setValidatorCompiler(({ schema, method, url, httpPart }) => {
    return ajv.compile(schema)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment