From 3b9efdfebfed9fd9327cad13a0f5f8f993088d1e Mon Sep 17 00:00:00 2001 From: Phoebe Schmidt Date: Wed, 28 Nov 2018 17:01:40 +0100 Subject: [PATCH] fix(validation): add validation schema for rich text nodes (#152) --- ...-create-rich-text-field-with-validation.js | 59 +++++++++++++++++++ .../schema/field-validations-schema.ts | 31 +++++++++- test/end-to-end/validations.spec.js | 8 +++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 examples/22-create-rich-text-field-with-validation.js diff --git a/examples/22-create-rich-text-field-with-validation.js b/examples/22-create-rich-text-field-with-validation.js new file mode 100644 index 000000000..8d9d214ba --- /dev/null +++ b/examples/22-create-rich-text-field-with-validation.js @@ -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' + } + ]); +}; diff --git a/src/lib/offline-api/validator/schema/field-validations-schema.ts b/src/lib/offline-api/validator/schema/field-validations-schema.ts index 16f7ae1bd..f90a236a4 100644 --- a/src/lib/offline-api/validator/schema/field-validations-schema.ts +++ b/src/lib/offline-api/validator/schema/field-validations-schema.ts @@ -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, @@ -41,7 +67,10 @@ const fieldValidations = Joi.alternatives().try( unique, dateRange, assetImageDimensions, - assetFileSize + assetFileSize, + nodes, + enabledMarks, + enabledNodeTypes ) export default fieldValidations diff --git a/test/end-to-end/validations.spec.js b/test/end-to-end/validations.spec.js index 62dbbbdb7..fe48a4a1a 100644 --- a/test/end-to-end/validations.spec.js +++ b/test/end-to-end/validations.spec.js @@ -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); + }); });