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

validation not working (invalid data accepted) when a schema is defined using shell process substitution/named pipes #244

Open
DavidBiesack opened this issue Oct 16, 2024 · 0 comments

Comments

@DavidBiesack
Copy link

The bash shell has a feature called process substitution (details below) which allows creating named pipes.
ajv (CLI) does not appear to work well with such named pipes

$ ajv validate -s <(longer pipeline that yield a JSON or YAML schema) <(longer pipeline that yield a JSON or YAML data file)
/dev/fd/62 valid

this shows the data is valid, even when it does not satisfy the schema.

Rewritten in a way that should be equivalent from a bash script yields different ajv results:

$ (longer pipeline that yield a JSON or YAML schema) > schema.json
$ (longer pipeline that yield a JSON or YAML data file) > data.json
$ ajv validate -s schema.json -d data.json
data.json invalid
[
  {
    instancePath: '/info/breakingChanges',
    schemaPath: '#/properties/info/properties/breakingChanges/const',
    keyword: 'const',
    params: { allowedValue: true },
    message: 'must be equal to constant'
  }
]

Here's a simpler use case

Save the attached files, which unfortunately GitHub required me to rename with .txt extensions, so rename with

$ mv schema.yaml.txt schema.yaml
$ mv data.yaml.txt data.yaml

)

Use the shell and js-yaml to convert the files from YAML to JSON:

$ npm i -g ajv-cli js-yaml  # Install if you don't already have these commands
$ ajv --spec=draft2020 validate -s <(js-yaml schema.yaml) -d <(js-yaml data.yaml)
/dev/fd/62 valid

Even though data.yaml is invalid (as per schema.yaml), this command incorrectly shows the file as valid.

(here, /dev/fd/62 is the named pipe for the pipeline js-yaml data.yaml )

Running ajv directly

$ ajv --spec=draft2020 validate -s schema.yaml -d data.yaml
data.yaml invalid
[
  {
    instancePath: '/info/breakingChanges',
    schemaPath: '#/properties/info/properties/breakingChanges/const',
    keyword: 'const',
    params: { allowedValue: true },
    message: 'must be equal to constant'
  }
]

In situations where longer pipelines are used to derive the schema (i.e. text pipelines on YAML or other content ) and a schema.yaml or schema.json file is not directly available, I think ajv should work with named pipes

ajv validate -s <(longer pipeline that yield a JSON or YAML schema) <(longer pipeline that yield a JSON or YAML data file)

without having to resort to temp files that one must then clean up/manage etc.


From the bash(1) man page:

Process Substitution

   Process substitution allows a process's input or output to be
   referred to using a filename.  It takes the form of <(list) or
   >(list).  The process list is run asynchronously, and its input
   or output appears as a filename.  This filename is passed as an
   argument to the current command as the result of the expansion.
DavidBiesack added a commit to DavidBiesack/ajv-cli that referenced this issue Oct 21, 2024
Fixes issue ajv-validator#244

Default to parsing JSON if filename (a named fifo) is passed
that does not have a recognized extension - parse rather than
return `{}` for the schema, which accepts all input.

Added code to allow tsc to compile without error when accessing
`err.message` etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant