# JSON Schema for dcupl models and loader files

dcupl publishes a JSON Schema for each of its own file types: model definitions (`*.dcupl.json`), the loader configuration (`dcupl.lc.json`), REST API templates, workflow templates and test configuration. Point your editor at them and you get autocomplete, hover docs and errors while you type. Separately, the CLI generates a JSON Schema of your **data** from your models, for validators and code generators outside dcupl.

## Which one you need

| You want to                                                   | Use                                                                                 |
| ------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| Autocomplete and errors while you edit model and loader files | The [published dcupl JSON Schemas](#the-published-json-schemas) in your editor      |
| Check a whole project before you push, or in CI               | [`dcupl validate`](#model-and-loader-files-dcupl-validate)                          |
| Check a workflow template or REST API template file           | [`dcupl schemas validate`](#workflow-and-rest-api-templates-dcupl-schemas-validate) |
| A JSON Schema that describes the items of your models         | [`dcupl generate json-schema`](#generate-a-json-schema-from-your-models)            |
| Look up the fields of a dcupl file type in the terminal       | [`dcupl schemas get`](/cli/schemas)                                                 |

## The published JSON Schemas

The schemas are generated from the SDK's types and served from GitHub. Every URL starts with `https://raw.githubusercontent.com/dcupl/dcupl-schema/main/bin/`:

| File type            | File pattern                       | Schema file                                     |
| -------------------- | ---------------------------------- | ----------------------------------------------- |
| Model definitions    | `*.dcupl.json`                     | `dcupl.model.vscode.schema.json`                |
| Loader configuration | `dcupl.lc.json`, `*.dcupl.lc.json` | `dcupl.loader.vscode.schema.json`               |
| REST API templates   | `*.api.json`                       | `dcupl.api.vscode.schema.json`                  |
| Workflow templates   | `*.workflow.json`                  | `dcupl.workflow.vscode.schema.json`             |
| Test configuration   | `*.dcupl.test-config.json`         | `dcupl.test-config.vscode.schema.json`          |
| Test scenario loader | `dcupl.test-scenario-loader.json`  | `dcupl.test-scenario-loader.vscode.schema.json` |

- Each schema also ships as `dcupl.<name>.schema.json` (without `vscode`). The two files differ only in their snippets: the `vscode` variant carries them in the form VS Code reads. Use it in VS Code, the other one anywhere else.
- `main` holds the latest regeneration. Each regeneration is tagged with the SDK version it was built from; to pin one, replace `main` in the URL with one of the repository's tags.
- The Console's code editor checks the same files against the same schemas.
- A workflow template in format version 3, as `dcupl generate workflow` writes it (`*.workflow-v3.json`), has no editor schema: check it with [`dcupl schemas validate`](#workflow-and-rest-api-templates-dcupl-schemas-validate).

## Use the schema in your editor

### VS Code

`dcupl init` writes the mapping for every file type into `.vscode/settings.json`, so a scaffolded project validates as soon as you open it. For a project that has none, add it yourself; these are the model and loader entries:

```json filename=".vscode/settings.json"
{
  "json.schemas": [
    {
      "fileMatch": ["*.dcupl.json"],
      "url": "https://raw.githubusercontent.com/dcupl/dcupl-schema/main/bin/dcupl.model.vscode.schema.json"
    },
    {
      "fileMatch": ["*.dcupl.lc.json", "dcupl.lc.json"],
      "url": "https://raw.githubusercontent.com/dcupl/dcupl-schema/main/bin/dcupl.loader.vscode.schema.json"
    }
  ]
}
```

The mapping works by file name, so keep the dcupl suffixes: a model in `product.json` gets no schema, `product.dcupl.json` does.

### Other editors

Any editor that reads JSON Schema takes the same two things: the schema URL and the file pattern from the table above. In JetBrains IDEs (WebStorm, IntelliJ IDEA) add them under **Settings → Languages & Frameworks → Schemas and DTDs → JSON Schema Mappings**, with the schema file without `vscode`.

### Why not `$schema` in the file

A `$schema` key at the top of a JSON file is the other common way to attach a schema. dcupl's model and loader schemas list only dcupl's own keys and allow no others, so the editor reports the `$schema` key itself as an error (VS Code: `Property $schema is not allowed.`). Map the schema by file name instead, as shown above.

## Validate dcupl files

### Model and loader files: dcupl validate

The JSON Schema catches a misspelled key or a wrong type in one file. `dcupl validate` loads the whole project the way your app does and reports what the schema cannot see, such as a reference to a model that does not exist or data that does not fit its model. Add `--fail-on critical` to fail a CI job on it; the options and the report are on [Validating a project](/cli/validate).

```bash filename="terminal"
dcupl validate --fail-on critical
```

To check model and loader files against the published schema outside the CLI, any JSON Schema validator for draft-07 works. Ajv needs `strict: false`, because its strict mode rejects the snippet keyword (`defaultSnippets`) the schemas carry.

### Workflow and REST API templates: dcupl schemas validate

`dcupl schemas validate` checks a JSON file against one of the CLI's built-in runtime schemas, the `[Zod]` entries of `dcupl schemas list`: workflow templates (`TemplateV3`), REST API templates (`RestApiTemplate`) and the workflow node configs.

```bash filename="terminal"
dcupl schemas validate dcupl/workflows/sync-products.workflow-v3.json --against TemplateV3
```

```text title="Output"
✓ valid against TemplateV3
```

An invalid file lists each error with its path and exits 1:

```text title="Output"
✗ key → Invalid input: expected string, received undefined

✗ invalid — 1 error(s) against TemplateV3
```

With `--json` the result is `{ "valid": false, "errors": [{ "path": "key", "message": "…" }] }`. `ModelDefinition` and `AppLoaderConfiguration` are TypeScript types without a runtime schema, so `schemas validate` refuses them with `SCHEMA_NOT_VALIDATABLE`: use `dcupl validate` or the published JSON Schema for those files.

### Look up a schema in the terminal

`dcupl schemas get <name>` prints a schema's TypeScript or Zod source, and `--example` adds a sample payload where one ships (see [Inspecting schemas](/cli/schemas)). With `--json` the output wraps that source in a `source` field; it is not a JSON Schema. Use the published files above.

## Generate a JSON Schema from your models

A dcupl model is not a JSON Schema: it lists properties with dcupl types (`string`, `int`, `float`, `date`, …) and references to other models, and the SDK uses it to load, check and index your data. `dcupl generate json-schema` turns the models of your workspace into a JSON Schema (draft-07) that describes their items, which you can hand to tools outside dcupl.

Starting from a JSON data file in a dcupl project (`dcupl init`) takes two commands. First infer a model from the data (`--dry-run` previews it without writing a file; see [Scaffolding](/cli/scaffolding)):

```json filename="products.json"
[
  { "sku": "P-100", "name": "Trail backpack", "price": 89.9, "inStock": true },
  { "sku": "P-101", "name": "Rain jacket", "price": 129, "inStock": false }
]
```

```bash filename="terminal"
dcupl generate model --from products.json --name Product --key-property sku
```

```json filename="dcupl/models/Product.dcupl.json"
{
  "key": "Product",
  "references": [],
  "properties": [
    { "key": "sku", "type": "string" },
    { "key": "name", "type": "string" },
    { "key": "price", "type": "float" },
    { "key": "inStock", "type": "boolean" }
  ],
  "keyProperty": "sku"
}
```

Then generate the JSON Schema of all models in the workspace. The file lands in `cli-output/`:

```bash filename="terminal"
dcupl generate json-schema --file-name products.schema.json
```

```json filename="cli-output/products.schema.json"
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "Product": {
      "title": "Product",
      "type": "object",
      "properties": {
        "key": { "type": "string" },
        "sku": { "type": "string" },
        "name": { "type": "string" },
        "price": { "type": "number" },
        "inStock": { "type": "boolean" }
      },
      "required": ["key", "sku", "name", "price", "inStock"],
      "additionalProperties": false
    },
    "dcupl_models": {
      "type": "object",
      "properties": {
        "Product": { "$ref": "#/definitions/Product" }
      },
      "additionalProperties": false
    }
  },
  "$ref": "#/definitions/dcupl_models"
}
```

### How the schema is built

- One definition per model, named after the model key. The root object has one property per model that points at it, so a single item is `#/definitions/Product`.
- Every definition requires `key`, the item key dcupl gives every item, plus every property and reference, and allows no other fields. If your source data identifies items by another field (`keyProperty`) and carries no `key`, take `key` out of `required` before you validate that data.
- A single-valued reference points at the referenced model's definition (`$ref`), a multi-valued one is an array of them.

| dcupl type                       | JSON Schema type                                 |
| -------------------------------- | ------------------------------------------------ |
| `string`                         | `string`                                         |
| `int`                            | `integer`                                        |
| `float`                          | `number`                                         |
| `boolean`                        | `boolean`                                        |
| `date`                           | `string`                                         |
| `json`, `any`                    | `object`                                         |
| `Array<string>` and other arrays | `string` for now: change them to `array` by hand |

The file is plain JSON Schema, so standard validators such as Ajv read it as it is, and JSON Schema code generators such as quicktype turn it into types for other languages. For TypeScript, `dcupl generate typescript` writes the types directly (see [Scaffolding](/cli/scaffolding)).

## Next steps

- [Validating a project](/cli/validate) — Load the whole project and gate CI on the report
- [Inspecting schemas](/cli/schemas) — List, print and validate against the built-in schemas
- [Scaffolding](/cli/scaffolding) — Infer models from CSV or JSON and generate types
- [Models](/sdk/core-concepts/models) — Property types, references and model options
