Skip to content

Error messages and parameters differ between default and strict (CHECK_MODE_STRICT) validation #975

Open

Description

@Roman3349

With Constraint::CHECK_MODE_STRICT, Draft-06, Draft-07 and Draft 2019-09 schemas are validated by the per-draft constraints in Constraints/Drafts/*. Without the flag, the legacy constraints are used. For the same schema and the same invalid instance, the two modes report errors with different message texts and different constraint.params.

This matters for anyone who shows the messages to users, asserts them in tests, or reads params programmatically. Turning on strict mode silently changes the API output even when the validation result is identical.

Reproduction

<?php
require __DIR__ . '/vendor/autoload.php';

use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;

$cases = [
    'type' => ['{"type":"string"}', 'true'],
    'type (multiple)' => ['{"type":["string","null"]}', '1'],
    'dependencies' => ['{"dependencies":{"a":["b","c"]}}', '{"a":1}'],
    'additionalProperties' => ['{"properties":{},"additionalProperties":false}', '{"x":1}'],
    'allOf' => ['{"allOf":[{"type":"string"}]}', '1'],
    'minLength' => ['{"minLength":5}', '"abc"'],
];

foreach ($cases as $name => [$schemaJson, $dataJson]) {
    echo $name, PHP_EOL;
    foreach (['default' => Constraint::CHECK_MODE_NORMAL, 'strict' => Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_STRICT] as $mode => $flags) {
        // Replace with draft-06 or 2019-09 to get the same differences
        $schema = json_decode('{"$schema":"http://json-schema.org/draft-07/schema#",' . substr($schemaJson, 1));
        $data = json_decode($dataJson);
        $validator = new Validator();
        $validator->validate($data, $schema, $flags);
        foreach ($validator->getErrors() as $error) {
            printf("  %-7s %s %s\n", $mode, $error['message'], json_encode($error['constraint']['params']));
        }
    }
}

Differences found (6.14.0)

Unless noted otherwise, the differences below are identical for Draft-06, Draft-07 and Draft 2019-09 schemas.

Keyword Default mode Strict mode
type Boolean value found, but a string is required
{"found":"boolean","expected":"a string"}
Boolean value found, but string is required
{"found":"boolean","expected":"string"}
type (multiple types) Integer value found, but a string or a null is required
{"expected":"a string or a null"}
Integer value found, but string, null is required
{"expected":"string, null"}
dependencies (Draft-06/07) One error: A depends on ["b","c"], which is missing
{"key":"a","dependency":["b","c"]}
One error per missing property: A depends on b, which is missing
{"dependant":"a","dependency":"b"}
additionalProperties {"property":"x"} {"found":"x"} (same message, renamed parameter)
allOf Sub-schema errors first, then Failed to match all schemas Failed to match all schemas first, then sub-schema errors
minLength, maxLength, minimum, maximum, multipleOf, minProperties, maxProperties, pattern {"minLength":5} {"minLength":5,"found":3} (same message, extra found parameter)

Separately, the strict-only propertyNames error renders the whole sub-schema instead of the property name, because propertyNames is the first parameter:

Property name {"maxLength":1} is invalid

The expected output is Property name abc is invalid (name is passed as a parameter, but last). #987 fixes this.

In Draft 2019-09, dependencies is replaced by dependentRequired and dependentSchemas, which only strict mode validates (strict mode ignores dependencies there, while default mode still applies it). Strict mode reports dependentRequired with the same per-property message as dependencies in Draft-06/07 (A depends on b, which is missing, {"dependant":"a","dependency":"b"}).

enum, const, format, required, minItems, maxItems, uniqueItems, additionalItems, oneOf and not produce identical output in both modes for all three drafts.

Draft 2020-12 (#971) is built from the Draft 2019-09 constraints, so it has the same differences.

Expected behaviour

The same failure should produce the same message, and ideally the same constraint.params keys, regardless of whether CHECK_MODE_STRICT is set. Strict mode is meant to change which rules apply, not how errors are worded.

A possible approach is to make the draft constraints pass parameters in the same order and format as the legacy ones (e.g. expected as a string or a null, property for additionalProperties). Alternatively, addError() could use named placeholders instead of relying on the positional order of $more. That would also prevent bugs like the pattern and propertyNames messages.

Environment

  • justinrainbow/json-schema 6.14.0 (originally reported against 6.13.1)
  • PHP 8.5

Related

Activity

  1. github-actions commented on Oct 14, 2026

    @github-actions
    Contributor

    🚀 @Roman3349, thanks for your contribution! Every idea, bug report, and discussion helps make this project better. Your input is invaluable, and we appreciate the time you took to share it. A maintainer will review it soon—stay awesome! 💡✨

    ⭐ Enjoying contributing? Star the project! ⭐ Your contributions help this project grow, and we'd love your support in another way too! If you find this repo helpful, consider leaving a star 🌟 on GitHub.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions