Skip to content

fix: strictSchema "log" should warn (not throw) on unknown format#2636

Open
chatman-media wants to merge 1 commit into
ajv-validator:masterfrom
chatman-media:fix/strict-schema-log-unknown-format
Open

fix: strictSchema "log" should warn (not throw) on unknown format#2636
chatman-media wants to merge 1 commit into
ajv-validator:masterfrom
chatman-media:fix/strict-schema-log-unknown-format

Conversation

@chatman-media

Copy link
Copy Markdown

Fixes #2538.

What issue does this pull request resolve?

With strictSchema: "log", an unknown format in a schema throws an exception instead of logging a warning. Per the documented option values, "log" should log a warning while only true should throw.

const ajv = new Ajv({strictSchema: "log"})
ajv.compile({type: "string", format: "unknown"})
// Expected: logs `unknown format "unknown" ...`
// Actual:   throws `Error: unknown format "unknown" ignored in schema at path ...`

Root cause

In lib/vocabularies/format/format.ts, unknownFormat() only special-cased opts.strictSchema === false (warn) and threw for every other value. Since "log" is not === false, it fell through to throw. As a side effect, strictSchema: false also emitted a warning, whereas it should be silent ("ignore all strict schema violations").

Fix

Replace the hand-rolled branch with the shared checkStrictMode helper (lib/compile/util.ts), which is already used throughout the codebase for the true | false | "log" tri-state:

  • true → throw
  • "log"logger.warn
  • false → silent (no-op)

This also makes the message consistent with every other strict-mode violation (prefixed with strict mode:).

Tests

Added spec/issues/2538_strict_schema_log_unknown_format.spec.ts covering all three modes. The "log" and false cases fail on master and pass with this change; true still throws. Existing spec/options/unknownFormats.spec.ts and the strict-mode suites remain green, and the full JSON Schema conformance suite passes.

The format keyword's unknownFormat() only special-cased
strictSchema === false (log) and threw for everything else, so
strictSchema: "log" incorrectly threw instead of logging a warning.

Replace the hand-rolled branch with the shared checkStrictMode helper,
which correctly handles the true/false/"log" tri-state used everywhere
else and is silent (not a warning) when strict mode is disabled.

Closes ajv-validator#2538
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

strictSchema: "log" throws on some schema compile errors

1 participant