# Validation rules

> Refuse a save or a submit with your own message in English and Arabic: a condition on an invoice, a bill, an expense, a document or a journal entry, checked at the stage you choose. The editor, the API and the assistant answer with the same message, so a record your rule refuses cannot enter by another door.

Source: /help/en/settings/validation-rules

A validation rule states what a record must not look like. When a record meets the rule's condition, the save or the submit is refused and the person (or the system) reads your message. The rules are under **Settings → Automation** → the **Validation** tab (`/settings/automation?tab=validation`). Owners and admins write them; they are available on every plan.

A validation rule has:

- **Record type**: invoices, bills, expenses, documents (quotes, order confirmations, delivery notes) or journal entries (manual journals only).
- **Condition**: the record is refused when it meets the condition (**All of these**, **Any of these**, **Not**, and groups inside groups).
- **Stage**: **On save** (the record cannot be saved, not even as a draft) or **On submit** (a draft can be saved; submitting, issuing, accepting or posting it is refused).
- **Message** and **Message (Arabic)**: what the reader sees. Write what to correct, not what went wrong.

A *Draft* or *Paused* rule refuses nothing. The **Test** card of the rule page runs the condition against the newest fifty records, or the ones you tick, and shows which would be refused; a test changes nothing.

## Walkthrough: the TRN rule in the editor, over the API and through the assistant

The rule: an invoice to a customer without a TRN cannot be submitted.

1. **Settings → Automation** → the **Validation** tab → **New validation rule**.
2. **Name**: `TRN before submit`; **Record type**: **Invoices**.
3. **Condition**: **Add a condition**: **Customer TRN** **is empty**. To limit the rule to standard-rated invoices, add **VAT** **greater than** `0` under **All of these**.
4. **Action**: **Stage**: **On submit**; **Message**: `An invoice to a customer without a TRN cannot be submitted.`; **Message (Arabic)**: `لا يمكن إرسال فاتورة إلى عميل بلا رقم تسجيل ضريبي.`
5. **Save**, then **Enable** in the header. The status reads *Active*.

**In the editor.** Open a draft invoice to a customer without a TRN. Saving the draft works, because the rule's stage is submit. Click the submit button: the editor checks the rules first, the invoice is not sent, and above the buttons an alert reads **A validation rule refused this record** with your message and "Rule: TRN before submit". When several rules refuse the record, the alert lists each message. In Arabic the alert shows the Arabic message.

**Over the API.** `POST /invoices/{id}/submit` for the same kind of invoice answers HTTP `422`:

```json
{
  "code": "validation_rule_failed",
  "message": "An invoice to a customer without a TRN cannot be submitted.",
  "nextStep": "A validation rule of the organisation refused the record; the message names what to correct, or an administrator changes the rule under Settings → Automation.",
  "extra": {
    "ruleKey": "trn_before_submit",
    "stage": "submit",
    "message": "An invoice to a customer without a TRN cannot be submitted.",
    "messageAr": "لا يمكن إرسال فاتورة إلى عميل بلا رقم تسجيل ضريبي."
  }
}
```

`extra` also carries `ruleId` and `failing`, the list of every rule that refused the record. The dry run (`POST /invoices/validate`) answers `validationRules` with the failing rules under `save` and `submit`, so an integration can show the message before it submits. Nothing is submitted and the invoice stays a draft.

**Through the assistant.** Ask the assistant to submit the invoice. `submit_invoice` is refused with the same code and the same message, and the assistant passes on the next step: add the customer's TRN, or ask an administrator to change the rule. It does not retry or look for another route.

**The fix.** Add the TRN on the customer (**Contacts** → the customer → **TRN (Tax Registration No.)**) and on the draft invoices, then submit again: the editor, the API and the assistant all accept it.

## Where the rules are checked

| Record | On save | On submit |
|---|---|---|
| Invoice | saving the draft, creating or replacing it over the API, a housekeeping edit | submit (one or many) |
| Bill | recording and editing the bill, on the bill's page and over the API | accepting the bill |
| Expense | creating and editing | submitting |
| Document | saving | the move from draft to sent |
| Journal entry (manual) | saving the draft | submitting it for approval, and posting |

The check runs inside the same database functions the app, the API and the assistant call, so the three cannot differ. A bulk action answers per record: the refused ones carry the rule's message and the others go through.

## Good to know

- A validation rule only refuses. It changes nothing on the record; to set a field or alert somebody, write a [workflow rule](/help/en/settings/workflow-rules).
- A check that cannot run in the browser (for example when you are offline) never blocks the editor; the save and submit functions on the server stay the authority and refuse the record there.
- The message is yours: the app does not translate it. Fill in both languages.
- The error code is always `validation_rule_failed`; match on the code and on `extra.ruleKey`, not on the text.

## API and assistants

- `POST /automation/rules` with `kind: "validation"`, an `entity`, a `stage` (`save` or `submit`), a `condition` and `action: {"type": "refuse", "message": "…", "message_ar": "…"}`.
- `POST /automation/validate` with `entity`, `row` and `stage`: checks a row against the active rules without saving anything.
- `POST /automation/rules/{id}/test`: the rule against records.
- MCP: `save_automation_rule` (an assistant saves a draft; a person enables it), `preview_automation_rule`, `test_automation_rule`, `list_rule_fields`, `list_automation_rules`.

Reference: docs.alyabooks.ai.

## Related

- [Workflow rules](/help/en/settings/workflow-rules)
- [Posting approvals](/help/en/settings/posting-approvals)
- [Custom fields](/help/en/settings/custom-fields)
