Skip to content

Commit b2d505d

Browse files
Simplify jsonschema example (#2504)
* simplify json schema example * adjust example test * adjust example test * fix --------- Co-authored-by: Thomas Bayer <bayer@predic8.de>
1 parent aed2ea8 commit b2d505d

22 files changed

Lines changed: 89 additions & 482 deletions

File tree

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,32 @@
1-
# Validation - JSON Schema
1+
# Validation - JSON Schema (simple + schemaMappings)
22

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

5-
6-
## Running the Example
7+
## Running the example
78

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

10-
2. Start `membrane.cmd` or `membrane.sh`.
11-
12-
3. Look at `schema2000.json` and compare the schema to `good2000.json` and `bad2000.json`.
13-
14-
4. Run `curl -d @good2000.json http://localhost:2000/` on the console. Observe that you get a successful response.
15-
16-
5. Run `curl -d @bad2000.json http://localhost:2000/`. Observe that you get a validation error response.
17-
18-
Keeping the router running, you can try a more complex schema.
19-
20-
1. Have a look at `schema2001.json`, `good2001.json` and `bad2001.json`.
21-
22-
2. Run `curl -d @good2001.json http://localhost:2001/`. Observe that you get a successful response.
12+
Simple validation:
13+
- Inspect `schemas/schema2000.json`, then compare with `good2000.json` and `bad2000.json`.
14+
- `curl -H "Content-Type: application/json" -d @good2000.json http://localhost:2000`
15+
- `curl -H "Content-Type: application/json" -d @bad2000.json http://localhost:2000`
2316

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

2623
## How it is done
2724

28-
Let's examine the `proxies.xml` file.
29-
30-
```xml
31-
<router>
32-
<api port="2000">
33-
<request>
34-
<validator jsonSchema="schema2000.json" />
35-
</request>
36-
<target host="localhost" port="2002" />
37-
</api>
38-
39-
<api port="2001">
40-
<request>
41-
<validator jsonSchema="schema2001.json" />
42-
</request>
43-
<target host="localhost" port="2002" />
44-
</api>
45-
46-
<api port="2002">
47-
<groovy>
48-
Response.ok("&lt;response&gt;good request&lt;/response&gt;").build()
49-
</groovy>
50-
</api>
51-
</router>
52-
```
53-
54-
We define three `<api>` components, running on ports 2000-2002.
55-
The first two validate all requests using the JSON schema defined in the `<validator />` component's `jsonSchema` attribute.
56-
The successfully validated requests then get sent to the third `<api>` component, where we simply return a 200 "Ok" response.
25+
Take a look at the `apis.yaml`. It includes two APIs for validation and one backend:
26+
- Port 2000 validates requests against a standalone schema `schemas/schema2000.json`.
27+
- 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.
5728

5829
---
59-
See:
60-
- [JSON Schema](https://json-schema.org/) documentation
61-
- [validator](https://membrane-api.io/docs/current/validator.html) reference
30+
See:
31+
- JSON Schema: https://json-schema.org/
32+
- Membrane validator reference: https://membrane-api.io/docs/current/validator.html

distribution/examples/validation/json-schema/apis.yaml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# yaml-language-server: $schema=https://www.membrane-api.io/v7.0.5.json
2+
# Simple JSON Schema validation example
23
api:
34
port: 2000
45
flow:
56
- request:
67
- validator:
7-
jsonSchema: schema2000.json
8+
jsonSchema: schemas/schema2000.json
89
target:
910
host: localhost
1011
port: 2002
1112

1213
---
13-
14+
# JSON Schema with schemaMappings for resolving $ref URNs
1415
api:
1516
port: 2001
1617
flow:
1718
- request:
1819
- validator:
19-
jsonSchema: schema2001.json
20+
jsonSchema: schemas/schema2001.json
21+
schemaMappings:
22+
schemas:
23+
- schema:
24+
id: urn:app:base_def
25+
location: schemas/base.json
26+
- schema:
27+
id: urn:app:meta_def
28+
location: schemas/meta.json
2029
target:
2130
host: localhost
2231
port: 2002
2332

2433
---
25-
34+
# Backend
2635
api:
2736
port: 2002
2837
flow:
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
2-
"id": "93241279A",
3-
"name": "CITROEN Berlingo HDi 90",
4-
"price": { "incl_vat": 7972.00 },
5-
"tags": "diesel",
6-
"weight": 131,
7-
"transmission": "5 gears"
8-
}
2+
"params": [
3+
{
4+
"name": "mode",
5+
"value": "fast"
6+
}
7+
],
8+
"meta": {
9+
"source": "",
10+
"unexpected": 123
11+
}
12+
}
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{
2-
"id": 106825605,
3-
"name": "FORD Fusion 1.25 Style",
4-
"price": 8990.00,
5-
"tags": ["black", "metallic", "gasoline" ],
6-
"weight": 1145,
7-
"co2": 140,
8-
"transmission":"manual"
9-
}
2+
"params": [
3+
{
4+
"name": "mode",
5+
"value": "fast"
6+
},
7+
{
8+
"name": "retries",
9+
"value": 3
10+
}
11+
],
12+
"meta": {
13+
"source": "curl",
14+
"requestId": "REQ-12345678"
15+
}
16+
}

distribution/examples/validation/json-schema/proxies.xml renamed to distribution/examples/validation/json-schema/proxies-old.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@
66

77
<router>
88

9+
<!-- Simple JSON Schema validation example -->
910
<api port="2000">
1011
<request>
11-
<validator jsonSchema="schema2000.json" />
12+
<validator jsonSchema="schemas/schema2000.json" />
1213
</request>
1314
<target host="localhost" port="2002" />
1415
</api>
1516

17+
<!-- JSON Schema with schemaMappings for resolving $ref URNs -->
1618
<api port="2001">
1719
<request>
18-
<validator jsonSchema="schema2001.json" />
20+
<validator jsonSchema="schemas/schema2001.json">
21+
<schemaMappings>
22+
<schema id="urn:app:base_def" location="schemas/base.json"/>
23+
<schema id="urn:app:meta_def" location="schemas/meta.json"/>
24+
</schemaMappings>
25+
</validator>
1926
</request>
2027
<target host="localhost" port="2002" />
2128
</api>
22-
29+
30+
<!-- Backend -->
2331
<api port="2002">
2432
<groovy>
2533
Response.ok("&lt;response&gt;good request&lt;/response&gt;").build()

distribution/examples/validation/json-schema/schema-mappings/README.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

distribution/examples/validation/json-schema/schema-mappings/bad2000.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

distribution/examples/validation/json-schema/schema-mappings/bad2001.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

distribution/examples/validation/json-schema/schema-mappings/good2000.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

distribution/examples/validation/json-schema/schema-mappings/good2001.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)