Skip to content

Commit

Permalink
Fix wrong behavior for enum with Joi 16 & 17 joi.override (kenspiri…
Browse files Browse the repository at this point in the history
…t#36)

joi.override is supported since Joi 16 to reset valid options for one schema.
  • Loading branch information
jandppw authored Sep 20, 2022
1 parent c954eda commit dde8116
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 18 deletions.
26 changes: 25 additions & 1 deletion fixtures/joi-obj-16.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,29 @@ module.exports = joi.object().keys({
readOnlyTrue: joi.string().meta({ readOnly: true }),
readOnlyFalse: joi.string().meta({ readOnly: false }),
writeOnlyTrue: joi.string().meta({ writeOnly: true }),
writeOnlyFalse: joi.string().meta({ writeOnly: false })
writeOnlyFalse: joi.string().meta({ writeOnly: false }),
extendedEnum: joi
.string()
.valid('a', 'b', 'c')
.example('a')
.example( 'b')
.example( 'c')
.valid('x', 'y', 'z')
.example('x')
.example('y')
.example('z'),
enumWithOverride: joi
.string()
.valid('a', 'b', 'c')
.example('a')
.example( 'b')
.example( 'c')
.valid(joi.override, 'x', 'y', 'z')
.example('x', {override: true})
.example('y')
.example('z'),
enumWithEmptyOverride: joi
.string()
.valid('a', 'b', 'c')
.valid(joi.override)
}).id('person').shared(unifiedString)
26 changes: 25 additions & 1 deletion fixtures/joi-obj-17.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,29 @@ module.exports = joi.object().keys({
readOnlyTrue: joi.string().meta({ readOnly: true }),
readOnlyFalse: joi.string().meta({ readOnly: false }),
writeOnlyTrue: joi.string().meta({ writeOnly: true }),
writeOnlyFalse: joi.string().meta({ writeOnly: false })
writeOnlyFalse: joi.string().meta({ writeOnly: false }),
extendedEnum: joi
.string()
.valid('a', 'b', 'c')
.example('a')
.example( 'b')
.example( 'c')
.valid('x', 'y', 'z')
.example('x')
.example('y')
.example('z'),
enumWithOverride: joi
.string()
.valid('a', 'b', 'c')
.example('a')
.example( 'b')
.example( 'c')
.valid(joi.override, 'x', 'y', 'z')
.example('x', {override: true})
.example('y')
.example('z'),
enumWithEmptyOverride: joi
.string()
.valid('a', 'b', 'c')
.valid(joi.override)
}).id('person').shared(unifiedString)
23 changes: 21 additions & 2 deletions lib/parsers/json.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
/* eslint no-use-before-define: 'off' */
const _ = require('lodash')

/**
* Recognize the `joi.override` representation in `describe()` output.
*
* `joi.override` is a Symbol that can be used in `joi.any().valid(…)`
* statements, to reset the list of valid values. In `describe()` output, it
* turns up as an object with 1 property:
*
* ```
* { override: true }
* ```
*/
function isJoiOverride(e) {
return typeof e === 'object'
&& e !== null
&& Object.keys(e).length === 1
&& e.override === true
}

class JoiJsonSchemaParser {
constructor() {
this.childrenFieldName = this._getChildrenFieldName()
Expand Down Expand Up @@ -124,8 +142,9 @@ class JoiJsonSchemaParser {

_getEnum(fieldDefn) {
const enumList = fieldDefn[this.enumFieldName]
if (fieldDefn.flags && fieldDefn.flags.only && _.size(enumList) > 1) {
return _.uniq(enumList)
const filteredEnumList = enumList ? _.filter(enumList, e => !isJoiOverride(e)) : enumList
if (fieldDefn.flags && fieldDefn.flags.only && _.size(filteredEnumList) > 1) {
return _.uniq(filteredEnumList)
}
}

Expand Down
37 changes: 36 additions & 1 deletion outputs/json-draft-04/joi-obj-16.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
}
]
},
"genderSpecificSwitch": {
"genderSpecificSwitch": {
"oneOf": [
{
"type": "string"
Expand Down Expand Up @@ -334,6 +334,41 @@
},
"writeOnlyFalse": {
"type": "string"
},
"extendedEnum": {
"enum": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"examples": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"type": "string"
},
"enumWithOverride": {
"enum": [
"x",
"y",
"z"
],
"examples": [
"x",
"y",
"z"
],
"type": "string"
},
"enumWithEmptyOverride": {
"type": "string"
}
},
"required": [
Expand Down
35 changes: 35 additions & 0 deletions outputs/json-draft-04/joi-obj-17.json
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@
},
"writeOnlyFalse": {
"type": "string"
},
"extendedEnum": {
"enum": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"examples": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"type": "string"
},
"enumWithOverride": {
"enum": [
"x",
"y",
"z"
],
"examples": [
"x",
"y",
"z"
],
"type": "string"
},
"enumWithEmptyOverride": {
"type": "string"
}
},
"required": [
Expand Down
37 changes: 36 additions & 1 deletion outputs/json-draft-2019-09/joi-obj-16.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
}
]
},
"genderSpecificSwitch": {
"genderSpecificSwitch": {
"oneOf": [
{
"type": "string"
Expand Down Expand Up @@ -335,6 +335,41 @@
},
"writeOnlyFalse": {
"type": "string"
},
"extendedEnum": {
"enum": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"examples": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"type": "string"
},
"enumWithOverride": {
"enum": [
"x",
"y",
"z"
],
"examples": [
"x",
"y",
"z"
],
"type": "string"
},
"enumWithEmptyOverride": {
"type": "string"
}
},
"required": [
Expand Down
35 changes: 35 additions & 0 deletions outputs/json-draft-2019-09/joi-obj-17.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,41 @@
},
"writeOnlyFalse": {
"type": "string"
},
"extendedEnum": {
"enum": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"examples": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"type": "string"
},
"enumWithOverride": {
"enum": [
"x",
"y",
"z"
],
"examples": [
"x",
"y",
"z"
],
"type": "string"
},
"enumWithEmptyOverride": {
"type": "string"
}
},
"required": [
Expand Down
53 changes: 44 additions & 9 deletions outputs/json/joi-obj-16.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@
}
]
},
"genderSpecificSwitch": {
"genderSpecificSwitch": {
"oneOf": [
{
"type": "string"
},
{
"type": "number"
}
]
},
{
"type": "string"
},
{
"type": "number"
}
]
},
"height": {
"$ref": "#/$defs/unit"
},
Expand Down Expand Up @@ -334,6 +334,41 @@
},
"writeOnlyFalse": {
"type": "string"
},
"extendedEnum": {
"enum": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"examples": [
"a",
"b",
"c",
"x",
"y",
"z"
],
"type": "string"
},
"enumWithOverride": {
"enum": [
"x",
"y",
"z"
],
"examples": [
"x",
"y",
"z"
],
"type": "string"
},
"enumWithEmptyOverride": {
"type": "string"
}
},
"required": [
Expand Down
Loading

0 comments on commit dde8116

Please sign in to comment.