26
loading...
This website collects cookies to deliver better user experience
a model that embraces decentralization and domain self-sovereignty, interoperability through global standardization, a dynamic topology and most importantly automated execution of decisions by the platform.
message: #Identity & {
first: "John"
Last: "Doe"
Age: 40
}
#Identity: {
// first name of the person
first: =~ "[A-Z].*"
// Last name of the person
Last: =~ "[A-Z].*"
// Age of the person
Age?: number & < 130
}
cue vet identity
will validate the data against their definition; you can play with the example directly in the Cue playground. If you don't have the tooling locally)Doe
to doe
or setting an age above 130 would result in an error.|
) symbol:// Send:
collect_data | marshal_message | emit_message | validate_message | marshal_event | send_to_channel
// Receive:
filter_events_from_channel | read_from_channel | unmarshal_event | unmarshal_data | profit
cue vet
we’ve issued before does under the hood. But we may want to turn it into a service for ease of testing and robustness. This simple code snippet shows the power of the SDK: fewer than ten lines are required to validate data against a schema (including the functional constraints).type DataProduct struct {
definition cue.Value
// ...
}
func (d *DataProduct) ExtractData(b []byte) (cue.Value, error) {
data := d.definition.Context().CompileBytes(b)
unified := d.definition.Unify(data)
opts := []cue.Option{
cue.Attributes(true),
cue.Definitions(true),
cue.Hidden(true),
}
return data, unified.Validate(opts...)
}
{
"specversion": "1.0",
"id": "1234-4567-8910-1234-5678",
"source": "MySource",
"type": "MySource:newPerson",
"datacontenttype": "application/json",
"data_base64": "MyMessageInJSONEncodedInBase64=="
}
❯ ( cat << EOF
#Identity: {
// first name of the person
first: =~ "[A-Z].*"
// Last name of the person
Last: =~ "[A-Z].*"
// Age of the person
Age?: number & < 130
}
EOF
) | cue export --out=openapi -
{
"openapi": "3.0.0",
"info": {
"title": "Generated by cue.",
"version": "no version"
},
"paths": {},
"components": {
"schemas": {
"Identity": {
"type": "object",
"required": [
"first",
"Last"
],
"properties": {
"first": {
"description": "first name of the person",
"type": "string",
"pattern": "[A-Z].*"
},
"Last": {
"description": "Last name of the person",
"type": "string",
"pattern": "[A-Z].*"
},
"Age": {
"description": "Age of the person",
"type": "number",
"maximum": 130,
"exclusiveMaximum": true
}
}
}
}
}
}
#Identity: {
// first name of the person
first: =~ "[A-Z].*"
// Last name of the person
Last: =~ "[A-Z].*"
// Age of the person
Age?: number & < 130
}
curl http://localhost:8181/openapi
{
"openapi": "3.0.0",
"info": {
"title": "Generated by cue.",
"version": "no version"
},
"paths": {},
"components": {
"schemas": {
...
}
❯ curl -XPOST -d'{"first": "John","Last": "Doe","Age": 40}' http://localhost:8181/
ok
❯ curl -XPOST -d'{"first": "John","Last": "Doe","Age": 140}' http://localhost:8181/
#Identity.Age: invalid value 140 (out of bound <130)
❯ curl -XPOST -d'{"first": "John","Last": "Doe","Age": 40}' http://localhost:8181/
sent to the channel ok
> go test -run=NONE -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: owulveryck.github.io/test1
cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
BenchmarkRun-12 1024 2135823 ns/op 55261 B/op 537 allocs/op
PASS
ok owulveryck.github.io/test1 2.503s