Skip to content

Instantly share code, notes, and snippets.

@robintan
robintan / routes.js
Created December 11, 2019 13:55
Fastify - Number validator example
module.exports = [{
method : 'GET',
url : '/checkNumbers',
handler : async (req, reply) => ({ message : 'Yay! Success!, success : true }),
schema : {
querystring : {
type : 'object',
properties : {
positive_number : {
type : 'number',
@robintan
robintan / geolocation.js
Last active August 26, 2021 15:53
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.`
},