createBindingValidator

See source code

Creates a runtime validator for a specific binding type. This factory function generates a complete validation schema for custom bindings that extends TLBaseBinding.

The validator ensures all binding records conform to the expected structure with proper type safety and runtime validation. It validates the base binding properties (id, type, fromId, toId) along with custom props and meta fields.

function createBindingValidator<
  Type extends string,
  Props extends JsonObject,
  Meta extends JsonObject,
>(
  type: Type,
  props?: {
    [K in keyof Props]: T.Validatable<Props[K]>
  },
  meta?: {
    [K in keyof Meta]: T.Validatable<Meta[K]>
  }
): T.ObjectValidator<
  import('@tldraw/utils').Expand<
    {
      [P in
        | 'fromId'
        | 'id'
        | 'meta'
        | 'toId'
        | 'typeName'
        | (undefined extends Props ? never : 'props')
        | (undefined extends Type ? never : 'type')]: TLBaseBinding<
        Type,
        Props
      >[P]
    } & {
      [P_1 in
        | (undefined extends Props ? 'props' : never)
        | (undefined extends Type ? 'type' : never)]?:
        | TLBaseBinding<Type, Props>[P_1]
        | undefined
    }
  >
>

Example

import { createBindingValidator } from '@tldraw/tlschema'
import { T } from '@tldraw/validate'

// Create validator for a custom binding type
const myBindingValidator = createBindingValidator(
  'myBinding',
  {
    strength: T.number,
    color: T.string,
    enabled: T.boolean,
  },
  {
    createdAt: T.number,
    author: T.string,
  }
)

// Validate a binding instance
const bindingData = {
  id: 'binding:123',
  typeName: 'binding',
  type: 'myBinding',
  fromId: 'shape:abc',
  toId: 'shape:def',
  props: {
    strength: 0.8,
    color: 'red',
    enabled: true,
  },
  meta: {
    createdAt: Date.now(),
    author: 'user123',
  },
}

const isValid = myBindingValidator.isValid(bindingData) // true
// Simple binding without custom props or meta
const simpleBindingValidator = createBindingValidator('simple')

// This will use JsonValue validation for props and meta
const binding = {
  id: 'binding:456',
  typeName: 'binding',
  type: 'simple',
  fromId: 'shape:start',
  toId: 'shape:end',
  props: {}, // Any JSON value allowed
  meta: {}, // Any JSON value allowed
}

Parameters

NameDescription

type

Type

The string literal type identifier for this binding (e.g., 'arrow', 'custom')

props

{
  [K in keyof Props]: T.Validatable<Props[K]>
}

Optional validation schema for binding-specific properties

meta

{
  [K in keyof Meta]: T.Validatable<Meta[K]>
}

Optional validation schema for metadata fields

Returns

T.ObjectValidator<
  import('@tldraw/utils').Expand<
    {
      [P in
        | 'fromId'
        | 'id'
        | 'meta'
        | 'toId'
        | 'typeName'
        | (undefined extends Props ? never : 'props')
        | (undefined extends Type ? never : 'type')]: TLBaseBinding<
        Type,
        Props
      >[P]
    } & {
      [P_1 in
        | (undefined extends Props ? 'props' : never)
        | (undefined extends Type ? 'type' : never)]?:
        | TLBaseBinding<Type, Props>[P_1]
        | undefined
    }
  >
>

A validator object that can validate complete binding records

Prev
createBindingPropsMigrationSequence
Next
createPresenceStateDerivation