juzz
v0.2.0
Published
Fuzzer for Joi schema
Downloads
6
Readme
Juzz
Fuzzer for Joi
Warning this is a proof of concept and is currently a work in progress. It is mostly used by me to automatically add fuzzing to my hapi test suites so stability and accuracy though important is not a major factor.
Usage
const Joi = require('joi');
const Juzz = require('juzz');
const schema = Joi.object({
id: Joi.number().integer().min(1).max(1000).required(),
name: Joi.string().alphanum().min(1).max(32).required(),
blob: Joi.any()
});
const example = Juzz(schema);
const {errors} = schema.validate(example);
console.log(errors) // errors should not exist
console.log(example) // example will be some object that contains a valid
API
Juzz(schema, [options])
Generates a random example according to schema
schema
- a valid joi schema
options
- object with optional options
replace: (res, desc, rules) => new res
- method that allows to override generated values default((res) => res)res
- the result thatJuzz
createddesc
- the description for that item
for example to replace all strings with
'aaaaa'
use the following replace function -relace (res, desc, rules) { if (desc.type === 'string') { return 'aaaaa'; } return res; }
replace can also be useful when using custom version of joi without a standard types -
relace (res, desc, rules) { if (desc.type === 'myCustomType') { return 'aaaaa'; } return res; }
context
Object - should be identical to thecontext
object passed to joi.validate when schema references a context default({})strict
Boolean - if set to true will bail with an error when schema description is not known (can be useful for debugging) default(false)extendedTypes
array - custom types that will pass strict mode default([])extendedTypesDict
Object - Dictionary object to allow replacing custom type with built-in type for example:{customType: 'string'}
default({})stringMin
integer: used if string min is not forced in schema default(0)stringMax
integer: used if string max is not forced in schema default(100)numberMin
integer: used if number min is not forced in schema default(-10e6)numberMax
integer: used if number max is not forced in schema default(10e6)numberPrecision
integer: used if number precision is not forced in schema default(4)arrayMin
integer: used if array min is not forced in schema default(0)arrayMax
integer: used if array max is not forced in schema default(10)objectMin
integer: used if object min is not forced in schema default(0)objectMax
integer: used if object max is not forced in schema _default(10)
Known issues
It is possible to have very limited schemas or even schemas which has no valid values. In cases where Juzz
can not create a valid schema it might throw a value but can also return an undefined value or invalid value instead.
If always getting a correct schema is essential, you should always check the returned value using joi validate
method.
Schemas containing lazy values (Joi.lazy()
) are currently not supported due to their description returned from joi is not useful.
this will hopefully change in future versions.