Skip to content
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

PR: allow JSON array as input #365

Merged
merged 2 commits into from
Feb 24, 2021
Merged

PR: allow JSON array as input #365

merged 2 commits into from
Feb 24, 2021

Conversation

qistoph
Copy link
Contributor

@qistoph qistoph commented Feb 4, 2021

As described in #363, node-jq currently does not allow JSON arrays as input.

This PR:

  • adds test cases to indicate and test the issue
  • fixes the input validation to allow arrays
  • possibly breaking side affect: changes the undefined JSON input error slightly
-  'node-jq: invalid json object argument supplied: "undefined"'
+  'node-jq: invalid json argument supplied: "undefined"'

It's worth considering allow even more input, since any JSON object is effectively allowed by jq.

$ echo '1' | jq .
1
$ echo '9.99' | jq .
9.99
$ echo '"asdf"' | jq .
"asdf"
$ echo '["asdf","test"]' | jq .
[
  "asdf",
  "test"
]
$ echo 'null' | jq .
null

The only issue I can think of right now is with undefined:

$ echo 'undefined' | jq .                                                   
parse error: Invalid numeric literal at line 2, column 0                      

@@ -48,7 +48,10 @@ export const preSpawnSchema = Joi.object({
otherwise: path
}).label('path')
},
json: (schema) => Joi.object().allow('', null).required().label('json object'),
json: (schema) => Joi.alternatives().try(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure If I understand this change correctly. Isn't there any Joi.json instead?
I thought that Joi.object() would treat as JS Object so, same as typeof [] === 'object'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that doesn't seem to be the case. If I send an array as input I get an error
ValidationError: "json object" must be of type object

You can checkout my test cases (99f8ae6) to see what I'm trying to do.

Brief example:

const jq = require('node-jq')

obj = ["asdf","test"]

jq.run(".", JSON.stringify(obj), {'input': 'json'})
.then( out => {
    console.log("out:", out)
})
.catch( err => {
    console.error(err.message)
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, totally get the why. I even wonder why do we even need Joi for the input tbh.

But this PR looks good.

@davesnx
Copy link
Member

davesnx commented Feb 23, 2021

I would add a breaking change since it does change the API surface. Maybe doable thought the commit, let me know if you need help with this, otherwise, I'm merging it.

@qistoph
Copy link
Contributor Author

qistoph commented Feb 24, 2021

  • Added 'BREAKING CHANGE' to the commit message.
  • Rebased onto current master

@davesnx davesnx merged commit 8de306b into sanack:master Feb 24, 2021
mackermans added a commit that referenced this pull request Jul 6, 2021
BREAKING CHANGE: allow json array as input
@mackermans
Copy link
Collaborator

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants