Skip to content

Commit 1e0a82c

Browse files
jarkkosyrjalaKhaledgarbaya
authored andcommitted
fix(validation): shouldPublish should accept 'preserve' in addition to boolean values (#165)
## Summary shouldPublish should accept 'preserve' in addition to boolean values ## Description Add 'preserve' as a valid option to validations for shouldPublish field ## Motivation and Context #143 (comment)
1 parent 74eb6e4 commit 1e0a82c

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/lib/intent-validator/content-transform.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ContentTransformIntentValidator extends SchemaValidator {
2323
from: Joi.array().items(Joi.string()).required(),
2424
to: Joi.array().items(Joi.string()).required(),
2525
transformEntryForLocale: Joi.func().required(),
26-
shouldPublish: Joi.boolean()
26+
shouldPublish: Joi.alternatives().try(Joi.boolean(), Joi.string().valid('preserve'))
2727
}
2828
}
2929
}

src/lib/intent-validator/schema-validator.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const validationErrors = {
1616
},
1717
INVALID_PROPERTY_TYPE: (propName, typeName, actualType, expectedType) => {
1818
return `"${actualType}" is not a valid type for the ${typeName} property "${propName}". Expected "${expectedType}".`
19+
},
20+
INVALID_VALUE_IN_ALTERNATIVES: (propName, typeName, value, schemaInnerMatches) => {
21+
const expectedTypes = schemaInnerMatches.map( (match) => {
22+
const validsSet = match.schema._valids._set
23+
return validsSet.length > 0 ? `${match.schema._type} value ${validsSet.map(validValue => `"${validValue}"`).join(' or ')}` : `type ${match.schema._type}`
24+
})
25+
return `"${value}" is not a valid value for the ${typeName} property "${propName}". Expected ${expectedTypes.join(' or ')}.`
1926
}
2027
}
2128

@@ -81,8 +88,10 @@ abstract class SchemaValidator implements IntentValidator {
8188
expectedType = 'function'
8289
}
8390
const actualType = kindOf(value)
84-
85-
const message = validationErrors.INVALID_PROPERTY_TYPE(propName, displayName, actualType, expectedType)
91+
const message =
92+
expectedType === 'alternatives' ?
93+
validationErrors.INVALID_VALUE_IN_ALTERNATIVES(propName, displayName, error._object, schema._inner.matches)
94+
: validationErrors.INVALID_PROPERTY_TYPE(propName, displayName, actualType, expectedType)
8695
errors.push({
8796
type: 'InvalidType',
8897
message,

test/unit/lib/intent-validator/content-transform.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('Content transformation', function () {
157157
type: 'contentType/transformEntries'
158158
}
159159
},
160-
message: '"string" is not a valid type for the content transformation property "shouldPublish". Expected "boolean".',
160+
message: '"yes please" is not a valid value for the content transformation property "shouldPublish". Expected type boolean or string value "preserve".',
161161
type: 'InvalidType'
162162
}
163163
])

0 commit comments

Comments
 (0)