Skip to content

Example from documentation is misleading #2562

Open
@fappaz

Description

@fappaz

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: N/A
  • module version: 17.4.0
  • environment (e.g. node, browser, native): browser
  • used with (e.g. hapi application, another framework, standalone, ...): N/A
  • any other relevant information:

How can we help?

The example from documentation implies that the validation should pass:

schema.validate({ username: 'abc', birth_year: 1994 });
// -> { value: { username: 'abc', birth_year: 1994 } }

However, if the same schema and data above are tested against the Schema Tester (v17.3.0), the error below is thrown:

Validation Error: "value" must contain at least one of [password, access_token]

I suggest the documentation should be updated to include the error in the validation result, so it won't mislead users into thinking that the example shouldn't throw an error. Also, it could include another example in the data is valid and an error isn't returned.

Just for the record, this is the schema used:

Joi.object({
    username: Joi.string()
        .alphanum()
        .min(3)
        .max(30)
        .required(),

    password: Joi.string()
        .pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),

    repeat_password: Joi.ref('password'),

    access_token: [
        Joi.string(),
        Joi.number()
    ],

    birth_year: Joi.number()
        .integer()
        .min(1900)
        .max(2013),

    email: Joi.string()
        .email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } })
})
    .with('username', 'birth_year')
    .xor('password', 'access_token')
    .with('password', 'repeat_password')

And this is the data:

{ username: 'abc', birth_year: 1994 }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions