-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from all commits
600bb3e
e05e01a
64c9a04
93f0966
f67c16c
0bdf2fd
8d67327
05a275e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
Example: | ||
|
||
|
@@ -216,3 +218,55 @@ const schema = Enjoi.schema({ | |
] | ||
}); | ||
``` | ||
|
||
### Extend Joi validation | ||
|
||
Example with root schema: | ||
```javascript | ||
const schema = Enjoi.schema({ | ||
type: 'object', | ||
properties: { | ||
title: {type: 'string'}, | ||
}, | ||
definitions: { | ||
Name: { | ||
type: 'string' | ||
} | ||
} | ||
}, { | ||
extendValidation: { | ||
'#': Joi.object().keys({title: Joi.string().max(3)}), | ||
} | ||
}); | ||
``` | ||
|
||
Example with root schema and references: | ||
```javascript | ||
const schema = Enjoi.schema({ | ||
type: 'object', | ||
properties: { | ||
title: {type: 'string'}, | ||
name: {$ref: '#/definitions/Name'}, | ||
address: {$ref: 'definitions#/Address'} | ||
}, | ||
definitions: { | ||
Name: { | ||
type: 'string' | ||
} | ||
} | ||
}, { | ||
subSchemas: { | ||
'definitions': { | ||
'Address': { | ||
'type': 'string' | ||
} | ||
} | ||
}, | ||
extendValidation: { | ||
'#': Joi.object().keys({title: Joi.string().max(3)}), | ||
'#/definitions/Name': Joi.string().max(8), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. What do you think about this example:
|
||
'definitions#/Address': Joi.string().max(7) | ||
} | ||
}); | ||
``` | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: