ajv-cli icon indicating copy to clipboard operation
ajv-cli copied to clipboard

Accept data on stdin

Open handrews opened this issue 8 years ago • 11 comments

The most common way I've noticed many people do casual testing of API responses is with curl. In particular some back-end devs really prefer the command line, which makes ajv-cli great for this purpose. Would it be possible for ajv-cli to take the data JSON from stdin so that

$ curl -X PUT "https://api.example.com/foos/506e3185e9c882d175a2d0cb0093d9f2" \
       -H "Content-Type: application/json" \
       --data '{"id":"506e3185e9c882d175a2d0cb0093d9f2","bar":"1234"}' \
       | ajv validate -s foos-response.json

works? And likewise for ajv test. This of course is more to allow people to poke at things with minimal setup than something intended for a large scale test automation / CI system.

handrews avatar Apr 30 '17 19:04 handrews

I like this idea, PR is welcome ;)

epoberezkin avatar May 03 '17 10:05 epoberezkin

I'll look into it :)

handrews avatar May 03 '17 10:05 handrews

@epoberezkin getting back to this now. There are several npm packages for working with stdin.

From least to most complex:

  • https://www.npmjs.com/package/get-stdin (stdin reading with promises)
  • https://www.npmjs.com/package/file-or-stdin (reads file, or if file is a false-y value, reads stdin)
  • https://www.npmjs.com/package/read-input (full-featured multi-file and stdin reading)

Do you have a preference? The middle one seems to have a nice level of functionality, but I do not have strong opinions on this (or a thorough knowledge of any node-specific design choices involved here).

handrews avatar May 17 '17 22:05 handrews

@handrews Simple get-stdin looks the best. Quite some refactoring here to make ajv-cli async. But there is no way around it...

epoberezkin avatar May 17 '17 23:05 epoberezkin

By the way, you should already be able to do something like:

ajv -s schema -d /dev/stdin

to consume data from stdin. Some caveats here though: http://stackoverflow.com/a/5794483/1816503

epoberezkin avatar May 17 '17 23:05 epoberezkin

also see https://unix.stackexchange.com/questions/16990/using-data-read-from-a-pipe-instead-than-from-a-file-in-command-options

epoberezkin avatar May 17 '17 23:05 epoberezkin

@epoberezkin thanks! I'll look through all of this. I'm on-board with using get-stdin.

handrews avatar May 17 '17 23:05 handrews

@epoberezkin use bash process substitution; it connects the stdout from the subcommand to a fifo or named pipe and substitutes in that path. e.g.

$ ajv -d <(echo null) -s <(echo {}) 
/dev/fd/63 valid

see also http://tldp.org/LDP/abs/html/process-sub.html

n1ywb avatar Jul 28 '17 03:07 n1ywb

Ran into this today; ended up just writing my schema and data to files first and then using ajv with those explicit filenames. This would definitely be a nice feature to have!

@n1ywb that would be great if it worked, but for some reason it doesn't seem to:

$ ajv -d <(echo null) -s <(echo {})
error:  ENOENT: no such file or directory, open '/proc/4839/fd/pipe:[60837]'

At first I thought the issue was because I had installed Node as a snap, but it persisted even after I uninstalled Node and reinstalled it from apt via NodeSource. Seems to be related to these two Node issues? The same thing happens when using pipes and /dev/stdin instead of process substitution:

$ echo {} > schema.json
$ echo null | ajv -d /dev/stdin -s schema.json
error:  ENOENT: no such file or directory, open '/proc/5717/fd/pipe:[70865]'

samestep avatar Jun 23 '20 16:06 samestep

Not exactly what's asked here, but related: v4 added a way to write compiled validation code to stdout (e.g. for formatting)

epoberezkin avatar Dec 20 '20 09:12 epoberezkin

Not sure this adds anything, but here's my variation. In theory, these calls should have the same result, but they don't.

> ajv -s audits-spec.schema.json -d audits.json --errors=text
audits.json invalid
data/accounts should be string
> ajv -s audits-spec.schema.json -d <(cat audits.json) --errors=text
/dev/fd/63 valid

zanerock avatar Jan 29 '21 22:01 zanerock