mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-24 07:52:50 +02:00
add schema text validators to be functions
This commit is contained in:
@@ -233,15 +233,20 @@ Will validate a node's parent against a [`match`](#match).
|
|||||||
|
|
||||||
### `text`
|
### `text`
|
||||||
|
|
||||||
`Array`
|
`RegExp|Function`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
text: /^\w+$/
|
text: /^\w+$/
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```js
|
||||||
|
{
|
||||||
|
text: string => string === 'valid'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Will validate a node's text with a regex.
|
Will validate a node's text with a regex or function.
|
||||||
|
|
||||||
## Static Methods
|
## Static Methods
|
||||||
|
|
||||||
|
@@ -561,7 +561,8 @@ function validateMarks(node, rule) {
|
|||||||
function validateText(node, rule) {
|
function validateText(node, rule) {
|
||||||
if (rule.text == null) return
|
if (rule.text == null) return
|
||||||
const { text } = node
|
const { text } = node
|
||||||
const valid = rule.text.test(text)
|
const valid =
|
||||||
|
typeof rule.text === 'function' ? rule.text(text) : rule.text.test(text)
|
||||||
if (valid) return
|
if (valid) return
|
||||||
return fail(NODE_TEXT_INVALID, { rule, node, text })
|
return fail(NODE_TEXT_INVALID, { rule, node, text })
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,25 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
blocks: {
|
||||||
|
paragraph: {
|
||||||
|
text: t => t === 'valid',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<paragraph>invalid</paragraph>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document />
|
||||||
|
</value>
|
||||||
|
)
|
@@ -0,0 +1,27 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
blocks: {
|
||||||
|
paragraph: {
|
||||||
|
text: t => t === 'valid',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<paragraph>valid</paragraph>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<paragraph>valid</paragraph>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
@@ -1,17 +1,11 @@
|
|||||||
/** @jsx h */
|
/** @jsx h */
|
||||||
|
|
||||||
import { NODE_TEXT_INVALID } from 'slate-schema-violations'
|
|
||||||
import h from '../../helpers/h'
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
export const schema = {
|
export const schema = {
|
||||||
blocks: {
|
blocks: {
|
||||||
paragraph: {
|
paragraph: {
|
||||||
text: /^\d*$/,
|
text: /^\d*$/,
|
||||||
normalize: (change, { code, node }) => {
|
|
||||||
if (code == NODE_TEXT_INVALID) {
|
|
||||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user