# Validation and JSON Schema (/policy/validation)

## JSON Schema in your editor [#json-schema-in-your-editor]

Upwell publishes JSON Schemas for both files. Add `$schema` to get autocomplete, hover docs and
inline errors in VS Code and other editors:

```json
{
  "$schema": "https://upwell.run/schemas/policy.schema.json",
  "schemaVersion": 1
}
```

| File | Schema |
| --- | --- |
| `policy.json` | `https://upwell.run/schemas/policy.schema.json` |
| `releases.json` | `https://upwell.run/schemas/releases.schema.json` |

The schemas also ship in the packages (`upwell/schemas/policy.schema.json`, or
`@upwell/core/schemas/policy.schema.json`), so you can validate in CI with any JSON Schema validator:

```bash
npx ajv-cli validate --spec=draft7 -s node_modules/upwell/schemas/policy.schema.json -d policy.json
```

## The runtime validator [#the-runtime-validator]

Every fetched policy goes through `validatePolicy()` before it's used. It's stricter than the schema:

| Check | Result |
| --- | --- |
| `schemaVersion` isn't `1` | Error |
| `version` / `build` aren't comparable (e.g. `"three"`) | Error |
| `releasedAt` isn't an ISO date | Error |
| `minimum.build` is above `latest.build` | Error: every install would be blocked with nothing to update to |
| `rollout` outside 0–100, negative day counts | Error |
| Unknown `applyMode` | Error |
| `appStoreId` isn't numeric, `country` isn't two letters | Error |
| `requireAfterDays` below `recommendAfterDays` | Warning: users skip the recommendation |
| Unknown keys | Warning, ignored |

**Errors** reject the whole policy, and Upwell keeps using the last valid copy. **Warnings** don't stop
anything, so newer policies stay compatible with older app versions.

`releases.json` is validated the same way with `validateReleases()`: required fields, known `kind`
and `platforms` values, ISO timestamps, and unique ids.

## Validating in CI [#validating-in-ci]

The [CLI](/docs/reference/cli) runs the same validators and fails with exit code 1 on errors:

```bash
npx @upwell/cli policy validate
npx @upwell/cli releases validate
```

Or call them yourself: the validators are plain functions from
[`@upwell/core`](/docs/reference/core#upwellcore), which has no dependencies, so a plain Node script
can run them and fail CI before you publish a broken policy:

```js title="scripts/check-policy.mjs"
import { readFileSync } from 'node:fs';
import { validatePolicy, validateReleases } from '@upwell/core';

const read = (path) => JSON.parse(readFileSync(path, 'utf8'));
let failed = false;
for (const [name, result] of [
  ['policy.json', validatePolicy(read('upwell/policy.json'))],
  ['releases.json', validateReleases(read('upwell/releases.json'))],
]) {
  for (const w of result.warnings) console.warn(`${name}: ${w}`);
  if (!result.ok) {
    for (const e of result.errors) console.error(`${name}: ${e}`);
    failed = true;
  }
}
process.exit(failed ? 1 : 0);
```

Add `@upwell/core` as a dev dependency for this (it's already installed with `upwell`, but list
what you import). Or run the same checks in your app's existing Jest setup:

```ts title="__tests__/upwell-policy.test.ts"
import { validatePolicy, validateReleases } from 'upwell';

import policy from '../upwell/policy.json';
import releases from '../upwell/releases.json';

test('policy.json is valid', () => {
  const result = validatePolicy(policy);
  expect(result.ok ? [] : result.errors).toEqual([]);
  expect(result.warnings).toEqual([]);
});

test('releases.json is valid', () => {
  const result = validateReleases(releases);
  expect(result.ok ? [] : result.errors).toEqual([]);
});
```

You can also assert decisions there with [`decide()`](/docs/reference/core#decide), e.g. "build 290
is required to update".

<Callout>
  A dedicated `npx @upwell/cli validate` command is on the [roadmap](/docs/resources/roadmap).
</Callout>

## Seeing errors on a device [#seeing-errors-on-a-device]

Validation errors show up in the trace:

```
policy: latest fetch failed (invalid policy: policy.android.minimum.build: 400 is above latest.build 340; …); using last known good
```

Read it with `explain()`, the [dev menu](/docs/reference/ui#upwelldevmenu), `debug: true`, or `onError`.
