43 lines
953 B
JavaScript
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;
|
|
}
|
|
};
|
|
}
|