bridge-node-server/node_server/schemas/customKeywords/maxdp.js
Martin Donnelly 57bd6c8e6a init
2018-06-24 21:15:03 +01:00

43 lines
953 B
JavaScript

/**
* @fileOverview Custom keyword for ajv
*
* This defines a custom keyword for ajv
*/
'use strict';
module.exports = {
keyword: 'maxDecimalPlaces',
definition: {
errors: false,
async: false,
metaSchema: {
type: 'number',
minimum: 0
},
compile: doValidate
}
};
/**
* Function to validate that a number passed in has less than the given
* number of places.
*
* @param {any} schema - The number of places
* @param {Object} parentSchema - The schema
*
* @returns {Function} - The function to do the compare at runtime
*/
function doValidate(schema, parentSchema) {
var numPlaces = schema;
return function(data) {
var tempString = '' + data;
var pieces = tempString.split('.');
if (pieces.length < 2) {
return true;
} else {
return pieces[1].length <= numPlaces;
}
};
}