---
title: Authoring rules
description: Short examples of what check refuses or warns about, what to write,
  and where to find the rule.
url: https://docs.rastok.com/reference/authoring-rules
source_revision: v0.2.0-alpha
---

# Authoring rules

Use these pairs when writing a graph or reading a refusal. The left column is
code to diagnose, not a template. A warning still allows the graph to run.

| Rule | Refused or warned | Write instead | Diagnostic |
|---|---|---|---|
| [Render an integer](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#14-rendering-a-value-as-text) | `data as string` | `string(data)` | `E-EXPR` |
| [Narrow a uint to int](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/types.md#numeric-widening-and-the-common-type) | `data as int` | `(data as? int).orValue(0)` | `E-EXPR` |
| [Read an array](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#15-absence) | `data[0]` | `data[?0].orValue("none")` | `E-EXPR` |
| [Read a map field](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#15-absence) | `data.name` | `data.?name.orValue("none")` | `E-EXPR` |
| [Read a schemaless JSON object](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#5-narrowing) | `out = data is Receipt` | `val receipt = data as? Receipt; out = receipt != null` | `E-EXPR` |
| [Initialize state](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/instances.md#4-mutable-state-has-one-executor-owner) | `var last: string` | `var last: string = ""` | `E-EXPR` |
| [Test array membership](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#11-compatibility-oracle-and-divergence-ledger) | `data.exists(x, x == "red")` | `"red" in data` | `E-EXPR` |
| [Test a map key](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#11-compatibility-oracle-and-divergence-ledger) | `1 in data` | `"id" in data` | `E-EXPR` |
| [Build an array](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#11-compatibility-oracle-and-divergence-ledger) | `["a"] + ["b"]` | `["a", "b"]` | `E-EXPR` |
| [Test a string](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/expressions.md#1-expression-contract) | `data.matches("^a.*b.*c$")` | `data.startsWith("a") && data.contains("b") && data.endsWith("c")` | `E-EXPR` |
| [Choose when a key expires](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/instances.md#6-lifetime) | `instance = { key = data }` | `instance = { key = data }; lifetime = { kind = idle; max = duration("5m") }` | `E-LIFETIME-REQUIRED` |
| [Close an outbound Call](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/instances.md#71-the-bounded-closing-contract) | `lifetime = { kind = once }` | `lifetime = { kind = once }; closing = { drain_for = duration("1s"); settle_for = duration("1s") }` | `E-INSTANCE` |
| [Discard a lost answer](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/delivery-semantics.md#44-recovery--a-triage-not-a-gamble) | `_ => out = "failed"` | `_ => nothing` | `W-MATCH-CATCHALL` |
| [Mock HTTP 204](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/stdlib.md#32-httpclient--outbound-http) | `out = "body"; exit = Completed { status = 204 }` | `exit = Completed { status = 204 }` | `E-CONTRACT-REQUIRED` |
| [Mock HTTP 205](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/stdlib.md#32-httpclient--outbound-http) | `out = "body"; exit = Completed { status = 205 }` | `exit = Completed { status = 205 }` | `E-CONTRACT-REQUIRED` |
| [Describe a codec refusal](https://github.com/rastok-io/rastok/blob/v0.2.0-alpha/docs/spec/stdlib.md#32-httpclient--outbound-http) | `exit = DecodeFailed {}` | `exit = DecodeFailed { message = "expected a status" }` | `E-PORT-TYPE` |

`int` and `uint` are signed and unsigned 32-bit integers. Either fits a
`long`; a computed `long` need not fit either narrower type. For an array
received at a port, even index zero might be absent. Choose a fallback only
if it means something for your application; otherwise handle null separately.

A schemaless JSON codec delivers an object as a map. If it does not fit
`Receipt`, `receipt` is null. Use `is` for a typed union or its case.
An HTTP timeout does not
prove that the operation failed. If you record an outcome instead of
dropping it, name each lost-answer case the warning lists before the
wildcard, even if several cases go to the same destination.

For example, this handler distinguishes an unknown outcome from a known failure:

```rkg
on in match data {
  Completed => out = "answered"
  BodyTooLarge | DecodeFailed | OutcomeUnknown | ProtocolFailed | TimeoutExpired => out = "unconfirmed"
  _ => out = "failed"
}
```

Lifetime choices are `persistent`, `idle`, `age`, `slot` and `once`.
A finite owner with an outbound Connector also declares its closing budgets.
A 204 or 205 has a terminal but no body. A codec refusal carries a message
about the expected data, not a copy of the value received.

## Specified, not shipped

| Not available | Alternative in this release |
|---|---|
| The iteration macro `exists` | Use [membership](/reference/expressions.md#operators) for an element or key. |
| Array concatenation with `+` | Construct the known elements explicitly. There is no general concatenation operator. |
