-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JSON content validation #38
Comments
I built something like this into rltest: package require tcltest
package require rltest
package require rl_json
namespace import rl_json::*
tcltest::test foo-1.1 {Assert some things about a JSON doc} -setup {
set now [clock seconds]
set date [clock format [expr {$now-45}] -format {%Y-%m-%d %H:%M:%S}]
} -body {
set doc [json template {
{
"foo": "123456",
"bar": "some exact value",
"greet": "hello, json",
"sub": [
{"lit": "?G:foo"},
{"when": "~S:date"}
]
}
}]
rltest compare_json $doc [json template {
{
"bar": "some exact value", // Require that the string matches exactly
"foo": "?R:^[0-9]+$", // Must match the regex
"greet": "?G:hello, *", // Must match the glob
"sub": [
{"lit": "?L:?G:foo"}, // Must exactly match "?G:foo", which would otherwise look like a glob match
{"when": "?D:within 30 seconds"} // Must contain a date recognisable to [clock scan] or ISO8601 like 2022-01-30T00:00:00Z that is within 30 seconds of the current time
]
}
}]
} -cleanup {
unset -nocomplain doc now date
} -result match
tcltest::cleanupTests Produces:
Things like optional whitespace and key ordering in objects are not considered a differences between the documents. To only assert certain things about a doc and ignore extra keys, missing keys, or both It would be easy enough to transplant it over to rl_json, but doing so would require managing some Tcl source in addition to the compiled code which slightly complicates things, and adding parse_args as a dependency. Also, I notice your rl_json branch adds jsonNRobj to fix the NR issue of the looping commands, that was just a bug in the normal commands which was fixed recently (NR support was added ages ago but some shuffle clearly broke it). |
Thanks for pointing me to this code. I'll check if/how I can use it for my needs. |
I'm starting using rl_json a lot and find it quiet useful for Tcl, as it allows typed string representation to Tcl easily and efficiently.
However, I have now a lot of code to validate content of JSON (array, object content, field type, ...).
Could an easy way to check / scan JSON be added?
Something similar to schema validation function in tDOM? Or based on JSON tempate?
The text was updated successfully, but these errors were encountered: