Core API Reference

validateSchemaShape

This function validates that the input schema meets several essential requirements based on the provided builder, such as having a valid structure, correct references in relationships, proper relationship constraints, accurate entity types, and valid IDs.

It performs all the tasks of validateSchema, except it skips attributes values and custom schema validations, making this function synchronous.

In most cases, you wouldn't typically use this method, as it doesn't validate attributes values. However, you may find it useful occasionally.

You should know!

Schema's shape is consistently validated synchronously under the hood when instantiating both builder stores and interpreter stores.

Reference

validateSchemaShape(schema, builder)

Use the validateSchemaShape function to validate the input schema:

"use server";

import { validateSchemaShape } from "@coltorapps/builder";

import { formBuilder } from "./form-builder";

export async function validateFormSchema(schema: unknown) {
  const result = validateSchemaShape(schema, formBuilder);

  if (result.success) {
    /*
    | The `result.data` contains a valid schema
    | that can be stored in the database.
    */
  } else {
    /*
    | The `result.reason` holds the reason for
    | validation failure.
    */
  }
}

Parameters

validateSchemaShape accepts two parameters:

ParameterTypeDescription
schemaobjectThe schema that needs to be validated.
builderobjectThe builder definition used to build the schema.

Returns

validateSchemaShape returns a union of two possible objects, depending on the validation outcome. This function never throws errors, meaning that you only need to narrow down its result based on the value of the success key.

In case of successful validation, you receive the following object:

PropertyTypeDescription
successbooleanSet to true, indicating that the validation was successful.
dataobjectThe validated schema.

In case of failed validation, you receive the following object:

PropertyTypeDescription
successbooleanSet to false, indicating that the validation failed.
reasonobjectAn object describing the failure's reason.

The reason object will contain the following properties:

PropertyTypeDescription
codestringAn enum of possible error codes describing the reason.
payloadobject optionalAn optional object containing additional information based on the code.

Error Codes

In case of a failed validation, the failure reason code can be one of the following:

Error CodeDescription
InvalidRootFormatThe root must be an array of strings.
DuplicateRootIdDuplicate IDs detected in the root.
DuplicateChildIdDuplicate IDs detected in the children.
RootEntityWithParentRoot entities can't have a parent.
EmptyRootThe root must contain at least one entity.
NonexistentEntityIdA provided entity ID does not exist.
InvalidEntitiesFormatEntities should be an object containing valid entities.
MissingEntityTypeEntity type is missing.
UnknownEntityTypeThe provided entity type is unknown.
InvalidChildrenFormatThe provided children are invalid.
NonexistentEntityParentThe parent ID references a non-existent entity.
MissingEntityAttributesEntity attributes are missing.
InvalidEntityAttributesFormatThe provided entity attributes are invalid.
UnknownEntityAttributeTypeThe provided entity attribute type is unknown.
SelfEntityReferenceSelf entity reference.
ChildNotAllowedChild is not allowed.
EntityChildrenMismatchChildren relationship mismatch.
EntityParentMismatchParent relationship mismatch.
ParentRequiredA parent is required.
ParentNotAllowedParent is not allowed.
UnreachableEntityThe entity is not in the root and has no parent ID.
Previous
validateSchema