# Event schemas

> Optional, versioned definitions of a custom event's properties. Herald checks every track event with that name, and either rejects or flags the ones that don't match.

Custom `track` events take any properties. That is flexible, but a typo or a type change in one app release can quietly break the goals, segments and personalization built on that event. An event schema states which properties an event should have, so Herald can catch that.

Schemas are optional. You define them only for the events you care about; every other event is accepted as it is.

## What a schema holds

A schema belongs to one app and one event name. Set it up in the console under **Settings → Event schemas** (admins). Names starting with `$` are reserved for the SDK's own events and can't have a schema. Each property has:

| Field | Values |
|---|---|
| Name | The property key |
| Type | `string`, `number`, `integer`, `boolean`, `object` or `array` |
| Required | Whether the event must have it. A `null` value counts as missing |
| Allowed values | Strings only: the values it may take |
| Max length | Strings only: the most characters it may have |
| Min and max | Numbers only: the range it must be in |

Properties the schema doesn't list are allowed and kept. An app can have up to 100 event schemas, each with up to 50 properties and 50 versions (see [Limits](/docs/reference/limits)).

## Versions

Every save creates a new version; a saved version never changes. Versions use SchemaVer, `MODEL-REVISION-ADDITION`, starting at `1-0-0`. The console shows the next version before you save, with the reason:

| Change from the latest version | Next version | Console reason |
|---|---|---|
| Only new optional properties, or anything else that keeps earlier events valid (more allowed values, a looser rule, a property made optional) | ADDITION: `1-0-0` → `1-0-1` | New optional properties only |
| Only the mode | ADDITION: `1-0-1` → `1-0-2` | Mode change only |
| A new required property, a removed property, a type change, fewer allowed values, a stricter max length, min or max, or an optional property made required | MODEL: `1-0-1` → `2-0-0` | Breaking change |

REVISION is reserved: Herald doesn't produce it. A save with no changes is refused (**No changes to save**). If someone else saved a newer version after you opened the form, your save is refused and you reload to see it. Each schema's version history is shown read-only in the console.

## Modes

| Mode | An event that doesn't match |
|---|---|
| **Reject invalid events** | Is rejected with reason `schema_violation` and left out of the batch; the rest of the batch is kept |
| **Accept and warn** | Is kept as usual, and recorded as a warning |

Mode belongs to each version. An event checked against a version, the latest or a pinned one, uses that version's mode, so saving a new version in **Accept and warn** doesn't change how events that pin an older version are checked.

Use **Accept and warn** while you roll out a change: save the new version in that mode, watch the warnings while app versions update, then save it again with **Reject invalid events**. Each mode switch saves a new ADDITION version.

## Pinning a version from the app

By default an event is checked against the latest version of its schema. An app release built for an older version can name it, so a breaking change for newer releases doesn't reject its events:

```swift
Herald.shared.track("signup", properties: ["plan": "pro"], schemaVersion: "1-0-0")
```

```kotlin
Herald.track("signup", mapOf("plan" to "pro"), schemaVersion = "1-0-0")
```

`schemaVersion` is optional, in SDK 0.6.0 and later; existing `track` calls are unchanged. It is sent as the event's `schemaVersion` field ([Event payload](/docs/reference/event-payload#track)). A version the schema doesn't have rejects the event with `unknown_schema_version`. For an event name with no schema, `schemaVersion` is ignored. The SDKs leave off a value that isn't a version like `1-0-0`, with a debug log, and still send the event.

Events your backend sends with [`POST /api/v1/events`](/docs/reference/server-api#post-api-v1-events) are checked the same way, and take the same optional `schemaVersion` field.

## How failures show up

Rejected events and warnings, from the SDK or your backend, are listed under **Rejected events** on the SDK connection's page in the console (**Settings → Sending & SDK**, then the connection), kept for 30 days. A warning shows as **Schema warning (accepted)**. Up to 200 warnings are stored per app a day, apart from the 1,000 rejected events, so a busy event in **Accept and warn** mode never hides real rejects. The detail names the first failing property and rule, and the version checked, such as `properties.plan: must be one of free, pro (schema signup 1-0-0)`.

A rejected event is also listed in the response's `rejected`, which the SDKs log at debug level and never retry. A warning isn't. See [Rejected events](/docs/reference/error-codes#rejected-events) for every reason code.
