Skip to content

Commit

Permalink
fix(validation): shouldPublish should accept 'preserve' in addition t…
Browse files Browse the repository at this point in the history
…o 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)
  • Loading branch information
jarkkosyrjala authored and Khaledgarbaya committed Jan 9, 2019
1 parent 74eb6e4 commit 1e0a82c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/lib/intent-validator/content-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ContentTransformIntentValidator extends SchemaValidator {
from: Joi.array().items(Joi.string()).required(),
to: Joi.array().items(Joi.string()).required(),
transformEntryForLocale: Joi.func().required(),
shouldPublish: Joi.boolean()
shouldPublish: Joi.alternatives().try(Joi.boolean(), Joi.string().valid('preserve'))
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/lib/intent-validator/schema-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const validationErrors = {
},
INVALID_PROPERTY_TYPE: (propName, typeName, actualType, expectedType) => {
return `"${actualType}" is not a valid type for the ${typeName} property "${propName}". Expected "${expectedType}".`
},
INVALID_VALUE_IN_ALTERNATIVES: (propName, typeName, value, schemaInnerMatches) => {
const expectedTypes = schemaInnerMatches.map( (match) => {
const validsSet = match.schema._valids._set
return validsSet.length > 0 ? `${match.schema._type} value ${validsSet.map(validValue => `"${validValue}"`).join(' or ')}` : `type ${match.schema._type}`
})
return `"${value}" is not a valid value for the ${typeName} property "${propName}". Expected ${expectedTypes.join(' or ')}.`
}
}

Expand Down Expand Up @@ -81,8 +88,10 @@ abstract class SchemaValidator implements IntentValidator {
expectedType = 'function'
}
const actualType = kindOf(value)

const message = validationErrors.INVALID_PROPERTY_TYPE(propName, displayName, actualType, expectedType)
const message =
expectedType === 'alternatives' ?
validationErrors.INVALID_VALUE_IN_ALTERNATIVES(propName, displayName, error._object, schema._inner.matches)
: validationErrors.INVALID_PROPERTY_TYPE(propName, displayName, actualType, expectedType)
errors.push({
type: 'InvalidType',
message,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/intent-validator/content-transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('Content transformation', function () {
type: 'contentType/transformEntries'
}
},
message: '"string" is not a valid type for the content transformation property "shouldPublish". Expected "boolean".',
message: '"yes please" is not a valid value for the content transformation property "shouldPublish". Expected type boolean or string value "preserve".',
type: 'InvalidType'
}
])
Expand Down

0 comments on commit 1e0a82c

Please sign in to comment.