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:
Parameter | Type | Description |
---|---|---|
builder | object | The builder definition. |
schema | object | The schema that was built using the provided builder definition. |
options | object optional | An optional partial object with initialization options. |
The options
parameter properties:
Property | Type | Description |
---|---|---|
initialData | object optional | The optional partial initial data. |
initialEntitiesValuesWithDefaults | boolean optional | A 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.
Method | Type | Description |
---|---|---|
getData | function | Retrieves the store's data. |
getEntitiesErrors | function | Retrieves the entities' validation errors from the store's data. |
getEntitiesValues | function | Retrieves the values of entities from the store's data. |
getUnprocessableEntitiesIds | function | Retrieves the IDs of entities excluded from processing from the store's data. |
setData | function | Sets the store's new data. |
subscribe | function | Subscribes to the store's events, returning a function () => void for unsubscribing. |
validateEntityValue | function | An async function that triggers the validation of a single entity. |
validateEntitiesValues | function | An async function that triggers the validation of all entities. |
setEntityValue | function | Sets the value of an entity. |
resetEntityValue | function | Resets the value of an entity to its default. |
resetEntitiesValues | function | Resets the values of all entities to their defaults. |
clearEntityValue | function | Clears the value of an entity. |
clearEntitiesValues | function | Clears the values of all entities. |
setEntityError | function | Sets the validation error of an entity. |
resetEntityError | function | Resets the validation error of an entity. |
resetEntitiesErrors | function | Resets the validation errors of all entities. |
setEntitiesErrors | function | Sets the validation errors of all entities. |
isEntityProcessable | function | Returns a boolean indicating whether an entity is processable or not. |
getEntityValue | function | Retrieves the value of a specific entity. |
getEntityError | function | Retrieves the validation error of a specific entity. |
builder | object | The builder definition used to instantiate the store. |
schema | object | The schema used to instantiate the store. |
Data
The data of the interpreter store is an object containing the following properties:
Property | Type | Description |
---|---|---|
entitiesValues | object | Represents the values of entities. |
entitiesErrors | object | Represents the validation errors of entities. |
unprocessableEntitiesIds | array | Represents 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:
Event | Description |
---|---|
EntityValueUpdated | An entity's value was updated. |
EntityErrorUpdated | An entity's validation error was updated. |
EntityUnprocessable | An entity was marked as unprocessable. |
EntityProcessable | An entity was marked as processable. |
DataSet | The data was manually set. |