Last active
August 26, 2021 15:53
-
-
Save robintan/b72b6c483d7dadd227fe1b67243e19be to your computer and use it in GitHub Desktop.
Fastify - AJV custom validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'], | |
}, | |
} | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
commented
Aug 26, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment