mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 02:49:56 +02:00
refactor schema violations to be a separate package
This commit is contained in:
13
packages/slate-schema-violations/Changelog.md
Normal file
13
packages/slate-schema-violations/Changelog.md
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
# Changelog
|
||||
|
||||
This document maintains a list of changes to the `slate-schema-violations` package with each new version. Until `1.0.0` is released, breaking changes will be added as minor version bumps, and smaller changes won't be accounted for since the library is moving quickly.
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
### `0.1.0` — January 26, 2018
|
||||
|
||||
:tada:
|
||||
|
2
packages/slate-schema-violations/Readme.md
Normal file
2
packages/slate-schema-violations/Readme.md
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
This package contains a set of constants for the built-in violations in a Slate schema.
|
35
packages/slate-schema-violations/package.json
Normal file
35
packages/slate-schema-violations/package.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "slate-schema-violations",
|
||||
"description": "A set of constants for the built-in violations in a Slate schema.",
|
||||
"version": "0.0.0",
|
||||
"license": "MIT",
|
||||
"repository": "git://github.com/ianstormtaylor/slate.git",
|
||||
"main": "./lib/index.js",
|
||||
"files": [
|
||||
"dist/",
|
||||
"lib/"
|
||||
],
|
||||
"dependencies": {},
|
||||
"peerDependencies": {},
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.10.1",
|
||||
"browserify": "^13.0.1",
|
||||
"mocha": "^2.5.3",
|
||||
"slate": "^0.32.1",
|
||||
"uglify-js": "^2.7.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel --out-dir ./lib ./src",
|
||||
"build:max": "mkdir -p ./dist && NODE_ENV=production browserify ./src/index.js --transform babelify --transform envify --transform [ browserify-global-shim --global ] --standalone SlateSchemaViolations > ./dist/slate-schema-violations.js",
|
||||
"build:min": "mkdir -p ./dist && NODE_ENV=production browserify ./src/index.js --transform babelify --transform envify --transform [ browserify-global-shim --global ] --transform uglifyify --standalone SlateSchemaViolations | uglifyjs > ./dist/slate-schema-violations.min.js",
|
||||
"clean": "rm -rf ./dist ./lib ./node_modules",
|
||||
"prepublish": "yarn run build && yarn run build:max && yarn run build:min",
|
||||
"watch": "babel --watch --out-dir ./lib ./src --source-maps inline"
|
||||
},
|
||||
"keywords": [
|
||||
"constants",
|
||||
"schema",
|
||||
"slate",
|
||||
"violation"
|
||||
]
|
||||
}
|
21
packages/slate-schema-violations/src/index.js
Normal file
21
packages/slate-schema-violations/src/index.js
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
/**
|
||||
* Schema violations.
|
||||
*
|
||||
* @type {String}
|
||||
*/
|
||||
|
||||
export const CHILD_OBJECT_INVALID = 'child_object_invalid'
|
||||
export const CHILD_REQUIRED = 'child_required'
|
||||
export const CHILD_TYPE_INVALID = 'child_type_invalid'
|
||||
export const CHILD_UNKNOWN = 'child_unknown'
|
||||
export const FIRST_CHILD_OBJECT_INVALID = 'first_child_object_invalid'
|
||||
export const FIRST_CHILD_TYPE_INVALID = 'first_child_type_invalid'
|
||||
export const LAST_CHILD_OBJECT_INVALID = 'last_child_object_invalid'
|
||||
export const LAST_CHILD_TYPE_INVALID = 'last_child_type_invalid'
|
||||
export const NODE_DATA_INVALID = 'node_data_invalid'
|
||||
export const NODE_IS_VOID_INVALID = 'node_is_void_invalid'
|
||||
export const NODE_MARK_INVALID = 'node_mark_invalid'
|
||||
export const NODE_TEXT_INVALID = 'node_text_invalid'
|
||||
export const PARENT_OBJECT_INVALID = 'parent_object_invalid'
|
||||
export const PARENT_TYPE_INVALID = 'parent_type_invalid'
|
0
packages/slate-schema-violations/test/index.js
Normal file
0
packages/slate-schema-violations/test/index.js
Normal file
@@ -17,6 +17,7 @@
|
||||
"is-plain-object": "^2.0.4",
|
||||
"lodash": "^4.17.4",
|
||||
"slate-dev-logger": "^0.1.36",
|
||||
"slate-schema-violations": "^0.0.0",
|
||||
"type-of": "^2.0.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
@@ -5,49 +5,28 @@ import logger from 'slate-dev-logger'
|
||||
import mergeWith from 'lodash/mergeWith'
|
||||
import { Record } from 'immutable'
|
||||
|
||||
import {
|
||||
CHILD_OBJECT_INVALID,
|
||||
CHILD_REQUIRED,
|
||||
CHILD_TYPE_INVALID,
|
||||
CHILD_UNKNOWN,
|
||||
FIRST_CHILD_OBJECT_INVALID,
|
||||
FIRST_CHILD_TYPE_INVALID,
|
||||
LAST_CHILD_OBJECT_INVALID,
|
||||
LAST_CHILD_TYPE_INVALID,
|
||||
NODE_DATA_INVALID,
|
||||
NODE_IS_VOID_INVALID,
|
||||
NODE_MARK_INVALID,
|
||||
NODE_TEXT_INVALID,
|
||||
PARENT_OBJECT_INVALID,
|
||||
PARENT_TYPE_INVALID,
|
||||
} from 'slate-schema-violations'
|
||||
|
||||
import CORE_SCHEMA_RULES from '../constants/core-schema-rules'
|
||||
import MODEL_TYPES from '../constants/model-types'
|
||||
import Stack from './stack'
|
||||
import memoize from '../utils/memoize'
|
||||
|
||||
/**
|
||||
* Validation failure reasons.
|
||||
*
|
||||
* @type {Object}
|
||||
*/
|
||||
|
||||
const CHILD_OBJECT_INVALID = 'child_object_invalid'
|
||||
const CHILD_REQUIRED = 'child_required'
|
||||
const CHILD_TYPE_INVALID = 'child_type_invalid'
|
||||
const CHILD_UNKNOWN = 'child_unknown'
|
||||
const FIRST_CHILD_OBJECT_INVALID = 'first_child_object_invalid'
|
||||
const FIRST_CHILD_TYPE_INVALID = 'first_child_type_invalid'
|
||||
const LAST_CHILD_OBJECT_INVALID = 'last_child_object_invalid'
|
||||
const LAST_CHILD_TYPE_INVALID = 'last_child_type_invalid'
|
||||
const NODE_DATA_INVALID = 'node_data_invalid'
|
||||
const NODE_IS_VOID_INVALID = 'node_is_void_invalid'
|
||||
const NODE_MARK_INVALID = 'node_mark_invalid'
|
||||
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.
|
||||
*
|
||||
@@ -203,32 +182,32 @@ class Schema extends Record(DEFAULTS) {
|
||||
/**
|
||||
* Fail validation by returning a normalizing change function.
|
||||
*
|
||||
* @param {String} reason
|
||||
* @param {String} violation
|
||||
* @param {Object} context
|
||||
* @return {Function}
|
||||
*/
|
||||
|
||||
fail(reason, context) {
|
||||
fail(violation, context) {
|
||||
return (change) => {
|
||||
debug(`normalizing`, { reason, context })
|
||||
debug(`normalizing`, { violation, context })
|
||||
const { rule } = context
|
||||
const { size } = change.operations
|
||||
if (rule.normalize) rule.normalize(change, reason, context)
|
||||
if (rule.normalize) rule.normalize(change, violation, context)
|
||||
if (change.operations.size > size) return
|
||||
this.normalize(change, reason, context)
|
||||
this.normalize(change, violation, context)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize an invalid value with `reason` and `context`.
|
||||
* Normalize an invalid value with `violation` and `context`.
|
||||
*
|
||||
* @param {Change} change
|
||||
* @param {String} reason
|
||||
* @param {String} violation
|
||||
* @param {Mixed} context
|
||||
*/
|
||||
|
||||
normalize(change, reason, context) {
|
||||
switch (reason) {
|
||||
normalize(change, violation, context) {
|
||||
switch (violation) {
|
||||
case CHILD_OBJECT_INVALID:
|
||||
case CHILD_TYPE_INVALID:
|
||||
case CHILD_UNKNOWN:
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -12,7 +12,7 @@ export const schema = {
|
||||
{ objects: ['block'], types: ['paragraph'], min: 1 }
|
||||
],
|
||||
normalize: (change, reason, { node, child }) => {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
if (reason == CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ objects: ['block'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.ChildObjectInvalid) {
|
||||
if (reason == CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_REQUIRED } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], min: 2 },
|
||||
],
|
||||
normalize: (change, reason, { node, index }) => {
|
||||
if (reason == SchemaViolations.ChildRequired) {
|
||||
if (reason == CHILD_REQUIRED) {
|
||||
change.insertNodeByKey(node.key, index, { object: 'block', type: 'paragraph' })
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'] },
|
||||
],
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.ChildTypeInvalid) {
|
||||
if (reason == CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { CHILD_UNKNOWN } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -11,7 +11,7 @@ export const schema = {
|
||||
{ types: ['paragraph'], max: 1 },
|
||||
],
|
||||
normalize: (change, reason, { node, child, index }) => {
|
||||
if (reason == SchemaViolations.ChildUnknown) {
|
||||
if (reason == CHILD_UNKNOWN) {
|
||||
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 }))
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { FIRST_CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.FirstChildObjectInvalid) {
|
||||
if (reason == FIRST_CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { FIRST_CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
first: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.FirstChildTypeInvalid) {
|
||||
if (reason == FIRST_CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { LAST_CHILD_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { objects: ['block'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.LastChildObjectInvalid) {
|
||||
if (reason == LAST_CHILD_OBJECT_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { LAST_CHILD_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
quote: {
|
||||
last: { types: ['paragraph'] },
|
||||
normalize: (change, reason, { child }) => {
|
||||
if (reason == SchemaViolations.LastChildTypeInvalid) {
|
||||
if (reason == LAST_CHILD_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(child.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_DATA_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -10,7 +10,7 @@ export const schema = {
|
||||
thing: v => v == 'value'
|
||||
},
|
||||
normalize: (change, reason, { node, key }) => {
|
||||
if (reason == SchemaViolations.NodeDataInvalid) {
|
||||
if (reason == NODE_DATA_INVALID) {
|
||||
change.setNodeByKey(node.key, { data: { thing: 'value' }})
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_IS_VOID_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
isVoid: false,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeIsVoidInvalid) {
|
||||
if (reason == NODE_IS_VOID_INVALID) {
|
||||
change.removeNodeByKey(node.key, 'paragraph')
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_MARK_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
marks: [{ type: 'bold' }],
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeMarkInvalid) {
|
||||
if (reason == NODE_MARK_INVALID) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { NODE_TEXT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
paragraph: {
|
||||
text: /^\d*$/,
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.NodeTextInvalid) {
|
||||
if (reason == NODE_TEXT_INVALID) {
|
||||
node.nodes.forEach(n => change.removeNodeByKey(n.key))
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { PARENT_OBJECT_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -8,7 +8,7 @@ export const schema = {
|
||||
link: {
|
||||
parent: { objects: ['block'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.ParentObjectInvalid) {
|
||||
if (reason == PARENT_OBJECT_INVALID) {
|
||||
change.unwrapNodeByKey(node.key)
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { SchemaViolations } from '../../..'
|
||||
import { PARENT_TYPE_INVALID } from 'slate-schema-violations'
|
||||
import h from '../../helpers/h'
|
||||
|
||||
export const schema = {
|
||||
@@ -9,7 +9,7 @@ export const schema = {
|
||||
item: {
|
||||
parent: { types: ['list'] },
|
||||
normalize: (change, reason, { node }) => {
|
||||
if (reason == SchemaViolations.ParentTypeInvalid) {
|
||||
if (reason == PARENT_TYPE_INVALID) {
|
||||
change.wrapBlockByKey(node.key, 'list')
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user