JSON Schema Arbitrary
🚀 Feature Request
I would like to have a method to generate arbitraries from a json schema object.
Motivation
When testing APIs that validate the input complies with a JSON schema it is a lot of work to generate arbitraries for every schema you have. Being it an standard I think it should be possible to create a function that generates the arbitrary from the schema directly.
Example
In my case I would use it to validate that any data that follow the schema is valid for other function's input.
const fc = require('fast-check');
const otherFunction = require('./function');
const schema = require('./schemas');
// Code under test
const isValidInput = (input) => {
try {
otherFunction(input);
return true;
} catch (e) {
return false;
}
}
describe('properties', () => {
it('should always contain itself', () => {
fc.assert(fc.property(fc.jsonSchema(schema), (input) => isValidInput(input)));
});
});
It has been some time since I last used the library, sorry if the code is not 100% valid.
I would take any other ideas on how to accomplish the same and I would be glad to help with the implementation if the feature gets approved.
Thanks in advance for taking the time to review.
Hey @Ceres6,
That could be a good idea. We thought of it multiple times since the beginning of fast-check and being able to magically generate data based on a schema (or typings details) seems to be great.
I do have some snippets doing so but I never pushed them up to a release as there are many challenges to deal with in order to implement it correctly. For instance most of the time when a schema asks for a numeric value, this is not any number but is might be restricted to positive integers, integer corresponding to an existing index... Same applies to all other types.
In other words: if we implement that we either need to confirm that the source constraints are expressive enough to be used as a basis to create the whole arbitrary or to offer a way to refine sub-part by sub-part the produced arbitrary. I know about similar initiative backed by zod or io-ts.
Anyway, I'd rather but that one as a side-package for now if we start building it natively.