Skip to content

Commit

Permalink
add jsonschema example in docs/compile.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ramaro committed Oct 4, 2019
1 parent 3fbda30 commit 8eae9d4
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ gzip_b64 - returns base64 encoded gzip of obj
inventory - returns a dictionary with the inventory for target
jsonschema - validates obj with schema, returns object with 'valid' and 'reason' keys
```
##### Jsonschema validation from jsonnet

Given the follow example inventory:

```
mysql:
storage: 10G
storage_class: standard
image: mysql:latest
```

The yaml inventory structure can be validated with the new `jsonschema()` function:

```
local schema = {
type: "object",
properties: {
storage: { type: "string", pattern: "^[0-9]+[MGT]{1}$"},
image: { type: "string" },
}
};
// run jsonschema validation
local validation = kap.jsonschema(inv.parameters.mysql, schema);
// assert valid, otherwise error with validation.reason
assert validation.valid: validation.reason;
```

If `validation.valid` is not true, it will then fail compilation and display `validation.reason`.

For example, if defining the `storage` value with an invalid pattern (`10Z`), compile fails:

```
Jsonnet error: failed to compile /code/components/mysql/main.jsonnet:
RUNTIME ERROR: '10Z' does not match '^[0-9]+[MGT]{1}$'
Failed validating 'pattern' in schema['properties']['storage']:
{'pattern': '^[0-9]+[MGT]{1}$', 'type': 'string'}
On instance['storage']:
'10Z'
/code/mysql/main.jsonnet:(19:1)-(43:2)
Compile error: failed to compile target: minikube-mysql
```

##### Jinja2 jsonnet templating

Expand Down

0 comments on commit 8eae9d4

Please sign in to comment.