diff --git a/packages/slate-schema/src/checkers.ts b/packages/slate-schema/src/checkers.ts index 948f09f48..238de58c7 100644 --- a/packages/slate-schema/src/checkers.ts +++ b/packages/slate-schema/src/checkers.ts @@ -118,7 +118,7 @@ export const checkAncestor = ( entry: AncestorEntry, rule: NodeRule, ancestorRules: NodeRule[] -): NodeError | undefined => { +): [NodeRule, NodeError] | undefined => { const { validate: v } = rule const [parent, parentPath] = entry const processed = new Set() @@ -144,7 +144,7 @@ export const checkAncestor = ( const e = checkParent(editor, entry, index, rule, r) if (e) { - return e + return [r, e] } } } @@ -180,13 +180,16 @@ export const checkAncestor = ( count = 0 continue } else { - return { - code: 'child_max_invalid', - node: child, - path: childPath, - count, - max: group.max, - } + return [ + rule, + { + code: 'child_max_invalid', + node: child, + path: childPath, + count, + max: group.max, + }, + ] } } @@ -194,13 +197,16 @@ export const checkAncestor = ( // we're missing a child in a group with a mininmum set. if (!child) { if (group.min != null && count <= group.min) { - return { - code: 'child_min_invalid', - node: child, - path: childPath, - count, - min: group.min, - } + return [ + rule, + { + code: 'child_min_invalid', + node: child, + path: childPath, + count, + min: group.min, + }, + ] } else { g++ count = 0 @@ -230,17 +236,20 @@ export const checkAncestor = ( const nc = groups[g + 1] if (nc && Editor.isMatch(editor, [child, childPath], nc.match || {})) { - return { - code: 'child_min_invalid', - node: child, - path: childPath, - count, - min: group.min, - } + return [ + rule, + { + code: 'child_min_invalid', + node: child, + path: childPath, + count, + min: group.min, + }, + ] } } - return { code: 'child_invalid', node: child, path: childPath } + return [rule, { code: 'child_invalid', node: child, path: childPath }] } } diff --git a/packages/slate-schema/src/with-schema.ts b/packages/slate-schema/src/with-schema.ts index 882b66424..63cb4acf3 100644 --- a/packages/slate-schema/src/with-schema.ts +++ b/packages/slate-schema/src/with-schema.ts @@ -40,17 +40,22 @@ export const withSchema = ( let rule: NodeRule | undefined for (const r of nodeRules) { - let e = checkNode(editor, entry, r, nodeRules) + error = checkNode(editor, [n, p], r, nodeRules) - if (!e && !Text.isText(n)) { - e = checkAncestor(editor, [n, p], r, ancestorRules) - } - - if (e) { - error = e + if (error) { rule = r break } + + if (!Text.isText(n)) { + const failure = checkAncestor(editor, [n, p], r, ancestorRules) + + if (failure) { + rule = failure[0] + error = failure[1] + break + } + } } if (error == null) {