Parsing Results Examples
Under the hood Zog follows the Parsing Execution Structure and does a bunch of things under the hood to make sure your data is parsed correctly. Such as checking for zero values, coercing types, etc...
Some of this might not be obvious so here is a summary table that breaksdown expected results of calling schema.Parse() on various inputs:
Schema Type | Data | Dest | Required Error (Zero Value) | Coercion Error |
---|---|---|---|---|
Bool() | true | true | no | no |
Bool() | false | false | no | no |
Bool() | nil | false | yes | yes |
Bool() | "" | false | yes | yes |
Bool() | " " | false | yes | yes |
Bool() | on | true | no | no |
Bool() | off | false | no | no |
Bool() | ["true", "t", "T", "True", "TRUE"] | true | no | no |
Bool() | ["false", "f", "F", "FALSE", "False"] | false | no | no |
Bool() | test | false | no | yes |
Bool() | 1 | true | no | no |
Bool() | 0 | false | no | no |
Bool() | 123 | false | no | yes |
String() | "" | "" | yes | no |
String() | " " | "" | yes | no |
String() | nil | "" | yes | yes |
String() | any value | fmt.Sprintf("%v", value) | no | no |
Int() | 0 | 0 | no | no |
Int() | 10 | 10 | no | no |
Int() | nil | 0 | yes | yes |
Int() | "" | 0 | yes | yes |
Int() | " " | 0 | yes | yes |
Int() | any string | strconv.Atoi(str) | no | depends |
Int() | 6.29 | 6 | no | no |
Int() | true | 1 | no | no |
Int() | false | 0 | no | no |
Float() | 1.21 | 1.21 | no | no |
Float() | 0 | 0 | no | no |
Float() | nil | 0 | yes | yes |
Float() | "" | 0 | yes | yes |
Float() | " " | 0 | yes | yes |
Float() | any string | strconv.ParseFloat(str) | no | depends |
Float() | 1 | 1 | no | no |
Time() | time.Time{} | time.Time{} | no | no |
Time() | time.Now() | time.Now() | no | no |
Time() | nil | time.Time{} | yes | yes |
Time() | "" | time.Time{} | yes | yes |
Time() | " " | time.Time{} | yes | yes |
Time() | unix_timestamp_ms | time.Unix(unix, 0) | no | no |
Time() | any string | time.Parse(format, str) | no | depends |
Slice() | [1] | [1] | no | no |
Slice() | [] | [] | no | no |
Slice() | nil | [null] | yes | no (error will show in the appropriate schema if any) |
Slice() | "" | [""] | yes | no (error will show in the appropriate schema if any) |
Slice() | " " | [" "] | yes | no (error will show in the appropriate schema if any) |
Slice() | any_value | [value] | depends | no (error will show in the appropriate schema if any) |