Skip to content

Feature Request: generate valid example from schema #946

Closed
@mjgpy3

Description

@mjgpy3

I just wanted to start by saying that this is an excellent library, keep up the great work!

Is there a way (or library) or would you be open to a PR that generates a random, valid example from a schema?

Something like

var schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: [Joi.string(), Joi.number()],
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');

schema.example()
// => { username: 'asdgdas', password: 'gdsaa', birthyear: 2000, email: 'foo@bar.com'  }

schema.example()
// => { username: 'dasgasdg', access_token: 12345, birthyear: 1901, email: 'asfd@aa.ext'  }

I'm asking because this would be a brutal feature in combination with something like F#'s JSON TypeProvider

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions