joi-func
Install
npm install joi-func --save
Usage example
var Joi = var JoiFunc = var assert = // Function call metadatavar meta = args: // Array of argument schemas (optional) Joi Joi return: Joi // Schema of return value (optional)// And schema with metadatavar schema = Joi var f // Wrap your function with created schemaf = // or just using call metadataf = // ValidationError: Too less arguments passed to function//f(10) // ValidationError: Invalid argument: "value" must be a number: xxx//f('xxx', 'abc') // ValidationError: Invalid argument: "value" must be a string: 123//f(10, 123) f = // ValidationError: Invalid return: "value" must be a boolean: NOT BOOL//f(10, 'abc') // Methods with 'this' binding also worksvar obj = field: 'xxx' method:
Todo
- Joi.func().meta({throw: ...}) // function must throw error (always???)
Change log
v1.2.0
- f.joiFunc.schema = Joi-schema
- f.joiFunc.srcFunc = source (wrapped) function
- error.joiFunc.stage is:
- 'def' - JoiFunc definition error (invalid meta info)
- 'args' - arguments validation error
- 'return' - return value validation error
- 'srcFunc' - error thrown from source function