corvus-dotnet / Corvus.JsonSchema

Support for Json Schema validation and entity generation
Apache License 2.0
103 stars 9 forks source link

Validation pattern did not match (Regex) #445

Open ItsDaveB opened 5 hours ago

ItsDaveB commented 5 hours ago

I have a fairly complex nested blockchain schema where a block can be of multiple types with a variety of properties in each type. Corvus.JsonSchema handles this very well in terms of type generation and validation (CompositionOneOfValidationHandler) however I am seeing some strange behaviour when validating one of the block type properties causing the block to appear as invalid due to properties validation pattern not matching. "Validation pattern - ada did not match '^[0-9a-f]56$'", this could actually be the schema syntax where it has 'ada' as required but enforces the property names pattern at the same time. (^[0-9a-f]{56}$)

Higher up this then causes me an issue because the RollForward validate function is then returning false, when I check IsRollForward after parsing, despite this being a RollForward operation. (CorvusValidation.ObjectValidationHandler)

Schema (Refs inline for brevity)

"Value":
    { "title": "Value"
    , "type": "object"
    , "propertyNames": { "pattern": "^[0-9a-f]{56}$" }
    , "additionalProperties":
      { "type": "object"
      , "propertyNames": { "pattern": "^[0-9a-f]{0,64}$" }
      , "additionalProperties": { "$ref": "AssetQuantity" }
      }
    , "required": [ "ada" ]
    , "properties":
      { "ada":
        { "type": "object"
        , "additionalProperties": false
        , "required": [ "lovelace" ]
        , "properties":
          { "lovelace": { "type": "integer" }
          }
        }
      }
    }

Example Data {{"ada":{"lovelace":2922180},"160a880d9fc45380737cb7e57ff859763230aab28b3ef6a84007bfcc":{"4d495241":500},"16fdd33c86af604e837ae57d79d5f0f1156406086db5f16afb3fcf51":{"44474f4c44":25000000},"5ad8deb64bfec21ad2d96e1270b5873d0c4d0f231b928b4c39eb2435":{"61646f736961":500000000},"5dac8536653edc12f6f5e1045d8164b9f59998d3bdc300fc92843489":{"4e4d4b52":80000000},"804f5544c1962a40546827cab750a88404dc7108c0f588b72964754f":{"56594649":500000},"a0028f350aaabe0545fdcb56b039bfb08e4bb4d8c4d7c3c7d481c235":{"484f534b59":2000000},"af2e27f580f7f08e93190a81f72462f153026d06450924726645891b":{"44524950":1350000000},"b6a7467ea1deb012808ef4e87b5ff371e85f7142d7b356a40d9b42a0":{"436f726e75636f70696173205b76696120436861696e506f72742e696f5d":5000000},"c0ee29a85b13209423b10447d3c2e6a50641a15c57770e27cb9d5073":{"57696e67526964657273":500000},"ea153b5d4864af15a1079a94a0e2486d6376fa28aafad272d15b243a":{"0014df10536861726473":1000000}}}

public bool IsRollForward
            {
                get
                {
                    return this.As<Generated.NextBlockResponse.ResultEntity.RollForward>().IsValid();
                }
            }

I was going to ask about the correct approach when using Corvus.JsonSchema for simply type generation from a specified schema, but also the possibility of opting to just reading data without performing additional validation, but still ensuring the mapped data remains exactly as it was read.

mwadams commented 4 hours ago

So that looks like a problem with your schema definition.

You can't have a propertyNames constraint with that pattern on Value and then also have properties on Value that do not conform to that pattern - that will always be false.

You have correctly applied the pattern in your additionalProperties subschema, which should be fine. Adding the explicit property names to the pattern on the Value schema itself should fix the problem.