Skip to content

Conversation

@afrittoli
Copy link
Contributor

Add the ability to generate jsonschemas from the golang structs.
Generate jsonschemas for existing events, and use them to validate
test data in JSON format.

Signed-off-by: Andrea Frittoli [email protected]

Add the ability to generate jsonschemas from the golang structs.
Generate jsonschemas for existing events, and use them to validate
test data in JSON format.

Signed-off-by: Andrea Frittoli <[email protected]>
@afrittoli
Copy link
Contributor Author

This mostly works well, there is only one issue to solve on the validation side.

Right now when doing validate the type of CDEvent object does not match what the validator expects for some reason.

A trick is to marshal the CDEvent to json, unmarshal it back using a generic interface, and then the validator is happy. However with this approach missing fields are rendered as empty strings in the process of marshalling to json, so when it gets to validation they are not missing anymore (only empty) which means validation always passes.

afrittoli added a commit to afrittoli/cdevents-spec that referenced this pull request Aug 23, 2022
Add initial jsonschemas for some of the events:
- taskrun
- pipelinerun
- change

These json schemas are automatically generated through the
go types defined in the new cdevents/go-sdk, using the
library github.com/invopop/jsonschema, see
cdevents/sdk-go#6 for details.

Signed-off-by: Andrea Frittoli <[email protected]>
Producing a cloudevent or json does not require changing
the event, so using the reader interface instead.

Signed-off-by: Andrea Frittoli <[email protected]>
The omitempty struct tag is interpreted by the schema generation
library like a non-required field, so we cannot set "omitempty"
on required fields, else the schema will be invalid.

To solve the issue, use instead minLenght tags for all string
required fields, to indicate that a non-empty value must be
provided for validation to pass.

Signed-off-by: Andrea Frittoli <[email protected]>
@afrittoli afrittoli changed the title WIP - Add support for jsonschemas Add support for jsonschemas Aug 24, 2022
@afrittoli
Copy link
Contributor Author

Found a solution in using minLength jsonschema struct tags to enforce validation on empty string fields.

@afrittoli afrittoli merged commit acedf39 into cdevents:main Aug 24, 2022
afrittoli added a commit to afrittoli/cdevents-spec that referenced this pull request Aug 24, 2022
Add initial jsonschemas for some of the events:
- taskrun
- pipelinerun
- change

These json schemas are automatically generated through the
go types defined in the new cdevents/go-sdk, using the
library github.com/invopop/jsonschema, see
cdevents/sdk-go#6 for details.

Signed-off-by: Andrea Frittoli <[email protected]>
afrittoli added a commit to afrittoli/cdevents-spec that referenced this pull request Aug 25, 2022
Add initial jsonschemas for all of the events.

These json schemas are automatically generated through the
go types defined in the new cdevents/go-sdk, using the
library github.com/invopop/jsonschema, see
cdevents/sdk-go#6 for details.

Signed-off-by: Andrea Frittoli <[email protected]>
afrittoli added a commit to cdevents/spec that referenced this pull request Sep 6, 2022
Add initial jsonschemas for all of the events.

These json schemas are automatically generated through the
go types defined in the new cdevents/go-sdk, using the
library github.com/invopop/jsonschema, see
cdevents/sdk-go#6 for details.

Signed-off-by: Andrea Frittoli <[email protected]>
afrittoli added a commit to cdevents/spec that referenced this pull request Sep 13, 2022
* Add jsonschema for a few of the events

Add initial jsonschemas for all of the events.

These json schemas are automatically generated through the
go types defined in the new cdevents/go-sdk, using the
library github.com/invopop/jsonschema, see
cdevents/sdk-go#6 for details.

Signed-off-by: Andrea Frittoli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant