luckyframework / lucky

A full-featured Crystal web framework that catches bugs for you, runs incredibly fast, and helps you write code that lasts.
https://luckyframework.org
MIT License
2.59k stars 156 forks source link

Add some sort of `with_keys` option to `send_json` #1846

Open jwoertink opened 10 months ago

jwoertink commented 10 months ago

I don't know exactly what this looks like yet, but I have a ton of really large custom JSON output in my actions that are too complex to say response.should send_json(200, messages: [{.......}]). Really I don't care that the value looks exactly a specific way, I just want to ensure that there is a value from that key. So really I want something kind of like..

response.should send_json(200, with_keys: [:messages, ....])

or maybe comparing a type somehow?

response.should send_json(200, with: {messages: Array(MessageSerializer)})

definitely a half-baked idea :joy:

https://github.com/luckyframework/lucky/blob/8eef897212ebbcbb6f0735ae3d8c3381f44b2449/src/lucky/request_expectations.cr#L12

jwoertink commented 10 months ago

Thinking about this more... Maybe we can just do deep nested checks? For example, I want to know if the response has a key with a value that's a massive object, but I only need to verify that one of the key/values in that massive object exist...

# {user: {username: "...", slug: "...", created_at: ..., updated_at: ...., preferences: {theme: "dark", size: "large"}}}
response.should send_json(200, user: {username: "...", preferences: {theme: "dark"}})

This would then let me just do a quick few spot checks on an object instead of trying to worry about the exact time on created_at, or building out each object in a sub array and so on.