Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonschema function for jsonnet #380

Merged
merged 3 commits into from
Oct 4, 2019
Merged

Conversation

ramaro
Copy link
Member

@ramaro ramaro commented Oct 1, 2019

Introduces new jsonschema function in jsonnet.

It can be used to validate the inventory with jsonschema.

Given this 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/components/mysql/main.jsonnet:(19:1)-(43:2)

Compile error: failed to compile target: minikube-mysql

Partially fixes issue #346

@uberspot
Copy link
Contributor

uberspot commented Oct 1, 2019

Note: Add the example schema validation code in this PR to docs/compile.md for reference. :)
Other than that, lgtm!

@prakashrj
Copy link

prakashrj commented Oct 2, 2019

Will it support reuse of object definitions like explained here.
https://json-schema.org/understanding-json-schema/structuring.html

We have been using RAML for validations. If reuse is supported, then we will use this feature to validate.

@ramaro
Copy link
Member Author

ramaro commented Oct 2, 2019

@prakashrj not that the moment, but we can look into extending support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants