Skip to content

Commit

Permalink
feat: add RichText fields to allowedResources validation
Browse files Browse the repository at this point in the history
  • Loading branch information
BasKiers committed Jun 21, 2023
1 parent 4d29651 commit e038dca
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/lib/offline-api/validator/resource-links.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ApiHook } from '../'
import { ContentTypePayloadValidator } from './content-type'
import { InvalidActionError, PayloadValidationError } from '../../interfaces/errors'
import { isResourceLink, MAX_RESOURCE_LINKS } from '../../utils/resource-links'
import {
getEnabledResourceLinkNodes,
isResourceLink,
MAX_RESOURCE_LINKS
} from '../../utils/resource-links'
import errorMessages from './errors'

export default class ResourceLinksValidator implements ContentTypePayloadValidator {
Expand All @@ -11,7 +15,9 @@ export default class ResourceLinksValidator implements ContentTypePayloadValidat
const errors: (InvalidActionError | PayloadValidationError)[] = []

const fields = contentType.fields.toRaw()
const resourceLinkCount = fields.filter(isResourceLink).length
const resourceLinkCount = fields.filter(
(field) => isResourceLink(field) || getEnabledResourceLinkNodes(field)?.length
).length

if (resourceLinkCount > MAX_RESOURCE_LINKS) {
errors.push({
Expand All @@ -33,6 +39,39 @@ export default class ResourceLinksValidator implements ContentTypePayloadValidat
})
}

const enabledResourceLinkNodes = getEnabledResourceLinkNodes(field)
if (Array.isArray(enabledResourceLinkNodes) && enabledResourceLinkNodes.length > 0) {
const nodesValidation = field.validations?.find(
({ nodes }) => nodes && typeof nodes === 'object'
)

if (!nodesValidation) {
errors.push({
type: 'InvalidPayload',
message: errorMessages.field.REQUIRED_DEPENDENT_PROPERTY(
'validations[].nodes',
field.id,
'type',
'RichText'
)
})
} else {
for (const nodeType of enabledResourceLinkNodes) {
if (!nodesValidation.nodes[nodeType]?.allowedResources) {
errors.push({
type: 'InvalidPayload',
message: errorMessages.field.REQUIRED_DEPENDENT_PROPERTY(
`validations[].nodes.${nodeType}.allowedResources`,
field.id,
'validations[].enabledNodeTypes[]',
nodeType
)
})
}
}
}
}

if (!isResourceLink(field) && field.allowedResources) {
errors.push({
type: 'InvalidPayload',
Expand Down
14 changes: 14 additions & 0 deletions src/lib/utils/resource-links.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
export const MAX_ALLOWED_RESOURCES = 3
export const MAX_RESOURCE_LINKS = 3

export const RICH_TEXT_RESOURCE_LINK_NODES = ['embedded-resource-block']

export const isResourceLink = (field) =>
field.type === 'ResourceLink' || (field.type === 'Array' && field.items?.type === 'ResourceLink')

export const getEnabledResourceLinkNodes = (field): string[] | undefined => {
if (field.type !== 'RichText' || !Array.isArray(field.validations)) {
return undefined
}

for (const { enabledNodeTypes } of field.validations) {
if (Array.isArray(enabledNodeTypes)) {
return RICH_TEXT_RESOURCE_LINK_NODES.filter((nodeType) => enabledNodeTypes.includes(nodeType))
}
}
}

0 comments on commit e038dca

Please sign in to comment.