Skip to content

How to validate an ad-hoc go value? #1752

Answered by myitcv
kstenerud asked this question in General
Discussion options

You must be logged in to vote

You were very close! I've presented your code as main1.go below and the fixed versions as main2.go.

The CompileString() call you had returned a struct value that has a field named #schema. You you were unifying and validating your encoded value against that struct value, which is open. Hence it passed. Instead, you need to choose the #schema value, and unify and validate against that, which is what I do in main2.go. You will also notice that I have ensured the field names match, using the json field tag.

go mod tidy

# This should fail but doesn't...
go run main1.go

# This does fail as expected
go run main2.go
stdout 'conflicting values string and 1 \(mismatched types string and int\)'

…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by kstenerud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants