Skip to content

Commit

Permalink
howto/validate-yaml-using-cue: flip to use preprocessor
Browse files Browse the repository at this point in the history
No other changes expected.

Signed-off-by: Paul Jolly <[email protected]>
Change-Id: I9a8538f1dd30c5b971233e1991aef583ddc5fd55
Dispatch-Trailer: {"type":"trybot","CL":1169621,"patchset":1,"ref":"refs/changes/21/1169621/1","targetBranch":"alpha"}
  • Loading branch information
myitcv authored and cueckoo committed Sep 19, 2023
1 parent 97ac457 commit 2dbbe51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
50 changes: 25 additions & 25 deletions content/docs/howto/validate-yaml-using-cue/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ allows you to run `cue` commands

Create a YAML file called `x.yaml` with the following:

``` {title="x.yaml"}
{{{with upload "en" "initial x.yaml"}}}
-- x.yaml --
people:
Gopher:
name: Gopher
Expand All @@ -39,36 +40,38 @@ people:
name: Ken
age: 21
address: The Blue Sky
```
{{{end}}}

Create a CUE file named `x.cue`

The following CUE creates a CUE definition that describes the data type
constraints for every person.

``` {title="x.cue"}
{{{with upload "en" "initial x.cue"}}}
-- x.cue --
#Person: {
name: string
age: int
address: string
name: string
age: int
address: string
}

people: [X=string]: #Person & {
name: X
name: X
}
```
{{{end}}}

Run the following `cue` command in your:

```
$ cue vet x.cue x.yaml
```
{{{with script "en" "initial vet"}}}
cue vet x.cue x.yaml
{{{end}}}

_NOTE: `cue vet` is silent when run successfully. Output will only show on error._

Add another person to your YAML data.

``` {title="x.yaml"}
{{{with upload "en" "another person"}}}
-- x.yaml --
people:
Gopher:
name: Gopher
Expand All @@ -82,24 +85,21 @@ people:
name: Rob
age: 42.2
address: CUEtopia
```
{{{end}}}

Validate again with `cue vet`

```console
$ cue vet x.cue x.yaml
people.Rob.age: conflicting values 42.2 and int (mismatched types float and int):
./x.cue:3:11
./x.cue:7:21
./x.yaml:13:11
```
{{{with script "en" "broken vet"}}}
! cue vet x.cue x.yaml
{{{end}}}

The command output shows validation errors where the YAML violates
the (type) constraints that you have declared.

Fix up the YAML

``` {title="x.yaml"}
{{{with upload "en" "fixed yaml"}}}
-- x.yaml --
people:
Gopher:
name: Gopher
Expand All @@ -113,13 +113,13 @@ people:
name: Rob
age: 42
address: CUEtopia
```
{{{end}}}

Validate with `cue vet` again

```
$ cue vet x.cue x.yaml
```
{{{with script "en" "fixed vet"}}}
cue vet x.cue x.yaml
{{{end}}}

The `cue vet` command will show no output on success.

Expand Down
17 changes: 17 additions & 0 deletions content/docs/howto/validate-yaml-using-cue/gen_cache.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package site
{
docs: {
howto: {
"validate-yaml-using-cue": {
cache: {
upload: {
"initial x.yaml": "WxVUfTfY24GA6ppglaMixgQzcc2XdcZvbMsNSMNU1ik="
"initial x.cue": "6hII6qs3ijcSfjIMCKZAWZAkLccZrVZbQdPfannwCU4="
"another person": "awM0G/zc1cXOgrMdN6qTtLRqgE+Vl3Hh3ADtS31cx1k="
"fixed yaml": "qslpo4m41YEjFbeYZ9MBK2cN8tPPUh4dqc+Ll6WE7TI="
}
}
}
}
}
}
24 changes: 12 additions & 12 deletions hugo/content/en/docs/howto/validate-yaml-using-cue/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ allows you to run `cue` commands

Create a YAML file called `x.yaml` with the following:

``` {title="x.yaml"}
```yaml { title="x.yaml" }
people:
Gopher:
name: Gopher
Expand All @@ -46,29 +46,29 @@ Create a CUE file named `x.cue`
The following CUE creates a CUE definition that describes the data type
constraints for every person.

``` {title="x.cue"}
```cue { title="x.cue" }
#Person: {
name: string
age: int
address: string
name: string
age: int
address: string
}
people: [X=string]: #Person & {
name: X
name: X
}
```

Run the following `cue` command in your:

```
```text { title="TERMINAL" codeToCopy="Y3VlIHZldCB4LmN1ZSB4LnlhbWwK" }
$ cue vet x.cue x.yaml
```

_NOTE: `cue vet` is silent when run successfully. Output will only show on error._

Add another person to your YAML data.

``` {title="x.yaml"}
```yaml { title="x.yaml" }
people:
Gopher:
name: Gopher
Expand All @@ -86,20 +86,20 @@ people:

Validate again with `cue vet`

```console
```text { title="TERMINAL" codeToCopy="Y3VlIHZldCB4LmN1ZSB4LnlhbWwK" }
$ cue vet x.cue x.yaml
people.Rob.age: conflicting values 42.2 and int (mismatched types float and int):
./x.cue:3:11
./x.cue:7:21
./x.yaml:13:11
./x.yaml:12:11
```

The command output shows validation errors where the YAML violates
the (type) constraints that you have declared.

Fix up the YAML

``` {title="x.yaml"}
```yaml { title="x.yaml" }
people:
Gopher:
name: Gopher
Expand All @@ -117,7 +117,7 @@ people:

Validate with `cue vet` again

```
```text { title="TERMINAL" codeToCopy="Y3VlIHZldCB4LmN1ZSB4LnlhbWwK" }
$ cue vet x.cue x.yaml
```

Expand Down

0 comments on commit 2dbbe51

Please sign in to comment.