Sample custom error hook usages for type.isValid
.
Last active
January 30, 2016 21:46
-
-
Save mtth/fe006b5b001beeaed95f to your computer and use it in GitHub Desktop.
Avro error hooks
This file contains hidden or 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
/* 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)); | |
} | |
} |
This file contains hidden or 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
/* 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