Skip to content

Instantly share code, notes, and snippets.

@mtth
Last active January 30, 2016 21:46
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 mtth/fe006b5b001beeaed95f to your computer and use it in GitHub Desktop.
Save mtth/fe006b5b001beeaed95f to your computer and use it in GitHub Desktop.
Avro error hooks
/* jshint node: true */
'use strict';
var util = require('util');
/**
* Throw an error if a value isn't of the correct type.
*
* @param type {Type} The expected type.
* @param val {...} A corresponding value to check against the above type.
*
*/
function assertValid(type, val) {
return type.isValid(val, {errorHook: hook});
function hook(path, any) {
throw new Error(util.format('invalid %s: %j', path.join(), any));
}
}
/* jshint node: true */
'use strict';
/**
* Retrieve an array of all invalid nested values.
*
* @param type {Type} The expected type.
* @param val {...} A corresponding value to check against the above type.
*
*/
function collectInvalidPaths(type, val) {
var paths = [];
type.isValid(val, {errorHook: function (path) { paths.push(path.join()); }});
return paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment