Skip to content

JsonSchema errors for additionalProperties not the same as 2019-09 draft specification #89

@brigadier90

Description

@brigadier90

.net core 3.1

  • Nuget Package: JsonSchema.Net
  • Nuget Version: 1.9.3
  • OS: Windows 10 version 20H2

I am trying to replicate the scenario specified in this official draft. Here is my code:



using Json.Pointer;
using Json.Schema;
using System;
using System.Text.Json;

namespace ConsoleApp1
{
    class Program
    {
        static void GetErrorString(
            ValidationResults results,
            ref string errorString)
        {
            if (results.Message != null)
            {
                errorString += results.InstanceLocation + ": ";
                errorString += results.Message + "\n";
            }

            foreach (var result in results.NestedResults)
                GetErrorString(result, ref errorString);
        }

        static void Main(string[] args)
        {
            var schema = JsonSchema.FromFile("./point.json");
            var jsnStr = @"[
                {""x"": 2.5, ""y"":1.3}, 
                {""x"": 1, ""z"":6.7}
            ]";

            var jsnObj = JsonDocument.Parse(jsnStr).RootElement;

            ValidationOptions options = new ValidationOptions();
            options.OutputFormat = OutputFormat.Detailed;
            options.ValidateAs = Draft.Draft201909;

            var result = schema.Validate(jsnObj, options);

            var errorString = "";
            if(!result.IsValid)
                GetErrorString(result, ref errorString);

            Console.WriteLine(errorString);
        }
    }
}

This is my output:

#: Value has less than 3 items
#/1: Required properties [y] were not present
#/1/z: All values fail against the false schema

I noticed the error for pointer "#/1/z" is different to the one specified in the draft here which says: "Additional property 'z' found but was invalid".

This becomes especially problematic when I have the following schema:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$id": "/CheckRequest",
  "type": "object",
  "properties": {
    "code": {
      "type": "string",
      "minLength": 4,
      "maxLength": 10
    },
    "phone": {
      "type": "string",
      "pattern": "^\\+[1-9]\\d{1,14}$"
    }
  },
  "required": [ "code", "phone"],
  "additionalProperties":  false 
}

with the following json instance:

var jsnStr = @"{
                ""code"": ""12555"", 
                ""phone"":""357998765436"", 
                ""extra"":""extra""
            }";

Note: the phone input is invalid since it doesn not match the regex ( does not begin with a +) and my output becomes:

#/phone: The string value was not a match for the indicated regular expression
#/code: All values fail against the false schema
#/phone: All values fail against the false schema
#/extra: All values fail against the false schema

Why does this message (All values fail against the false schema) print for all my properties instead of just the "extra" property?

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature in an existing library

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions