or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-slate-prop-types

React PropTypes validators for Slate editor data structures

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/slate-prop-types@0.5.x

To install, run

npx @tessl/cli install tessl/npm-slate-prop-types@0.5.0

index.mddocs/

Slate Prop Types

Slate Prop Types provides React PropTypes validators for Slate editor data structures. It offers validation for all major Slate objects including blocks, documents, marks, selections, and their corresponding collection types, enabling type checking during development for Slate-based rich text editors.

Package Information

  • Package Name: slate-prop-types
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install slate-prop-types
  • Peer Dependencies: immutable (>=3.8.1), slate (>=0.32.0 <0.50.0)

Core Imports

import Types from "slate-prop-types";

For CommonJS:

const Types = require("slate-prop-types");

Note: This package only exports a default export, not named exports.

For UMD (browser):

<script src="https://unpkg.com/slate-prop-types/dist/slate-prop-types.min.js"></script>
<!-- Available as global SlatePropTypes -->

Basic Usage

import React from "react";
import PropTypes from "prop-types";
import Types from "slate-prop-types";

// Component using slate prop type validators
const SlateEditor = ({ value, document, selection }) => {
  return (
    <div>
      {/* Your Slate editor implementation */}
    </div>
  );
};

SlateEditor.propTypes = {
  value: Types.value.isRequired,
  document: Types.document,
  selection: Types.selection,
};

// Usage with collection types
const BlockList = ({ blocks, nodes }) => {
  return (
    <div>
      {/* Render blocks */}
    </div>
  );
};

BlockList.propTypes = {
  blocks: Types.blocks.isRequired,
  nodes: Types.nodes,
};

Capabilities

Individual Slate Object Validators

Validators for individual Slate data types.

// Block element validator
Types.block: PropType;
Types.block.isRequired: PropType;

// Change operation validator  
Types.change: PropType;
Types.change.isRequired: PropType;

// Data object validator
Types.data: PropType;
Types.data.isRequired: PropType;

// Document validator
Types.document: PropType;
Types.document.isRequired: PropType;

// Inline element validator
Types.inline: PropType;
Types.inline.isRequired: PropType;

// Leaf node validator
Types.leaf: PropType;
Types.leaf.isRequired: PropType;

// Mark/formatting validator
Types.mark: PropType;
Types.mark.isRequired: PropType;

// Node validator
Types.node: PropType;
Types.node.isRequired: PropType;

// Range selection validator
Types.range: PropType;
Types.range.isRequired: PropType;

// Selection validator
Types.selection: PropType;
Types.selection.isRequired: PropType;

// Value/state validator
Types.value: PropType;
Types.value.isRequired: PropType;

// Text node validator
Types.text: PropType;
Types.text.isRequired: PropType;

Collection Type Validators

Validators for Slate collection types (Lists and Sets).

// List of Block elements
Types.blocks: PropType;
Types.blocks.isRequired: PropType;

// List of Inline elements
Types.inlines: PropType;
Types.inlines.isRequired: PropType;

// List of Leaf nodes
Types.leaves: PropType;
Types.leaves.isRequired: PropType;

// Set of Mark objects
Types.marks: PropType;
Types.marks.isRequired: PropType;

// List of Node objects
Types.nodes: PropType;
Types.nodes.isRequired: PropType;

// List of Range objects
Types.ranges: PropType;
Types.ranges.isRequired: PropType;

// List of Text nodes
Types.texts: PropType;
Types.texts.isRequired: PropType;

PropType Validator Interface

Each exported prop type validator follows the standard React PropTypes interface:

Optional Validation

componentName.propTypes = {
  // Allows null, undefined, or valid Slate block
  myBlock: Types.block,
};

Required Validation

componentName.propTypes = {
  // Throws error if null, undefined, or invalid
  myBlock: Types.block.isRequired,
};

Validation Behavior

  • Valid Value: Returns null (no error)
  • Invalid Value: Returns Error object with descriptive message
  • Null/Undefined:
    • Optional: Returns null (no error)
    • Required: Returns Error object

Error Messages

When validation fails, descriptive error messages are provided:

// Example error message for invalid block
"Invalid prop `myBlock` supplied to `MyComponent`, expected a Slate `Block` but received: [object Object]"

// Example error message for missing required prop
"The prop `myBlock` is marked as required in `MyComponent`, but it was not supplied."

Slate Type Dependencies

All validators use Slate's built-in type checking methods for validation:

// Internal validation functions (not directly exposed)
Block.isBlock(value): boolean;
Block.isBlockList(value): boolean;
Change.isChange(value): boolean;
Data.isData(value): boolean;
Document.isDocument(value): boolean;
Inline.isInline(value): boolean;
Inline.isInlineList(value): boolean;
Leaf.isLeaf(value): boolean;
Leaf.isLeafList(value): boolean;
Mark.isMark(value): boolean;
Mark.isMarkSet(value): boolean;
Node.isNode(value): boolean;
Node.isNodeList(value): boolean;
Range.isRange(value): boolean;
Range.isRangeList(value): boolean;
Selection.isSelection(value): boolean;
Value.isValue(value): boolean;
Text.isText(value): boolean;
Text.isTextList(value): boolean;

Types

/**
 * PropType validator function interface
 * @param props - Component props object
 * @param propName - Name of the prop being validated
 * @param componentName - Name of the component
 * @param location - Location description (e.g., "prop", "context")
 * @param propFullName - Full prop name for nested validation (optional)
 * @returns null if valid, Error if invalid
 */
interface PropTypeValidator {
  (props: object, propName: string, componentName: string, location: string, propFullName?: string): Error | null;
  isRequired: PropTypeValidator;
}

/**
 * Main Types export - object containing all prop type validators
 */
interface Types {
  // Individual validators
  block: PropTypeValidator;
  change: PropTypeValidator;
  data: PropTypeValidator;
  document: PropTypeValidator;
  inline: PropTypeValidator;
  leaf: PropTypeValidator;
  mark: PropTypeValidator;
  node: PropTypeValidator;
  range: PropTypeValidator;
  selection: PropTypeValidator;
  value: PropTypeValidator;
  text: PropTypeValidator;
  
  // Collection validators
  blocks: PropTypeValidator;
  inlines: PropTypeValidator;
  leaves: PropTypeValidator;
  marks: PropTypeValidator;
  nodes: PropTypeValidator;
  ranges: PropTypeValidator;
  texts: PropTypeValidator;
}

Advanced Usage Examples

Complex Component Validation

import React from "react";
import PropTypes from "prop-types";
import Types from "slate-prop-types";

const RichTextEditor = ({ 
  value, 
  document, 
  selection, 
  blocks, 
  marks, 
  onChange, 
  onSelectionChange 
}) => {
  return (
    <div className="slate-editor">
      {/* Editor implementation */}
    </div>
  );
};

RichTextEditor.propTypes = {
  // Required Slate objects
  value: Types.value.isRequired,
  onChange: PropTypes.func.isRequired,
  
  // Optional Slate objects
  document: Types.document,
  selection: Types.selection,
  
  // Collection types
  blocks: Types.blocks,
  marks: Types.marks,
  
  // Event handlers
  onSelectionChange: PropTypes.func,
};

RichTextEditor.defaultProps = {
  document: null,
  selection: null,
  blocks: null,
  marks: null,
  onSelectionChange: () => {},
};

Plugin Prop Validation

const SlatePlugin = ({ nodes, ranges, marks }) => {
  // Plugin implementation
  return null;
};

SlatePlugin.propTypes = {
  nodes: Types.nodes.isRequired,
  ranges: Types.ranges,
  marks: Types.marks,
};

export default SlatePlugin;