Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Created April 10, 2014 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraintluff/10425070 to your computer and use it in GitHub Desktop.
Save geraintluff/10425070 to your computer and use it in GitHub Desktop.
var tv4coerce = require('tv4-coerce');
tv4coerce.addFix(tv4coerce.errorCodes.INVALID_TYPE, function (data, expectedType) {
if (typeof data === 'string' && expectedType === 'array') {
return data.split(',');
}
});
var data = "1,2,3";
var schema = {"type": "array", "items": {"type": "number"}};
var result = tv4coerce.coerce(data, schema);
// result.valid: true
// result.data: [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment