Skip to content

Commit

Permalink
fix(validation): add validation schema for rich text nodes (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoebe Schmidt authored Nov 28, 2018
1 parent 99b4976 commit 3b9efdf
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
59 changes: 59 additions & 0 deletions examples/22-create-rich-text-field-with-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Example of setting `validations` for rich text fields of a content type.
module.exports = function (migration) {
const myRichTextCT = migration.createContentType('myContentTypeWithRichText').name('MyContentTypeWithRichText');
myRichTextCT
.createField('richText')
.name('Text')
.type('RichText')
.validations([
{
nodes: {
'embedded-entry-block': [
{
size: {
min: 1,
max: 4
}
},
{
linkContentType: ['markdownContentType']
}
],
'embedded-entry-inline': [
{
size: {
min: 10,
max: 20
},
message: 'this is a custom error for number of embedded inline entries'
},
{
linkContentType: ['parent'],
message: 'we only accept parent'
}
]
}
},
{
enabledNodeTypes: [
'heading-1',
'heading-2',
'heading-3',
'heading-4',
'heading-5',
'ordered-list',
'unordered-list',
'hr',
'blockquote',
'embedded-entry-block',
'embedded-asset-block',
'hyperlink',
'entry-hyperlink',
'asset-hyperlink',
'embedded-entry-inline'
],
message:
'Only heading 1, heading 2, heading 3, heading 4, heading 5, ordered list, unordered list, horizontal rule, quote, block entry, asset, link to Url, link to entry, link to asset, and inline entry nodes are allowed'
}
]);
};
31 changes: 30 additions & 1 deletion src/lib/offline-api/validator/schema/field-validations-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ const assetImageDimensions = validation('assetImageDimensions', Joi.object({

const assetFileSize = validation('assetFileSize', range('number'))

const nodes = validation('nodes', Joi.object({
'embedded-entry-block': Joi.array(),
'embedded-entry-inline': Joi.array()
}))

const enabledMarks = validation('enabledMarks', Joi.array().items(Joi.string().valid('bold', 'italic', 'code', 'underline')))

const enabledNodeTypes = validation('enabledNodeTypes', Joi.array().items(Joi.string().valid(
'heading-1',
'heading-2',
'heading-3',
'heading-4',
'heading-5',
'heading-6',
'ordered-list',
'unordered-list',
'hr',
'blockquote',
'embedded-entry-block',
'embedded-asset-block',
'hyperlink',
'entry-hyperlink',
'asset-hyperlink',
'embedded-entry-inline'
)))

const fieldValidations = Joi.alternatives().try(
linkContentType,
inValidation,
Expand All @@ -41,7 +67,10 @@ const fieldValidations = Joi.alternatives().try(
unique,
dateRange,
assetImageDimensions,
assetFileSize
assetFileSize,
nodes,
enabledMarks,
enabledNodeTypes
)

export default fieldValidations
8 changes: 8 additions & 0 deletions test/end-to-end/validations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ describe('apply validations migration examples', function () {
done();
}));
});

it.only('successfully creates field with rich text and validations', function (done) {
cli()
.run(`--space-id ${SOURCE_TEST_SPACE} --environment-id ${environmentId} ./examples/22-create-rich-text-field-with-validation.js`)
.on(/\? Do you want to apply the migration \(Y\/n\)/).respond('y\n')
.stdout(/.* Migration successful/)
.end(done);
});
});

0 comments on commit 3b9efdf

Please sign in to comment.