Skip to content

schema.describe minor issues and feature suggestions #1606

Closed
@yonjah

Description

@yonjah

I'm working on a fuzzer that would accept joi schema and would return a valid example with random data.
based on the discussion here (#946) I was using describe to
understand the schema and try to create a valid example.

I found a few places where the description is not clear enough to understand the actual schema.
Not sure if this is something joi would care to change but it can be useful

  • Joi.lazy() - does not describe in any way the schema to be lazy load.
    I think the minimum will be to return the function that can be called to retrieve the schema
    but a better solution might be to actually get the schema from the function and include it's description.
  • References are hard to decode and can be impossible to distinguish from valid strings
    For example in the following schema -
const testSchema = Joi.object().keys({
    a: Joi.alternatives().when('b', { is: Joi.ref('c'), then: 'x' }).try('y'),
    b: Joi.any(),
    c: Joi.number(),
    d: Joi.alternatives().when('b', { is: Joi.any().valid('ref:c'), then: 'x' }).try('y'),
});

console.log(require('util').inspect(testSchema.describe(), {depth: 6, colors: true}));

from the description it looks like o.a and o.d has the same definition, but o.a has to be 'x' when o.b === o.c and o.d has to be 'x' when o.b is the literal string ref:c.
I know chances that there are small chances for the schema ever use literal string start with 'ref:' are small but it still would be great to have an option to differentiate between those values and in general maybe a nicer description for references (including context references) might be more useful
like an actual object {ref: key} and {context: key}

Another feature that can be useful (but might be a bit specific to my use case) is to include the schema as part of the description.
This can be useful test if the generated value actually meet the required description (especially in the case of alternatives and references where you want to check if a value meet a certain condition)
alternative option will be to have a reverse method that will regenerate a schema out of a description, but it will probably be much harder to implement

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing codefeatureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions