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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/extend joi validation #67

Closed

Conversation

maldimirov
Copy link
Contributor

Resolves #65 + some small additions

  • Able to extend the default Joi validation schema produced by enjoi
  • Make console warning on empty schema configurable through options - an empty schema or one that is considered empty is valid, so the warning should be removable. Refer to https://json-schema.org/latest/json-schema-core.html#rfc.section.4.3.1. The default value of the option is left to true to avoid breaking changes.
  • Tests and documentation covering the new functionality

@@ -28,6 +28,8 @@ Please file issues for other unsupported features.
- `refineType(type, format)` - an (optional) function to call to apply to type based on the type and format of the JSON schema.
- `extensions` - an array of extensions to pass [joi.extend](https://github.com/hapijs/joi/blob/master/API.md#extendextension).
- `strictMode` - make schemas `strict(value)` with a default value of `false`.
- `warnOnEmpty` - outputs a warning message on console if a schema is empty or considered empty - https://json-schema.org/latest/json-schema-core.html#rfc.section.4.3.1
- `extendValidation` - gives the ability to extend the default joi validation schema produced by `enjoi`. Works for the root schema and for references.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerned this sounds a lot like joi extensions.

Copy link
Contributor Author

@maldimirov maldimirov Dec 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sound similar, but the key is in the details. Maybe it can be called extendJoiSchemas with a bit addition to the description:

extendJoiSchemas - gives the ability to extend the default Joi validation schema produced by Enjoi. Works for the root schema and for referenced ones. In contrast to extensions which extends on a per type level, this options extends on a per schema level. Useful when you want the extend the Joi schema produced by Enjoi, without defining a new custom type.

},
extendValidation: {
'#': Joi.object().keys({title: Joi.string().max(3)}),
'#/definitions/Name': Joi.string().max(8),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to use this as an example to illustrate why this is useful. Could use currently supported json-schema to specify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. What do you think about this example:

const schema = Enjoi.schema({
    type: 'object',
    required: ['min', 'max'],
    properties: {
        lower: {type: 'number', minimum: 5},
        higher: {type: 'number', maximum: 20},
    },
}, {
    extendValidation: {
        '#': Joi.object().keys({
            higher: Joi.number().greater(Joi.ref('lower')),
        }),
    },
});

@tlivings
Copy link
Owner

I wonder if the best way to support this is to use the concept of "vendor" extensions.

For example:

{
  "type": "string",
  "x-additional-validation": ... capture somehow
}

@tlivings
Copy link
Owner

Maybe, just enable something that lets people have a general catch-all for an x- attribute which will invoke a function on an object under that key, pass the current schema to it. Could be used for almost anything - not just extending.

@maldimirov
Copy link
Contributor Author

Well the original idea (which was inspired by the needs of our project) was to stay as close to native JSON Schema and Joi as possible. x- attributes are not exactly native JSON Schema, so they conflict with this idea. We can also extend the validation by writing validation logic ourselves outside of Enjoi. I don't see what advantages would x- attributes bring us in that case.
The advantage of using Joi is the standardization of the validation definitions and the aggregation of all validation errors in one place. The latter being the bigger advantage and the main motivation for this PR.

@tlivings
Copy link
Owner

I think at this point this needs to be closed. I believe this feature would now be supported by recent merged refineSchema as well.

@tlivings tlivings closed this Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Additional joi validation
2 participants