diff --git a/content/docs/howto/use-encoding-json-validate-as-a-field-validator/en.md b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/en.md new file mode 100644 index 000000000..63d09b90a --- /dev/null +++ b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/en.md @@ -0,0 +1,72 @@ +--- +title: Using "encoding/json.Validate" as a field validator +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/json.Validate`](https://pkg.go.dev/cuelang.org/go/pkg/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. + +{{{with code "en" "cc"}}} +#location top bottom + +! exec cue vet +cmp stderr out +-- file.cue -- +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 +} +-- out -- +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): + ./file.cue:14:7 + ./file.cue:5:7 + ./file.cue:15:7 + ./file.cue:18:6 + json.Validate:2:8 +{{{end}}} + +{{< info >}} +`encoding/json.Validate` validates JSON data *that is encoded in a string*. + +To validate data stored in a separate `.json` file, use CUE's native and +simpler unification instead.\ +This is documented in +[validating JSON with CUE]({{< relref "../validate-json-using-cue" >}}) +{{< /info >}} + +## Related content + +- {{< linkto/related/howto "use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators" >}} +- The [`encoding/json`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/json) + built-in package diff --git a/content/docs/howto/use-encoding-json-validate-as-a-field-validator/gen_cache.cue b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/gen_cache.cue new file mode 100644 index 000000000..aabdf82c6 --- /dev/null +++ b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-encoding-json-validate-as-a-field-validator": { + page: { + cache: { + code: { + cc: "Ctg5Ic9BX+2N0Bj+tUWQUofQm2WU/uTsWKd+5DmLJI8=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-encoding-json-validate-as-a-field-validator/page.cue b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/page.cue new file mode 100644 index 000000000..bb8174dfc --- /dev/null +++ b/content/docs/howto/use-encoding-json-validate-as-a-field-validator/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-encoding-json-validate-as-a-field-validator": {} diff --git a/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/en.md b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/en.md new file mode 100644 index 000000000..53937f5ec --- /dev/null +++ b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/en.md @@ -0,0 +1,95 @@ +--- +title: Using "encoding/yaml.Validate" and "encoding/yaml.ValidatePartial" as field validators +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in functions +[`encoding/yaml.Validate`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml#Validate) +and +[`encoding/yaml.ValidatePartial`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml#ValidatePartial) +as field validators. + +They assert that properly-formed YAML, encoded in a string, adheres to specific +constraints by checking that the data and schema unify successfully. +`encoding.yaml/Validate` also requires that all non-optional fields are +present. + +{{{with code "en" "cc"}}} +#location top bottom + +! exec cue vet +cmp stderr out +-- file.cue -- +package example + +import "encoding/yaml" + +data: """ + a: 1 + b: "two" + """ + +// Validate requires that all non-optional fields in a schema are present in +// data, and that data adheres to schema constraints. +data: yaml.Validate(_outOfBoundsSchema) +data: yaml.Validate(_missingFieldSchema) + +// ValidatePartial requires only that data adheres to schema constraints. +data: yaml.ValidatePartial(_outOfBoundsSchema) +data: yaml.ValidatePartial(_missingFieldSchema) + +_outOfBoundsSchema: { + a!: >99 // validation failure for both functions + b!: string +} +_missingFieldSchema: { + a!: int + b!: string + c!: bool // validation failure for yaml.Validate only +} +-- out -- +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.Validate({a!:>99,b!:string})): error in call to encoding/yaml.Validate: invalid value 1 (out of bound >99): + ./file.cue:12:7 + ./file.cue:5:7 + ./file.cue:13:7 + ./file.cue:16:7 + ./file.cue:17:7 + ./file.cue:20:6 + yaml.Validate:1:4 +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.Validate({a!:int,b!:string,c!:bool})): error in call to encoding/yaml.Validate: field is required but not present: + ./file.cue:13:7 + ./file.cue:5:7 + ./file.cue:12:7 + ./file.cue:16:7 + ./file.cue:17:7 + ./file.cue:26:2 +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.ValidatePartial({a!:>99,b!:string})): error in call to encoding/yaml.ValidatePartial: invalid value 1 (out of bound >99): + ./file.cue:16:7 + ./file.cue:5:7 + ./file.cue:12:7 + ./file.cue:13:7 + ./file.cue:17:7 + ./file.cue:20:6 + yaml.ValidatePartial:1:4 +{{{end}}} + +{{< info >}} +These functions validate YAML data *that is encoded in a string*. + +To validate data stored in a separate `.yaml` file, use CUE's native and +simpler unification instead.\ +This is documented in +[validating YAML with CUE]({{< relref "../validate-yaml-using-cue" >}}) +{{< /info >}} + +## Related content + +- {{< linkto/related/howto "use-encoding-json-validate-as-a-field-validator" >}} +- The [`encoding/yaml`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml) + built-in package diff --git a/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/gen_cache.cue b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/gen_cache.cue new file mode 100644 index 000000000..712af3a28 --- /dev/null +++ b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/gen_cache.cue @@ -0,0 +1,18 @@ +package site +{ + content: { + docs: { + howto: { + "use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators": { + page: { + cache: { + code: { + cc: "NwmV91mXtPzz8yX9M1F7AuxkKXbsTp/ostoF9i0KF+A=" + } + } + } + } + } + } + } +} diff --git a/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/page.cue b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/page.cue new file mode 100644 index 000000000..f58c9607c --- /dev/null +++ b/content/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/page.cue @@ -0,0 +1,3 @@ +package site + +content: docs: howto: "use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators": {} diff --git a/hugo/content/en/docs/howto/use-encoding-json-validate-as-a-field-validator/index.md b/hugo/content/en/docs/howto/use-encoding-json-validate-as-a-field-validator/index.md new file mode 100644 index 000000000..a834915b8 --- /dev/null +++ b/hugo/content/en/docs/howto/use-encoding-json-validate-as-a-field-validator/index.md @@ -0,0 +1,71 @@ +--- +title: Using "encoding/json.Validate" as a field validator +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in function +[`encoding/json.Validate`](https://pkg.go.dev/cuelang.org/go/pkg/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. + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top" >}} +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 +} +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} +$ 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): + ./file.cue:14:7 + ./file.cue:5:7 + ./file.cue:15:7 + ./file.cue:18:6 + json.Validate:2:8 +{{< /code-tab >}} +{{< /code-tabs >}} + +{{< info >}} +`encoding/json.Validate` validates JSON data *that is encoded in a string*. + +To validate data stored in a separate `.json` file, use CUE's native and +simpler unification instead.\ +This is documented in +[validating JSON with CUE]({{< relref "../validate-json-using-cue" >}}) +{{< /info >}} + +## Related content + +- {{< linkto/related/howto "use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators" >}} +- The [`encoding/json`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/json) + built-in package diff --git a/hugo/content/en/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/index.md b/hugo/content/en/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/index.md new file mode 100644 index 000000000..9a92f7a8c --- /dev/null +++ b/hugo/content/en/docs/howto/use-encoding-yaml-validate-encoding-yaml-validatepartial-as-field-validators/index.md @@ -0,0 +1,94 @@ +--- +title: Using "encoding/yaml.Validate" and "encoding/yaml.ValidatePartial" as field validators +tags: +- commented cue +- encodings +authors: +- jpluscplusm +toc_hide: true +--- + +This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}}) +demonstrates how to use the built-in functions +[`encoding/yaml.Validate`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml#Validate) +and +[`encoding/yaml.ValidatePartial`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml#ValidatePartial) +as field validators. + +They assert that properly-formed YAML, encoded in a string, adheres to specific +constraints by checking that the data and schema unify successfully. +`encoding.yaml/Validate` also requires that all non-optional fields are +present. + +{{< code-tabs >}} +{{< code-tab name="file.cue" language="cue" area="top" >}} +package example + +import "encoding/yaml" + +data: """ + a: 1 + b: "two" + """ + +// Validate requires that all non-optional fields in a schema are present in +// data, and that data adheres to schema constraints. +data: yaml.Validate(_outOfBoundsSchema) +data: yaml.Validate(_missingFieldSchema) + +// ValidatePartial requires only that data adheres to schema constraints. +data: yaml.ValidatePartial(_outOfBoundsSchema) +data: yaml.ValidatePartial(_missingFieldSchema) + +_outOfBoundsSchema: { + a!: >99 // validation failure for both functions + b!: string +} +_missingFieldSchema: { + a!: int + b!: string + c!: bool // validation failure for yaml.Validate only +} +{{< /code-tab >}} +{{< code-tab name="TERMINAL" language="" type="terminal" area="bottom" >}} +$ cue vet +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.Validate({a!:>99,b!:string})): error in call to encoding/yaml.Validate: invalid value 1 (out of bound >99): + ./file.cue:12:7 + ./file.cue:5:7 + ./file.cue:13:7 + ./file.cue:16:7 + ./file.cue:17:7 + ./file.cue:20:6 + yaml.Validate:1:4 +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.Validate({a!:int,b!:string,c!:bool})): error in call to encoding/yaml.Validate: field is required but not present: + ./file.cue:13:7 + ./file.cue:5:7 + ./file.cue:12:7 + ./file.cue:16:7 + ./file.cue:17:7 + ./file.cue:26:2 +data: invalid value "a: 1\nb: \"two\"" (does not satisfy encoding/yaml.ValidatePartial({a!:>99,b!:string})): error in call to encoding/yaml.ValidatePartial: invalid value 1 (out of bound >99): + ./file.cue:16:7 + ./file.cue:5:7 + ./file.cue:12:7 + ./file.cue:13:7 + ./file.cue:17:7 + ./file.cue:20:6 + yaml.ValidatePartial:1:4 +{{< /code-tab >}} +{{< /code-tabs >}} + +{{< info >}} +These functions validate YAML data *that is encoded in a string*. + +To validate data stored in a separate `.yaml` file, use CUE's native and +simpler unification instead.\ +This is documented in +[validating YAML with CUE]({{< relref "../validate-yaml-using-cue" >}}) +{{< /info >}} + +## Related content + +- {{< linkto/related/howto "use-encoding-json-validate-as-a-field-validator" >}} +- The [`encoding/yaml`](https://pkg.go.dev/cuelang.org/go/pkg/encoding/yaml) + built-in package