Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 21 additions & 50 deletions distribution/examples/validation/json-schema/README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,32 @@
# Validation - JSON Schema
# Validation - JSON Schema (simple + schemaMappings)

This sample explains how to set up and use the `validator` plugin, utilizing JSON schemas for validation.
This example now combines both:
- a simple JSON Schema validation setup, and
- a setup that uses `$id`/`$ref` URN-based references resolved via `schemaMappings`.


## Running the Example
## Running the example

1. Go to the directory `<membrane-root>/examples/validation/json-schema`.
2. Start `membrane.cmd` (Windows) or `./membrane.sh` (Unix/macOS).

2. Start `membrane.cmd` or `membrane.sh`.

3. Look at `schema2000.json` and compare the schema to `good2000.json` and `bad2000.json`.

4. Run `curl -d @good2000.json http://localhost:2000/` on the console. Observe that you get a successful response.

5. Run `curl -d @bad2000.json http://localhost:2000/`. Observe that you get a validation error response.

Keeping the router running, you can try a more complex schema.

1. Have a look at `schema2001.json`, `good2001.json` and `bad2001.json`.

2. Run `curl -d @good2001.json http://localhost:2001/`. Observe that you get a successful response.
Simple validation:
- Inspect `schemas/schema2000.json`, then compare with `good2000.json` and `bad2000.json`.
- `curl -H "Content-Type: application/json" -d @good2000.json http://localhost:2000`
- `curl -H "Content-Type: application/json" -d @bad2000.json http://localhost:2000`

3. Run `curl -d @bad2001.json http://localhost:2001/`. Observe that you get a validation error response.
Schema with `$ref` URNs resolved via schemaMappings:
- Inspect `schemas/schema2001.json` referencing `urn:app:base_def` and `urn:app:meta_def`.
- See the mapped schemas under `schemas/base.json` and `schemas/meta.json`.
- `curl -H "Content-Type: application/json" -d @good2001.json http://localhost:2001`
- `curl -H "Content-Type: application/json" -d @bad2001.json http://localhost:2001`

## How it is done

Let's examine the `proxies.xml` file.

```xml
<router>
<api port="2000">
<request>
<validator jsonSchema="schema2000.json" />
</request>
<target host="localhost" port="2002" />
</api>

<api port="2001">
<request>
<validator jsonSchema="schema2001.json" />
</request>
<target host="localhost" port="2002" />
</api>

<api port="2002">
<groovy>
Response.ok("&lt;response&gt;good request&lt;/response&gt;").build()
</groovy>
</api>
</router>
```

We define three `<api>` components, running on ports 2000-2002.
The first two validate all requests using the JSON schema defined in the `<validator />` component's `jsonSchema` attribute.
The successfully validated requests then get sent to the third `<api>` component, where we simply return a 200 "Ok" response.
Take a look at the `apis.yaml`. It includes two APIs for validation and one backend:
- Port 2000 validates requests against a standalone schema `schemas/schema2000.json`.
- The schemaMappings example (port 2001) loads `schemas/schema2001.json` which contains `$ref` URNs. Those URNs are mapped to local files via `schemaMappings` so validation can resolve them.

---
See:
- [JSON Schema](https://json-schema.org/) documentation
- [validator](https://membrane-api.io/docs/current/validator.html) reference
See:
- JSON Schema: https://json-schema.org/
- Membrane validator reference: https://membrane-api.io/docs/current/validator.html
17 changes: 13 additions & 4 deletions distribution/examples/validation/json-schema/apis.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.5.json
# Simple JSON Schema validation example
api:
port: 2000
flow:
- request:
- validator:
jsonSchema: schema2000.json
jsonSchema: schemas/schema2000.json
target:
host: localhost
port: 2002

---

# JSON Schema with schemaMappings for resolving $ref URNs
api:
port: 2001
flow:
- request:
- validator:
jsonSchema: schema2001.json
jsonSchema: schemas/schema2001.json
schemaMappings:
schemas:
- schema:
id: urn:app:base_def
location: schemas/base.json
- schema:
id: urn:app:meta_def
location: schemas/meta.json
target:
host: localhost
port: 2002

---

# Backend
api:
port: 2002
flow:
Expand Down
18 changes: 11 additions & 7 deletions distribution/examples/validation/json-schema/bad2001.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"id": "93241279A",
"name": "CITROEN Berlingo HDi 90",
"price": { "incl_vat": 7972.00 },
"tags": "diesel",
"weight": 131,
"transmission": "5 gears"
}
"params": [
{
"name": "mode",
"value": "fast"
}
],
"meta": {
"source": "",
"unexpected": 123
}
}
23 changes: 15 additions & 8 deletions distribution/examples/validation/json-schema/good2001.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
{
"id": 106825605,
"name": "FORD Fusion 1.25 Style",
"price": 8990.00,
"tags": ["black", "metallic", "gasoline" ],
"weight": 1145,
"co2": 140,
"transmission":"manual"
}
"params": [
{
"name": "mode",
"value": "fast"
},
{
"name": "retries",
"value": 3
}
],
"meta": {
"source": "curl",
"requestId": "REQ-12345678"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@

<router>

<!-- Simple JSON Schema validation example -->
<api port="2000">
<request>
<validator jsonSchema="schema2000.json" />
<validator jsonSchema="schemas/schema2000.json" />
</request>
<target host="localhost" port="2002" />
</api>

<!-- JSON Schema with schemaMappings for resolving $ref URNs -->
<api port="2001">
<request>
<validator jsonSchema="schema2001.json" />
<validator jsonSchema="schemas/schema2001.json">
<schemaMappings>
<schema id="urn:app:base_def" location="schemas/base.json"/>
<schema id="urn:app:meta_def" location="schemas/meta.json"/>
</schemaMappings>
</validator>
</request>
<target host="localhost" port="2002" />
</api>


<!-- Backend -->
<api port="2002">
<groovy>
Response.ok("&lt;response&gt;good request&lt;/response&gt;").build()
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading