public void JsonMatchesValuesTest() { Assert.True(MPJson.Matches(null, null)); Assert.False(MPJson.Matches(null, "x")); Assert.True(MPJson.Matches(1, 1)); Assert.True(MPJson.Matches("a", "a")); Assert.False(MPJson.Matches(1, 2)); Assert.False(MPJson.Matches("a", "b")); Assert.False(MPJson.Matches("a", 1)); }
public void JsonMatchesArraysTest() { object[] array0 = new object[] { }; object[] array1 = new object[] { true }; object[] array3 = new object[] { 1.0, 2.0, "x" }; object[] array3b = new object[] { 1.0, 3.0, "x" }; object[] array2 = new object[] { 1.0, 2.0 }; Assert.False(MPJson.Matches(null, array0)); Assert.True(MPJson.Matches(array3, array3)); Assert.True(MPJson.Matches(array2, array2.ToArray())); Assert.False(MPJson.Matches(array1, array2)); Assert.False(MPJson.Matches(array3, array3b)); Assert.False(MPJson.Matches(array2, array3)); }
private bool ValidateGeneric(Subschema schema, object instance) { bool finalResult = true; for (SchemaFlags mask = schema.Flags & (SchemaFlags.GenericProperties & SchemaFlags.AllProperties); mask != 0; mask = (SchemaFlags)RemoveLowestBit((long)mask)) { Keyword keyword = (Keyword)IndexOfLowestBit((long)mask); bool? result = null; Subschema[] schemaArray; int index; switch (keyword) { case Keyword.If: int level = TurnOffErrors(); result = Validate(schema.If, instance, Keyword.If); RestoreSuppressionLevel(level); result = result == true ? Validate(schema.Then, instance, Keyword.Then) : Validate(schema.Else, instance, Keyword.Else); break; case Keyword.Not: level = TurnOffErrors(); result = !Validate(schema.Not, instance, Keyword.Not); RestoreSuppressionLevel(level); break; case Keyword.AllOf: result = true; schemaArray = schema.AllOf; SchemaPointer.Push(keyword); for (index = 0; index < schemaArray.Length; index++) { if (!Validate(schemaArray[index], instance, index)) { finalResult = false; if (!IsRecordingErrors) { break; } } } SchemaPointer.Pop(); break; case Keyword.AnyOf: result = false; level = TurnOffErrors(); schemaArray = schema.AnyOf; SchemaPointer.Push(keyword); for (index = 0; index < schemaArray.Length; index++) { if (Validate(schemaArray[index], instance, index)) { result = true; break; } } SchemaPointer.Pop(); RestoreSuppressionLevel(level); break; case Keyword.OneOf: result = false; level = TurnOffErrors(); schemaArray = schema.OneOf; SchemaPointer.Push(keyword); for (index = 0; index < schemaArray.Length; index++) { if (Validate(schemaArray[index], instance, index)) { result = !result; if (result == false) { break; } } } SchemaPointer.Pop(); RestoreSuppressionLevel(level); break; case Keyword.Enum: result = false; // TODO: Provide support for type coercion foreach (object v in (object[])schema.Enum.Value) { if (MPJson.Matches(v, instance)) { result = true; break; } } break; case Keyword.Const: // TODO: Provide support for string to value coercion result = MPJson.Matches(schema.Const.Value, instance); break; case Keyword._Ref: var r = schema.Ref; if (r.Version != 0) { var draft = new MPSchema(MPJson.From(instance), r.Version); result = draft.IsValid; break; // This was checked on creation } var rSchema = r.Schema; result = rSchema != null && ReferenceUsageIsControlled(); if (result == true && !Validate(rSchema, instance, Keyword._Ref)) { finalResult = false; } break; case Keyword.Metadata: break; } if (result == false) { finalResult = false; if (ReportError(keyword, schema, instance)) { return(false); } } } return(finalResult); }