Created
November 27, 2014 15:22
-
-
Save defel/27bf93be3cc449430df8 to your computer and use it in GitHub Desktop.
z-schema floating point issue script
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
var | |
ZSchema = require('z-schema'), | |
options = {}, | |
validator = new ZSchema(options), | |
fs = require('fs'), | |
schema, | |
schemaValid; | |
schema = { | |
"type": "object", | |
"properties": { | |
"decimal": { | |
"type": "number", | |
"multipleOf": 0.01 | |
} | |
} | |
}; | |
var schemaValid = validator.validateSchema(schema); | |
if(!schemaValid) { | |
console.error('Schema invalid: ', validator.getLastErrors()); | |
return false; | |
} | |
[100, 100.10, 136.67].forEach(function(value) { | |
var | |
json = {"decimal": value}, | |
valid = validator.validate(json, schema), | |
err = validator.getLastErrors(); | |
if(err) { | |
console.error('value ' + value + ' is not valid!'); | |
formatError(err); | |
} | |
if(valid) { | |
console.log('value ' + value + ' is valid'); | |
} | |
}); | |
function formatError(err) { | |
err.forEach(function(errBlock) { | |
console.log( | |
"\n", | |
"Validation Error: \n", | |
"Code: ", errBlock.code, "\n", | |
"Message: ", errBlock.message, "\n", | |
"Path: ", errBlock.path, "\n" | |
); | |
if(errBlock.inner) { | |
console.log("====================\n"); | |
formatError(errBlock.inner); | |
formatError(errBlock.inner); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment