1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 15:02:51 +02:00

fix schema properties to be exhaustive, add leaves (#3251)

This commit is contained in:
Ian Storm Taylor
2019-12-05 15:16:49 -05:00
committed by GitHub
parent d24296e372
commit be1ce2e099
10 changed files with 165 additions and 0 deletions

View File

@@ -26,6 +26,8 @@ export const checkNode = (
if (Editor.isMatch(editor, entry, m)) {
if ('properties' in v) {
const { children, text, ...existing } = node
for (const k in v.properties) {
const p = v.properties[k]
const value = node[k]
@@ -34,6 +36,12 @@ export const checkNode = (
if (isInvalid) {
return { code: 'node_property_invalid', node, path, property: k }
}
delete existing[k]
}
for (const k in existing) {
return { code: 'node_property_invalid', node, path, property: k }
}
}
@@ -64,6 +72,33 @@ export const checkNode = (
return { code: 'last_child_invalid', node: n, path: p, index: i }
}
}
if ('leaves' in v && v.leaves != null) {
for (const [n, p] of Editor.texts(editor, { at: path })) {
const { text, ...existing } = n
for (const k in v.leaves) {
const l = v.leaves[k]
const value = n[k]
const isInvalid = typeof l === 'function' ? !l(value) : l !== value
if (isInvalid) {
return {
code: 'node_leaf_invalid',
node: n,
path: p,
property: k,
}
}
delete existing[k]
}
for (const k in existing) {
return { code: 'node_leaf_invalid', node: n, path: p, property: k }
}
}
}
}
}
}

View File

@@ -42,6 +42,13 @@ export interface NextSiblingInvalidError {
path: Path
}
export interface NodeLeafInvalidError {
code: 'node_leaf_invalid'
node: Node
path: Path
property: string
}
export interface NodePropertyInvalidError {
code: 'node_property_invalid'
node: Node
@@ -76,6 +83,7 @@ export type NodeError =
| FirstChildInvalidError
| LastChildInvalidError
| NextSiblingInvalidError
| NodeLeafInvalidError
| NodePropertyInvalidError
| NodeTextInvalidError
| ParentInvalidError

View File

@@ -11,6 +11,7 @@ export interface NodeValidation {
children?: ChildValidation[]
first?: NodeMatch
last?: NodeMatch
leaves?: Record<string, any>
next?: NodeMatch
parent?: NodeMatch
previous?: NodeMatch

View File

@@ -121,6 +121,7 @@ export const withSchema = (
case 'child_invalid':
case 'next_sibling_invalid':
case 'node_leaf_invalid':
case 'node_property_invalid':
case 'node_text_invalid':
case 'previous_sibling_invalid': {

View File

@@ -0,0 +1,31 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const schema = [
{
for: 'node',
match: { a: true },
validate: {
leaves: {
bold: v => v === true || v === undefined,
},
},
},
]
export const input = (
<editor>
<element a>
<text unknown>word</text>
</element>
</editor>
)
export const output = (
<editor>
<element a>
<text />
</element>
</editor>
)

View File

@@ -0,0 +1,31 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const schema = [
{
for: 'node',
match: { a: true },
validate: {
leaves: {
bold: v => v === true || v === undefined,
},
},
},
]
export const input = (
<editor>
<element a>
<text bold="invalid">word</text>
</element>
</editor>
)
export const output = (
<editor>
<element a>
<text />
</element>
</editor>
)

View File

@@ -0,0 +1,31 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const schema = [
{
for: 'node',
match: { a: true },
validate: {
leaves: {
bold: v => v === true || v === undefined,
},
},
},
]
export const input = (
<editor>
<element a>
<text bold>word</text>
</element>
</editor>
)
export const output = (
<editor>
<element a>
<text bold>word</text>
</element>
</editor>
)

View File

@@ -0,0 +1,25 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const schema = [
{
for: 'node',
match: { a: true },
validate: {
properties: {
a: true,
},
},
},
]
export const input = (
<editor>
<element a thing="unknown">
word
</element>
</editor>
)
export const output = <editor />

View File

@@ -8,6 +8,7 @@ export const schema = [
match: { a: true },
validate: {
properties: {
a: true,
thing: v => v == null || v === 'valid',
},
},

View File

@@ -8,6 +8,7 @@ export const schema = [
match: { a: true },
validate: {
properties: {
a: true,
thing: v => v == null || v === 'valid',
},
},