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

strictSchema: "log" throws on some schema compile errors #2538

Open
chris48s opened this issue Mar 7, 2025 · 1 comment
Open

strictSchema: "log" throws on some schema compile errors #2538

chris48s opened this issue Mar 7, 2025 · 1 comment

Comments

@chris48s
Copy link

chris48s commented Mar 7, 2025

Quick description of the issue

In

function unknownFormat(): void {
if (opts.strictSchema === false) {
self.logger.warn(unknownMsg())
return
}
throw new Error(unknownMsg())

strictSchema === false logs an error, whereas strictSchema === "log" throws

Based on the docs in

ajv/docs/options.md

Lines 96 to 104 in 82735a1

### strictSchema
Prevent unknown keywords, formats etc. (see [Strict schema](./strict-mode.md#strict-schema))
Option values:
- `true` (default) - throw an exception when any strict schema restriction is violated.
- `"log"` - log warning when any strict schema restriction is violated.
- `false` - ignore all strict schema violations.

I would expect strictSchema === "log" to log and strictSchema === true to throw.

Issue template

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.17.1
Yes

Ajv options object

{ strictSchema: "log" }

JSON Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://json.schemastore.org/ruff.json",
  "title": "Options",
  "type": "object",
  "properties": {
    "Flake8CopyrightOptions": {
      "description": "Options for the `flake8-copyright` plugin.",
      "type": "object",
      "properties": {
        "min-file-size": {
          "description": "A minimum file size (in bytes) required for a copyright notice to be enforced. By default, all files are validated.",
          "type": [
            "integer",
            "null"
          ],
          "format": "uint",
          "minimum": 0
        }
      }
    }
  }
}

Sample data

irrelevant

Your code

import Ajv from "ajv";

const ajv = new Ajv({ allErrors: true, strictSchema: "log" });

const schema = {
  $schema: "http://json-schema.org/draft-07/schema#",
  $id: "https://json.schemastore.org/ruff.json",
  title: "Options",
  type: "object",
  properties: {
    Flake8CopyrightOptions: {
      description: "Options for the `flake8-copyright` plugin.",
      type: "object",
      properties: {
        "min-file-size": {
          description:
            "A minimum file size (in bytes) required for a copyright notice to be enforced. By default, all files are validated.",
          type: ["integer", "null"],
          format: "uint",
          minimum: 0,
        },
      },
    },
  },
};

const validate = ajv.compile(schema);

const data = {
  foo: 1,
  bar: "abc",
}; // this doesn't matter

validate(data);
console.log(validate.errors);

Validation result, data AFTER validation, error messages

                throw new Error(unknownMsg());
                      ^
Error: unknown format "uint" ignored in schema at path "#/properties/Flake8CopyrightOptions/properties/min-file-size"

What results did you expect?

With { strictSchema: "log" }, I would expect this error to be logged (like it is when strictSchema is false), not thrown as an exception.

Are you going to resolve the issue?

I have not contributed before, but I would be happy to try and submit a PR if you agree this is a bug.

@mpal15
Copy link

mpal15 commented Mar 17, 2025

Hi, can you assign to me .i want to try to solve this problem

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

No branches or pull requests

2 participants