Core API Reference

validateSchema

This async 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, valid attributes, accurate entity types, and valid IDs.

In most cases, you'll call this function on the server side to validate the incoming built schema from the client.

Reference

validateSchema(schema, builder)

Use the validateSchema function to validate the input schema:

"use server";

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

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

export async function validateFormSchema(schema: unknown) {
  const result = await validateSchema(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

validateSchema accepts two parameters:

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

Returns

validateSchema 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.
InvalidEntitiesAttributesValidation has failed for some entities attributes.
InvalidSchemaCustom schema validation has failed.

Most of the time, when validating your schema on the server side, you will likely want to address InvalidEntitiesAttributes and, if applicable, InvalidSchema.

Previous
createInterpreterStore