This Commented CUE demonstrates how to use the built-in function encoding/json.Validate as a field validator.

It asserts that properly-formed JSON, encoded in a string, adheres to specific constraints by checking that the data and schema unify successfully.

package example

import "encoding/json"

data: """
	  "a": 1,
	  "b": "two"

// Validate requires only that data adheres to schema constraints.
// Missing fields do not cause validation failures.
data: json.Validate(_outOfBoundsSchema)
data: json.Validate(_missingFieldSchema)

_outOfBoundsSchema: {
	a!: >99 // validation failure
	b!: string
_missingFieldSchema: {
	a!: int
	b!: string
	c!: bool // NOT a validation failure
$ cue vet
data: invalid value "{\n  \"a\": 1,\n  \"b\": \"two\"\n}" (does not satisfy encoding/json.Validate({a!:>99,b!:string})): error in call to encoding/json.Validate: invalid value 1 (out of bound >99):