This PR addresses around 500 TypeScript errors by enabling strictNullChecks in the richtext-lexical package. In the process, several bugs were identified and fixed. In some cases, I applied non-null assertions where necessary, although there may be room for further type refinement in the future. The focus of this PR is to resolve the immediate issues without introducing additional technical debt, rather than aiming for perfect type definitions at this stage. --------- Co-authored-by: Alessio Gravili <alessio@gravili.de>
49 lines
1.4 KiB
TypeScript
49 lines
1.4 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({
|
|
nodes,
|
|
nodeValidations,
|
|
validation: validationFromProps,
|
|
}: {
|
|
nodes: SerializedLexicalNode[]
|
|
nodeValidations: Map<string, Array<NodeValidation>>
|
|
validation: {
|
|
options: ValidateOptions<unknown, unknown, RichTextField, SerializedEditorState>
|
|
value: SerializedEditorState
|
|
}
|
|
}): Promise<string | true> {
|
|
for (const node of nodes) {
|
|
// Validate node
|
|
const validations = nodeValidations.get(node.type)
|
|
if (validations) {
|
|
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({
|
|
nodes: node.children as SerializedLexicalNode[],
|
|
nodeValidations,
|
|
validation: validationFromProps,
|
|
})
|
|
if (childrenValidationResult !== true) {
|
|
return childrenValidationResult
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|