From 7d5a33025b5abb973fc559c754719ce3aa81c3b7 Mon Sep 17 00:00:00 2001 From: DamareYoh <34608378+DamareYoh@users.noreply.github.com> Date: Wed, 17 Jan 2018 07:24:28 -0800 Subject: [PATCH] added export constant enum for schema violations (#1532) * added export constant enum for schema violations * updated examples to use the schema violations enum * use SchemaViolations enum in tests and docs * fixed path for schema violations import --- docs/reference/slate/schema.md | 36 ++++++++++--------- examples/forced-layout/index.js | 6 ++-- examples/images/index.js | 4 +-- packages/slate/src/index.js | 4 ++- packages/slate/src/models/schema.js | 17 +++++++++ ...hild-kind-invalid-custom-optional-first.js | 3 +- .../custom/child-kind-invalid-custom.js | 3 +- .../schema/custom/child-required-custom.js | 3 +- .../custom/child-type-invalid-custom.js | 3 +- .../schema/custom/child-unknown-custom.js | 3 +- .../custom/first-child-kind-invalid-custom.js | 3 +- .../custom/first-child-type-invalid-custom.js | 3 +- .../custom/last-child-kind-invalid-custom.js | 3 +- .../custom/last-child-type-invalid-custom.js | 3 +- .../schema/custom/node-data-invalid-custom.js | 3 +- .../custom/node-is-void-invalid-custom.js | 3 +- .../schema/custom/node-mark-invalid-custom.js | 3 +- .../schema/custom/node-text-invalid-custom.js | 3 +- .../custom/parent-kind-invalid-custom.js | 3 +- .../custom/parent-type-invalid-custom.js | 3 +- 20 files changed, 75 insertions(+), 37 deletions(-) diff --git a/docs/reference/slate/schema.md b/docs/reference/slate/schema.md index dd5ae5090..7337cefd5 100644 --- a/docs/reference/slate/schema.md +++ b/docs/reference/slate/schema.md @@ -214,9 +214,13 @@ Returns a JSON representation of the schema. ## Invalid Reasons -When supplying your own `normalize` property for a schema rule, it will be called with `(change, reason, context)`. The `reason` will be one of a set of reasons, and `context` will vary depending on the reason. Here's the full set: +When supplying your own `normalize` property for a schema rule, it will be called with `(change, reason, context)`. The `reason` will be one of a set of reasons, and `context` will vary depending on the reason. Invalid reason constants are available through the`SchemaViolations` object. -### `child_object_invalid` +`import { SchemaViolations } from 'slate'` + +Here's the full set: + +### `SchemaViolations.ChildObjectInvalid` ```js { @@ -227,7 +231,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `child_required` +### `SchemaViolations.ChildRequired` ```js { @@ -237,7 +241,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `child_type_invalid` +### `SchemaViolations.ChildTypeInvalid` ```js { @@ -248,7 +252,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `child_unknown` +### `SchemaViolations.ChildUnknown` ```js { @@ -259,7 +263,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `first_child_object_invalid` +### `SchemaViolations.FirstChildObjectInvalid` ```js { @@ -269,7 +273,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `first_child_type_invalid` +### `SchemaViolations.FirstChildTypeInvalid` ```js { @@ -279,7 +283,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `last_child_object_invalid` +### `SchemaViolations.LastChildObjectInvalid` ```js { @@ -289,7 +293,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `last_child_type_invalid` +### `SchemaViolations.LastChildTypeInvalid` ```js { @@ -299,7 +303,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `node_data_invalid` +### `SchemaViolations.NodeDataInvalid` ```js { @@ -310,7 +314,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `node_is_void_invalid` +### `SchemaViolations.NodeIsVoidInvalid` ```js { @@ -319,7 +323,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `node_object_invalid` +### `SchemaViolations.NodeObjectInvalid` ```js { @@ -328,7 +332,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `node_mark_invalid` +### `SchemaViolations.NodeMarkInvalid` ```js { @@ -338,7 +342,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `node_text_invalid` +### `SchemaViolations.NodeTextInvalid` ```js { @@ -348,7 +352,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `parent_object_invalid` +### `SchemaViolations.ParentObjectInvalid` ```js { @@ -358,7 +362,7 @@ When supplying your own `normalize` property for a schema rule, it will be calle } ``` -### `parent_type_invalid` +### `SchemaViolations.ParentTypeInvalid` ```js { diff --git a/examples/forced-layout/index.js b/examples/forced-layout/index.js index 343ebec0a..308937bc1 100644 --- a/examples/forced-layout/index.js +++ b/examples/forced-layout/index.js @@ -1,6 +1,6 @@ import { Editor } from 'slate-react' -import { Block, Value } from 'slate' +import { Block, Value, SchemaViolations } from 'slate' import React from 'react' import initialValue from './value.json' @@ -19,10 +19,10 @@ const schema = { ], normalize: (change, reason, { node, child, index }) => { switch (reason) { - case 'child_type_invalid': { + case SchemaViolations.ChildTypeInvalid: { return change.setNodeByKey(child.key, index == 0 ? 'title' : 'paragraph') } - case 'child_required': { + case SchemaViolations.ChildRequired: { const block = Block.create(index == 0 ? 'title' : 'paragraph') return change.insertNodeByKey(node.key, index, block) } diff --git a/examples/images/index.js b/examples/images/index.js index 976685551..999b930c1 100644 --- a/examples/images/index.js +++ b/examples/images/index.js @@ -1,6 +1,6 @@ import { Editor, getEventRange, getEventTransfer } from 'slate-react' -import { Block, Value } from 'slate' +import { Block, Value, SchemaViolations } from 'slate' import React from 'react' import initialValue from './value.json' @@ -38,7 +38,7 @@ const schema = { last: { types: ['paragraph'] }, normalize: (change, reason, { node, child }) => { switch (reason) { - case 'last_child_type_invalid': { + case SchemaViolations.LastChildTypeInvalid: { const paragraph = Block.create('paragraph') return change.insertNodeByKey(node.key, node.nodes.size, paragraph) } diff --git a/packages/slate/src/index.js b/packages/slate/src/index.js index c2e25ab68..cb0b57827 100644 --- a/packages/slate/src/index.js +++ b/packages/slate/src/index.js @@ -12,7 +12,7 @@ import Node from './models/node' import Operation from './models/operation' import Operations from './operations' import Range from './models/range' -import Schema from './models/schema' +import Schema, { SchemaViolations } from './models/schema' import Stack from './models/stack' import Text from './models/text' import Value from './models/value' @@ -39,6 +39,7 @@ export { Operations, Range, Schema, + SchemaViolations, Stack, Text, Value, @@ -61,6 +62,7 @@ export default { Operations, Range, Schema, + SchemaViolations, Stack, Text, Value, diff --git a/packages/slate/src/models/schema.js b/packages/slate/src/models/schema.js index 58e351c49..c3507884d 100644 --- a/packages/slate/src/models/schema.js +++ b/packages/slate/src/models/schema.js @@ -31,6 +31,23 @@ const NODE_TEXT_INVALID = 'node_text_invalid' const PARENT_OBJECT_INVALID = 'parent_object_invalid' const PARENT_TYPE_INVALID = 'parent_type_invalid' +export const SchemaViolations = Object.freeze({ + ChildObjectInvalid: CHILD_OBJECT_INVALID, + ChildRequired: CHILD_REQUIRED, + ChildTypeInvalid: CHILD_TYPE_INVALID, + ChildUnknown: CHILD_UNKNOWN, + FirstChildObjectInvalid: FIRST_CHILD_OBJECT_INVALID, + FirstChildTypeInvalid: FIRST_CHILD_TYPE_INVALID, + LastChildObjectInvalid: LAST_CHILD_OBJECT_INVALID, + LastChildTypeInvalid: LAST_CHILD_TYPE_INVALID, + NodeDataInvalid: NODE_DATA_INVALID, + NodeIsVoidInvalid: NODE_IS_VOID_INVALID, + NodeMarkInvalid: NODE_MARK_INVALID, + NodeTextInvalid: NODE_TEXT_INVALID, + ParentObjectInvalid: PARENT_OBJECT_INVALID, + ParentTypeInvalid: PARENT_TYPE_INVALID, +}) + /** * Debug. * diff --git a/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js b/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js index 0cd9809f4..197615f05 100644 --- a/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js +++ b/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -11,7 +12,7 @@ export const schema = { { objects: ['block'], types: ['paragraph'], min: 1 } ], normalize: (change, reason, { node, child }) => { - if (reason == 'child_object_invalid') { + if (reason == SchemaViolations.ChildObjectInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/child-kind-invalid-custom.js b/packages/slate/test/schema/custom/child-kind-invalid-custom.js index 72d50065a..5493158d8 100644 --- a/packages/slate/test/schema/custom/child-kind-invalid-custom.js +++ b/packages/slate/test/schema/custom/child-kind-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -10,7 +11,7 @@ export const schema = { { objects: ['block'] }, ], normalize: (change, reason, { child }) => { - if (reason == 'child_object_invalid') { + if (reason == SchemaViolations.ChildObjectInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/child-required-custom.js b/packages/slate/test/schema/custom/child-required-custom.js index bf43086cc..5094e3d11 100644 --- a/packages/slate/test/schema/custom/child-required-custom.js +++ b/packages/slate/test/schema/custom/child-required-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -10,7 +11,7 @@ export const schema = { { types: ['paragraph'], min: 2 }, ], normalize: (change, reason, { node, index }) => { - if (reason == 'child_required') { + if (reason == SchemaViolations.ChildRequired) { change.insertNodeByKey(node.key, index, { object: 'block', type: 'paragraph' }) } } diff --git a/packages/slate/test/schema/custom/child-type-invalid-custom.js b/packages/slate/test/schema/custom/child-type-invalid-custom.js index c6d26a648..a40aafa97 100644 --- a/packages/slate/test/schema/custom/child-type-invalid-custom.js +++ b/packages/slate/test/schema/custom/child-type-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -10,7 +11,7 @@ export const schema = { { types: ['paragraph'] }, ], normalize: (change, reason, { child }) => { - if (reason == 'child_type_invalid') { + if (reason == SchemaViolations.ChildTypeInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/child-unknown-custom.js b/packages/slate/test/schema/custom/child-unknown-custom.js index 89457c3ef..eb6d1a7b2 100644 --- a/packages/slate/test/schema/custom/child-unknown-custom.js +++ b/packages/slate/test/schema/custom/child-unknown-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -10,7 +11,7 @@ export const schema = { { types: ['paragraph'], max: 1 }, ], normalize: (change, reason, { node, child, index }) => { - if (reason == 'child_unknown') { + if (reason == SchemaViolations.ChildUnknown) { const previous = node.getPreviousSibling(child.key) const offset = previous.nodes.size child.nodes.forEach((n, i) => change.moveNodeByKey(n.key, previous.key, offset + i, { normalize: false })) diff --git a/packages/slate/test/schema/custom/first-child-kind-invalid-custom.js b/packages/slate/test/schema/custom/first-child-kind-invalid-custom.js index 67a54a4bd..07d2303fb 100644 --- a/packages/slate/test/schema/custom/first-child-kind-invalid-custom.js +++ b/packages/slate/test/schema/custom/first-child-kind-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -8,7 +9,7 @@ export const schema = { quote: { first: { objects: ['block'] }, normalize: (change, reason, { child }) => { - if (reason == 'first_child_object_invalid') { + if (reason == SchemaViolations.FirstChildObjectInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/first-child-type-invalid-custom.js b/packages/slate/test/schema/custom/first-child-type-invalid-custom.js index 912af00f8..a6e1ed582 100644 --- a/packages/slate/test/schema/custom/first-child-type-invalid-custom.js +++ b/packages/slate/test/schema/custom/first-child-type-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -8,7 +9,7 @@ export const schema = { quote: { first: { types: ['paragraph'] }, normalize: (change, reason, { child }) => { - if (reason == 'first_child_type_invalid') { + if (reason == SchemaViolations.FirstChildTypeInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/last-child-kind-invalid-custom.js b/packages/slate/test/schema/custom/last-child-kind-invalid-custom.js index 582e52b6e..61ade734b 100644 --- a/packages/slate/test/schema/custom/last-child-kind-invalid-custom.js +++ b/packages/slate/test/schema/custom/last-child-kind-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -8,7 +9,7 @@ export const schema = { quote: { last: { objects: ['block'] }, normalize: (change, reason, { child }) => { - if (reason == 'last_child_object_invalid') { + if (reason == SchemaViolations.LastChildObjectInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/last-child-type-invalid-custom.js b/packages/slate/test/schema/custom/last-child-type-invalid-custom.js index 8f80afe46..c2f9cd6ab 100644 --- a/packages/slate/test/schema/custom/last-child-type-invalid-custom.js +++ b/packages/slate/test/schema/custom/last-child-type-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -8,7 +9,7 @@ export const schema = { quote: { last: { types: ['paragraph'] }, normalize: (change, reason, { child }) => { - if (reason == 'last_child_type_invalid') { + if (reason == SchemaViolations.LastChildTypeInvalid) { change.wrapBlockByKey(child.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/node-data-invalid-custom.js b/packages/slate/test/schema/custom/node-data-invalid-custom.js index 8c3df4d80..f006af801 100644 --- a/packages/slate/test/schema/custom/node-data-invalid-custom.js +++ b/packages/slate/test/schema/custom/node-data-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -9,7 +10,7 @@ export const schema = { thing: v => v == 'value' }, normalize: (change, reason, { node, key }) => { - if (reason == 'node_data_invalid') { + if (reason == SchemaViolations.NodeDataInvalid) { change.setNodeByKey(node.key, { data: { thing: 'value' }}) } } diff --git a/packages/slate/test/schema/custom/node-is-void-invalid-custom.js b/packages/slate/test/schema/custom/node-is-void-invalid-custom.js index a425bd7cd..fb945cef4 100644 --- a/packages/slate/test/schema/custom/node-is-void-invalid-custom.js +++ b/packages/slate/test/schema/custom/node-is-void-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -7,7 +8,7 @@ export const schema = { paragraph: { isVoid: false, normalize: (change, reason, { node }) => { - if (reason == 'node_is_void_invalid') { + if (reason == SchemaViolations.NodeIsVoidInvalid) { change.removeNodeByKey(node.key, 'paragraph') } } diff --git a/packages/slate/test/schema/custom/node-mark-invalid-custom.js b/packages/slate/test/schema/custom/node-mark-invalid-custom.js index 0b4eb8192..bd8e4429c 100644 --- a/packages/slate/test/schema/custom/node-mark-invalid-custom.js +++ b/packages/slate/test/schema/custom/node-mark-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -7,7 +8,7 @@ export const schema = { paragraph: { marks: [{ type: 'bold' }], normalize: (change, reason, { node }) => { - if (reason == 'node_mark_invalid') { + if (reason == SchemaViolations.NodeMarkInvalid) { node.nodes.forEach(n => change.removeNodeByKey(n.key)) } } diff --git a/packages/slate/test/schema/custom/node-text-invalid-custom.js b/packages/slate/test/schema/custom/node-text-invalid-custom.js index 02c5874a4..f40c932c9 100644 --- a/packages/slate/test/schema/custom/node-text-invalid-custom.js +++ b/packages/slate/test/schema/custom/node-text-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -7,7 +8,7 @@ export const schema = { paragraph: { text: /^\d*$/, normalize: (change, reason, { node }) => { - if (reason == 'node_text_invalid') { + if (reason == SchemaViolations.NodeTextInvalid) { node.nodes.forEach(n => change.removeNodeByKey(n.key)) } } diff --git a/packages/slate/test/schema/custom/parent-kind-invalid-custom.js b/packages/slate/test/schema/custom/parent-kind-invalid-custom.js index 09f11bac2..a0ed5ccca 100644 --- a/packages/slate/test/schema/custom/parent-kind-invalid-custom.js +++ b/packages/slate/test/schema/custom/parent-kind-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -7,7 +8,7 @@ export const schema = { link: { parent: { objects: ['block'] }, normalize: (change, reason, { node }) => { - if (reason == 'parent_object_invalid') { + if (reason == SchemaViolations.ParentObjectInvalid) { change.unwrapNodeByKey(node.key) } } diff --git a/packages/slate/test/schema/custom/parent-type-invalid-custom.js b/packages/slate/test/schema/custom/parent-type-invalid-custom.js index 5ae4953b6..cd25fa81f 100644 --- a/packages/slate/test/schema/custom/parent-type-invalid-custom.js +++ b/packages/slate/test/schema/custom/parent-type-invalid-custom.js @@ -1,5 +1,6 @@ /** @jsx h */ +import { SchemaViolations } from '../../..' import h from '../../helpers/h' export const schema = { @@ -8,7 +9,7 @@ export const schema = { item: { parent: { types: ['list'] }, normalize: (change, reason, { node }) => { - if (reason == 'parent_type_invalid') { + if (reason == SchemaViolations.ParentTypeInvalid) { change.wrapBlockByKey(node.key, 'list') } }