Block validation with JavaScript

A block template can run a JavaScript validator. InfraBuffet calls it when someone creates or edits a block, after the normal checks on names, field types, and references. Use it for rules a form schema cannot express, and to compute values the module needs.

The validator does not run when someone calculates a diff or applies a plan.

What runs, in order

  1. Name and field checks: required fields, types, bounds, choices, and regular expressions.
  2. Reference checks: a selected provider or block must exist in the infrastructure or scope.
  3. Your validate function.
  4. Field checks again, so values your function sets are checked too.
  5. Reference type, owner-managed dependency, and circular dependency checks.

Write the function

Define a top-level validate(block, context) function. It runs synchronously.

function validate(block, context) {
  if (block.values.vlan === 200) {
    return "VLAN 200 is reserved";
  }
}
  • Return nothing, or any falsy value such as null, false, or "", to accept the block.
  • Return a message to reject it. A plain true is shown as the text “true”, so return a useful message.
  • Throwing an error also rejects the block.

Inside the function:

NameContents
block.idThe block’s ID. It is 0 for a new block.
block.nameThe block’s name.
block.valuesField values keyed by field name. Secret values are included.
block.template, block.scope, block.infraObjects with id and name.
context.userContextThe object entered as the template’s validator user context.
context.client.get(path, query)Read-only access to InfraBuffet records. Reference.

You can change block.name and block.values, and those changes are saved. Other properties are ignored, so you cannot move a block to another scope or template. Keep values an object. Keys that are not form fields are dropped.

Example: check input and compute a bridge name

Use this form schema:

[
  {"name": "vlan", "type": "number", "mode": "required", "min": 100, "max": 250},
  {"name": "description", "type": "string", "mode": "required"},
  {"name": "bridge_name", "type": "string", "mode": "internal"}
]

Set the template’s validator user context to {"name_prefix": "sandbox"}, then use this function:

function validate(block, context) {
  if (block.values.vlan === 200) {
    return "VLAN 200 is reserved";
  }
  var prefix = context.userContext.name_prefix;
  if (typeof prefix !== "string" || !prefix) {
    return "The template owner must configure name_prefix";
  }
  block.values.bridge_name = prefix + "-" + block.name;
}

The field already rejects fractional values and values outside 100–250; the script adds the reserved-VLAN rule. Required inputs must be present before the script runs. Computed fields therefore use internal, not a required field the script hopes to fill later. The module must accept bridge_name if this field is to be passed as an input.

Example: reject a VLAN already used in the scope

This function reads the blocks in the same scope and rejects a VLAN that another block already stores:

function validate(block, context) {
  var answer = context.client.get("/api/infras/" + block.infra.id + "/blocks", {
    scope: block.scope.id,
    values__vlan: String(block.values.vlan),
    size: 2
  });
  if (!answer.ok) {
    return "Cannot check VLAN availability: " + answer.data.detail;
  }
  var conflict = answer.data.items.some(function (other) {
    return other.id !== block.id;
  });
  if (conflict) {
    return "Another block in this scope already uses that VLAN";
  }
}

When you edit a block, the result includes that block, so the function ignores results with the same ID. Two results are enough to find another block. Add block_template: block.template.id to limit the check to one template.

This check reads saved records. Two requests saved at the same moment can both pass it. If a value must be unique, enforce that in the external system too.

Limits

  • The script runs in Goja, an ECMAScript engine. It is not Node.js or a browser. There is no require, no file access, and no fetch. The only way to read other data is context.client.
  • A validation has a one-second time limit. Keep loops short. Each run starts fresh, so do not rely on variables surviving between saves.
  • Each validation can make at most 20 client calls. Failed calls count too. Later calls return status 429.

Save-time checks and testing

  • Saving a template with a validator checks that the file loads and defines validate. It does not run any block.
  • If the validator file is missing, the template shows a warning, and every block save fails until the file exists.
  • A validator from Git updates after the source syncs. Sync before you test changes.

Before you rely on a validator, create a block that should be accepted and one that should be rejected. Edit an existing block as well, then calculate a diff to check that the module receives the values you expect.