Updates V3 with V2 PR's - previousVersion type https://github.com/payloadcms/payload/pull/6805 - tests from https://github.com/payloadcms/payload/pull/6805
53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import type { SerializedEditorState, SerializedLexicalNode } from 'lexical'
|
|
import type { RichTextField, ValidateOptions } from 'payload'
|
|
|
|
import type { NodeValidation } from '../features/typesServer.js'
|
|
|
|
export async function validateNodes({
|
|
nodeValidations,
|
|
nodes,
|
|
validation: validationFromProps,
|
|
}: {
|
|
nodeValidations: Map<string, Array<NodeValidation>>
|
|
nodes: SerializedLexicalNode[]
|
|
validation: {
|
|
options: ValidateOptions<unknown, unknown, RichTextField, SerializedEditorState>
|
|
value: SerializedEditorState
|
|
}
|
|
}): Promise<string | true> {
|
|
for (const node of nodes) {
|
|
// Validate node
|
|
if (
|
|
nodeValidations &&
|
|
typeof nodeValidations?.has === 'function' &&
|
|
nodeValidations?.has(node.type)
|
|
) {
|
|
const validations = nodeValidations.get(node.type)
|
|
for (const validation of validations) {
|
|
const validationResult = await validation({
|
|
node,
|
|
nodeValidations,
|
|
validation: validationFromProps,
|
|
})
|
|
if (validationResult !== true) {
|
|
return `${node.type} node failed to validate: ${validationResult}`
|
|
}
|
|
}
|
|
}
|
|
|
|
// Validate node's children
|
|
if ('children' in node && node?.children) {
|
|
const childrenValidationResult = await validateNodes({
|
|
nodeValidations,
|
|
nodes: node.children as SerializedLexicalNode[],
|
|
validation: validationFromProps,
|
|
})
|
|
if (childrenValidationResult !== true) {
|
|
return childrenValidationResult
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|