Core API Reference

createInterpreterStore

This function instantiates an interpreter store, used for for filling entities values based on a schema and builder definition.

In most cases, you won't need to use the createInterpreterStore method directly. Instead, you will mostly utilize useInterpreterStore from @coltorapps/builder-react, which essentially creates the interpreter store for you.

Reference

createInterpreterStore(builder, schema, options?)

Use the createInterpreterStore function to instantiate an interpreter store.

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

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

const formSchema = {
  entities: {
    "51324b32-adc3-4d17-a90e-66b5453935bd": {
      type: "textField",
      attributes: {
        label: "First name",
      },
    },
  },
  root: ["51324b32-adc3-4d17-a90e-66b5453935bd"],
};

export const interpreterStore = createInterpreterStore(formBuilder, schema);

In the example above, we've hardcoded the schema, but typically, you would retrieve it from a database, for instance.

Parameters

createInterpreterStore accepts three parameters:

ParameterTypeDescription
builderobjectThe builder definition.
schemaobjectThe schema that was built using the provided builder definition.
optionsobject optionalAn optional partial object with initialization options.

The options parameter properties:

PropertyTypeDescription
initialDataobject optionalThe optional partial initial data.
initialEntitiesValuesWithDefaultsboolean optionalA flag to disable the automatic setting of default values. Defaults to true.

Returns

createInterpreterStore instantiates an interpreter store, providing a set of methods to operate with the store.

MethodTypeDescription
getDatafunctionRetrieves the store's data.
getEntitiesErrorsfunctionRetrieves the entities' validation errors from the store's data.
getEntitiesValuesfunctionRetrieves the values of entities from the store's data.
getUnprocessableEntitiesIdsfunctionRetrieves the IDs of entities excluded from processing from the store's data.
setDatafunctionSets the store's new data.
subscribefunctionSubscribes to the store's events, returning a function () => void for unsubscribing.
validateEntityValuefunctionAn async function that triggers the validation of a single entity.
validateEntitiesValuesfunctionAn async function that triggers the validation of all entities.
setEntityValuefunctionSets the value of an entity.
resetEntityValuefunctionResets the value of an entity to its default.
resetEntitiesValuesfunctionResets the values of all entities to their defaults.
clearEntityValuefunctionClears the value of an entity.
clearEntitiesValuesfunctionClears the values of all entities.
setEntityErrorfunctionSets the validation error of an entity.
resetEntityErrorfunctionResets the validation error of an entity.
resetEntitiesErrorsfunctionResets the validation errors of all entities.
setEntitiesErrorsfunctionSets the validation errors of all entities.
isEntityProcessablefunctionReturns a boolean indicating whether an entity is processable or not.
getEntityValuefunctionRetrieves the value of a specific entity.
getEntityErrorfunctionRetrieves the validation error of a specific entity.
builderobjectThe builder definition used to instantiate the store.
schemaobjectThe schema used to instantiate the store.

Data

The data of the interpreter store is an object containing the following properties:

PropertyTypeDescription
entitiesValuesobjectRepresents the values of entities.
entitiesErrorsobjectRepresents the validation errors of entities.
unprocessableEntitiesIdsarrayRepresents the entities IDs excluded from processing.

Events

The interpreter store emits various events after mutations to subscribed listeners, with different payloads based on the event. A mutation might cause the store to emit multiple events simultaneously. These events can be emitted by the store:

EventDescription
EntityValueUpdatedAn entity's value was updated.
EntityErrorUpdatedAn entity's validation error was updated.
EntityUnprocessableAn entity was marked as unprocessable.
EntityProcessableAn entity was marked as processable.
DataSetThe data was manually set.
Previous
createBuilderStore