Skip to content

Commit

Permalink
feat: add embedded-resource-block schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BasKiers committed Jun 21, 2023
1 parent 46cfd51 commit 4d29651
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/lib/offline-api/validator/schema/allowed-resources-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Joi from 'joi'
import { MAX_ALLOWED_RESOURCES } from '../../../utils/resource-links'

export const allowedResourcesSchema = Joi.array()
.min(1)
.max(MAX_ALLOWED_RESOURCES)
.unique('source')
.items(
Joi.object({
type: Joi.string().valid('Contentful:Entry'),
source: Joi.string(),
contentTypes: Joi.array().min(1).items(Joi.string())
})
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Joi from 'joi'
import { allowedResourcesSchema } from './allowed-resources-schema'

const validation = (name, constraint) =>
Joi.object({
Expand Down Expand Up @@ -61,6 +62,10 @@ const nodes = validation(
'embedded-entry-block': Joi.array(),
'embedded-entry-inline': Joi.array(),
'embedded-asset-block': Joi.array(),
'embedded-resource-block': Joi.object({
validations: Joi.array().items(size),
allowedResources: allowedResourcesSchema
}),
'entry-hyperlink': Joi.array(),
'asset-hyperlink': Joi.array(),
hyperlink: Joi.array()
Expand All @@ -69,7 +74,9 @@ const nodes = validation(

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

const enabledNodeTypes = validation(
Expand All @@ -89,6 +96,7 @@ const enabledNodeTypes = validation(
'table',
'embedded-entry-block',
'embedded-asset-block',
'embedded-resource-block',
'hyperlink',
'entry-hyperlink',
'asset-hyperlink',
Expand Down
14 changes: 2 additions & 12 deletions src/lib/offline-api/validator/schema/fields-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Joi from 'joi'
import fieldValidations from './field-validations-schema'
import { MAX_ALLOWED_RESOURCES } from '../../../utils/resource-links'
import { allowedResourcesSchema } from './allowed-resources-schema'

export function createFieldsSchema(locales: string[]) {
const fieldSchema = Joi.object().keys({
Expand Down Expand Up @@ -42,17 +42,7 @@ export function createFieldsSchema(locales: string[]) {
localized: Joi.boolean(),
required: Joi.boolean(),
validations: Joi.array().unique().items(fieldValidations),
allowedResources: Joi.array()
.min(1)
.max(MAX_ALLOWED_RESOURCES)
.unique('source')
.items(
Joi.object({
type: Joi.string().valid('Contentful:Entry'),
source: Joi.string(),
contentTypes: Joi.array().min(1).items(Joi.string())
})
),
allowedResources: allowedResourcesSchema,
defaultValue: Joi.object()
.strict()
.pattern(
Expand Down

0 comments on commit 4d29651

Please sign in to comment.