mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-23 15:32:59 +02:00
fix schema properties to be exhaustive, add leaves (#3251)
This commit is contained in:
@@ -26,6 +26,8 @@ export const checkNode = (
|
|||||||
|
|
||||||
if (Editor.isMatch(editor, entry, m)) {
|
if (Editor.isMatch(editor, entry, m)) {
|
||||||
if ('properties' in v) {
|
if ('properties' in v) {
|
||||||
|
const { children, text, ...existing } = node
|
||||||
|
|
||||||
for (const k in v.properties) {
|
for (const k in v.properties) {
|
||||||
const p = v.properties[k]
|
const p = v.properties[k]
|
||||||
const value = node[k]
|
const value = node[k]
|
||||||
@@ -34,6 +36,12 @@ export const checkNode = (
|
|||||||
if (isInvalid) {
|
if (isInvalid) {
|
||||||
return { code: 'node_property_invalid', node, path, property: k }
|
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 }
|
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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -42,6 +42,13 @@ export interface NextSiblingInvalidError {
|
|||||||
path: Path
|
path: Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface NodeLeafInvalidError {
|
||||||
|
code: 'node_leaf_invalid'
|
||||||
|
node: Node
|
||||||
|
path: Path
|
||||||
|
property: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface NodePropertyInvalidError {
|
export interface NodePropertyInvalidError {
|
||||||
code: 'node_property_invalid'
|
code: 'node_property_invalid'
|
||||||
node: Node
|
node: Node
|
||||||
@@ -76,6 +83,7 @@ export type NodeError =
|
|||||||
| FirstChildInvalidError
|
| FirstChildInvalidError
|
||||||
| LastChildInvalidError
|
| LastChildInvalidError
|
||||||
| NextSiblingInvalidError
|
| NextSiblingInvalidError
|
||||||
|
| NodeLeafInvalidError
|
||||||
| NodePropertyInvalidError
|
| NodePropertyInvalidError
|
||||||
| NodeTextInvalidError
|
| NodeTextInvalidError
|
||||||
| ParentInvalidError
|
| ParentInvalidError
|
||||||
|
@@ -11,6 +11,7 @@ export interface NodeValidation {
|
|||||||
children?: ChildValidation[]
|
children?: ChildValidation[]
|
||||||
first?: NodeMatch
|
first?: NodeMatch
|
||||||
last?: NodeMatch
|
last?: NodeMatch
|
||||||
|
leaves?: Record<string, any>
|
||||||
next?: NodeMatch
|
next?: NodeMatch
|
||||||
parent?: NodeMatch
|
parent?: NodeMatch
|
||||||
previous?: NodeMatch
|
previous?: NodeMatch
|
||||||
|
@@ -121,6 +121,7 @@ export const withSchema = (
|
|||||||
|
|
||||||
case 'child_invalid':
|
case 'child_invalid':
|
||||||
case 'next_sibling_invalid':
|
case 'next_sibling_invalid':
|
||||||
|
case 'node_leaf_invalid':
|
||||||
case 'node_property_invalid':
|
case 'node_property_invalid':
|
||||||
case 'node_text_invalid':
|
case 'node_text_invalid':
|
||||||
case 'previous_sibling_invalid': {
|
case 'previous_sibling_invalid': {
|
||||||
|
@@ -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>
|
||||||
|
)
|
@@ -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>
|
||||||
|
)
|
31
packages/slate-schema/test/validations/leaves/valid.js
Normal file
31
packages/slate-schema/test/validations/leaves/valid.js
Normal 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>
|
||||||
|
)
|
@@ -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 />
|
@@ -8,6 +8,7 @@ export const schema = [
|
|||||||
match: { a: true },
|
match: { a: true },
|
||||||
validate: {
|
validate: {
|
||||||
properties: {
|
properties: {
|
||||||
|
a: true,
|
||||||
thing: v => v == null || v === 'valid',
|
thing: v => v == null || v === 'valid',
|
||||||
},
|
},
|
||||||
},
|
},
|
@@ -8,6 +8,7 @@ export const schema = [
|
|||||||
match: { a: true },
|
match: { a: true },
|
||||||
validate: {
|
validate: {
|
||||||
properties: {
|
properties: {
|
||||||
|
a: true,
|
||||||
thing: v => v == null || v === 'valid',
|
thing: v => v == null || v === 'valid',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user