From b4fd2b1976ef8819d309fe49247ccf9b28b66d05 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 27 Dec 2020 13:06:53 -0500 Subject: [PATCH] adds default value to block field schema validation --- demo/collections/DefaultValueTest.ts | 275 --------------------------- demo/collections/DefaultValues.ts | 9 + src/fields/config/schema.ts | 1 + 3 files changed, 10 insertions(+), 275 deletions(-) delete mode 100644 demo/collections/DefaultValueTest.ts diff --git a/demo/collections/DefaultValueTest.ts b/demo/collections/DefaultValueTest.ts deleted file mode 100644 index 3de6136de7..0000000000 --- a/demo/collections/DefaultValueTest.ts +++ /dev/null @@ -1,275 +0,0 @@ -import checkRole from '../access/checkRole'; -import Email from '../blocks/Email'; -import Quote from '../blocks/Quote'; -import NumberBlock from '../blocks/Number'; -import CallToAction from '../blocks/CallToAction'; - -const DefaultValueTest = { - slug: 'default-value-test', - labels: { - singular: 'Default Value Test', - plural: 'Default Value Tests', - }, - admin: { - useAsTitle: 'text', - }, - preview: (doc, token) => { - if (doc && doc.text) { - return `http://localhost:3000/previewable-posts/${doc.text.value}?preview=true&token=${token}`; - } - - return null; - }, - access: { - read: () => true, - }, - fields: [ - { - name: 'text', - type: 'text', - label: 'Text', - defaultValue: 'Default Value', - unique: true, - access: { - create: ({ req: { user } }) => checkRole(['admin'], user), - update: ({ req: { user } }) => checkRole(['admin'], user), - read: ({ req: { user } }) => Boolean(user), - }, - }, - { - name: 'image', - type: 'upload', - label: 'Image', - relationTo: 'media', - }, - { - name: 'select', - label: 'Select', - type: 'select', - options: [{ - value: 'option-1', - label: 'Option 1 Label', - }, { - value: 'option-2', - label: 'Option 2 Label', - }, { - value: 'option-3', - label: 'Option 3 Label', - }, { - value: 'option-4', - label: 'Option 4 Label', - }], - defaultValue: 'option-1', - }, - { - name: 'selectMany', - label: 'Select w/ hasMany', - type: 'select', - options: [{ - value: 'option-1', - label: 'Option 1 Label', - }, { - value: 'option-2', - label: 'Option 2 Label', - }, { - value: 'option-3', - label: 'Option 3 Label', - }, { - value: 'option-4', - label: 'Option 4 Label', - }], - defaultValue: ['option-1', 'option-4'], - hasMany: true, - }, - { - name: 'radioGroupExample', - label: 'Radio Group Example', - type: 'radio', - options: [{ - value: 'option-1', - label: 'Options 1 Label', - }, { - value: 'option-2', - label: 'Option 2 Label', - }, { - value: 'option-3', - label: 'Option 3 Label', - }], - defaultValue: 'option-2', - admin: { - readOnly: true, - }, - }, - { - type: 'row', - fields: [ - { - name: 'email', - label: 'Email', - type: 'email', - defaultValue: 'some@email.com', - }, { - name: 'number', - label: 'Number', - type: 'number', - defaultValue: 5, - }, - ], - }, - { - type: 'group', - label: 'Group', - name: 'group', - fields: [ - { - type: 'row', - fields: [ - { - name: 'nestedText1', - label: 'Nested Text 1', - type: 'text', - defaultValue: 'nested default text 1', - }, { - name: 'nestedText2', - label: 'Nested Text 2', - type: 'text', - defaultValue: 'nested default text 2', - }, - ], - }, - ], - }, - { - type: 'array', - label: 'Array', - name: 'array', - fields: [ - { - type: 'row', - fields: [ - { - name: 'arrayText1', - label: 'Array Text 1', - type: 'text', - admin: { - width: '50%', - }, - defaultValue: 'default array text', - }, - { - name: 'arrayText2', - label: 'Array Text 2', - type: 'text', - admin: { - width: '50%', - }, - access: { - read: ({ req: { user } }) => Boolean(user), - update: ({ req: { user } }) => checkRole(['admin'], user), - }, - }, - ], - }, - { - type: 'text', - name: 'arrayText3', - label: 'Array Text 3', - admin: { - readOnly: true, - }, - }, - { - name: 'checkbox', - label: 'Checkbox', - type: 'checkbox', - default: true, - }, - ], - }, - { - type: 'blocks', - label: 'Blocks Content', - name: 'blocks', - labels: { - singular: 'Block', - plural: 'Blocks', - }, - blocks: [Email, NumberBlock, Quote, CallToAction], - localized: true, - }, - { - type: 'relationship', - label: 'Relationship to One Collection', - name: 'relationship', - relationTo: 'conditions', - }, - { - type: 'relationship', - label: 'Relationship hasMany', - name: 'relationshipHasMany', - relationTo: 'localized-posts', - hasMany: true, - }, - { - type: 'relationship', - label: 'Relationship to Multiple Collections', - name: 'relationshipMultipleCollections', - relationTo: ['localized-posts', 'conditions'], - }, - { - type: 'textarea', - label: 'Textarea', - name: 'textarea', - defaultValue: 'my textarea text', - }, - { - name: 'slug', - type: 'text', - label: 'Slug', - admin: { - position: 'sidebar', - }, - localized: true, - unique: true, - defaultValue: 'my-slug', - }, - { - name: 'checkbox', - type: 'checkbox', - label: 'Checkbox', - admin: { - position: 'sidebar', - }, - defaultValue: true, - }, - { - name: 'richText', - type: 'richText', - label: 'Rich Text', - admin: { - elements: [ - 'h1', - 'h2', - 'h3', - 'h4', - 'h5', - 'h6', - 'blockquote', - 'ul', - 'ol', - 'link', - ], - leaves: [ - 'bold', - 'italic', - 'underline', - 'strikethrough', - ], - }, - // defaultValue: 'sooo riiiiiiiich', - }, - ], - timestamps: true, -}; - -export default DefaultValueTest; diff --git a/demo/collections/DefaultValues.ts b/demo/collections/DefaultValues.ts index bec032e31f..cf0bc0d1d4 100644 --- a/demo/collections/DefaultValues.ts +++ b/demo/collections/DefaultValues.ts @@ -204,6 +204,15 @@ const DefaultValues: PayloadCollectionConfig = { }, blocks: [Email, NumberBlock, Quote, CallToAction], localized: true, + admin: { + readOnly: true, + }, + defaultValue: [ + { + blockType: 'email', + testEmail: 'dev@payloadcms.com', + }, + ], }, { type: 'relationship', diff --git a/src/fields/config/schema.ts b/src/fields/config/schema.ts index 6a5bcdd9ad..d5af8b83bc 100644 --- a/src/fields/config/schema.ts +++ b/src/fields/config/schema.ts @@ -175,6 +175,7 @@ export const blocks = baseField.keys({ fields: joi.array().items(joi.link('#field')), }), ), + defaultValue: joi.array().items(joi.object()), }); export const richText = baseField.keys({