Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Schema Arbitrary #3730

Open
Ceres6 opened this issue Apr 4, 2023 · 1 comment
Open

JSON Schema Arbitrary #3730

Ceres6 opened this issue Apr 4, 2023 · 1 comment

Comments

@Ceres6
Copy link

Ceres6 commented Apr 4, 2023

馃殌 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.

@dubzzz
Copy link
Owner

dubzzz commented Apr 24, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants