Maybe I'm in the minority but I don't want light-weight at the expense of compatible/standard. It's being recognized that validation is more important than we'd expected - the recent ACM guidelines for moderation on Postel's Law really resonated with my experiences with RESTful/JSON micro-services (https://queue.acm.org/detail.cfm?id=1999945). But if we're going to the effort of validating, let's do it in a language/framework agnostic way ... jsonschema.org is mentioned in other comments but I'm a fan of the OpenAPI initiative (https://www.openapis.org/). Then we can all share the validation rules!
This has already been said in the nested comments, but why not “?” for optional? Coming from Typescript, Swift, etc “!” feels more like an assertion of non-nullity (is that a word?). It’s also easy to read as “not <key>”.
Exclamation mark for optional is just weird. Question mark would be a lot more intuitive, unless there’s an origin story here I’m not aware of.
It's crazy the amount of work that is spent to essentially implement static typing into dynamically / duck typed languages.
Projects like this, mypy and others makes the whole thing bizarre to say the least.
Why are schemas strings?
It would seem much more natural to make them JSON structures since they're almost that anyway.
JSON schema and ajv are my go to tools for this: https://github.com/epoberezkin/ajv
I built something similar for my latest webapp to validate json-requests in python, the syntax is like this:
{
"content": Required(str),
"username": Required(str, validate=username_is_ok, transform=lambda x: x.lower(),
fail_message="Username isn't valid"),
"message": Optional(str),
"some_list": Required({
"name": Required(str),
"date": Required(str)
}, is_list=True)
}
Then I can provide this in a hook to the request method.This is OK for simple structures, but in a deeply nested structures and larger APIs you'd probably also need some equivalent of json-schema $refs and ability to reuse blocks in your api schema.
You shouldn't validate json. You should parse it, e.g. transform it into your desired data structure if it comes in in a valid shape.
https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...
I really like Elms way of Json decoding for this.
Karate[1] has its own dsl for describing both json and xml. The output is short, but not very readable. For people new to the project it's rather cryptic. It is easy to maintain though once your team becomes fluent in Karate.
You can also use fefe (https://github.com/paperhive/fefe/) to validate any data with its pure functional and minimalist approach. Bonus: it's 100% type-safe automatically if you use Typescript.
i saw this and was thinking “hrm...this is the sorts thing i use lodash for...if i even need it”.
and now i see lodash is a dependency.
Enter https://cuelang.org
Wow this is very clean!
There has been a schema specification for json already. it's called conveniently `json-schema` https://json-schema.org/
Cool another syntax to learn! -nobody
Very neat. I had a similar need recently which led to a similar solution[^1]. Notably, I used a syntax nearly identical to yours, albeit without the very handy pluggable validators yours has. Nice job.
What led you to choose “!” as the “optional” modifier? My intuition would have guessed that character to have the opposite meaning.
[1]: project: https://github.com/tylerchr/jstn/ — playground: https://tylerchr.github.io/jstn/