public InheritanceValidator <T> ValidateIsolated <TProp>(string property) { ChildValidators.Add((obj, index) => { var children = obj.SelectToken($"$.{property}"); obj.Remove(property); return(new InheritanceValidator <TProp>(children as JArray, Settings) .At($"{Path}[{index}].{property}") .OnError(RaiseError) .Validate()); }); return(this); }
public bool Validate() { if (Objects == null) { return(true); } var count = Objects.Count(); var isValid = true; for (int i = 0; i < count; i++) { var obj = (JObject)Objects[i]; isValid = ChildValidators.Select(validate => validate(obj, 0)).Aggregate(true, (a, b) => a && b) && isValid; string discriminatorValue = obj.SelectToken($"$.{Dicriminator}")?.ToString(); if (discriminatorValue == null) { RaiseError(new SchemaValidationError( path: $"{Path}[{i}]", message: $"Required property {Dicriminator} is missing." )); continue; } try { var schema = Schemas[discriminatorValue]; var errors = schema.Validate(obj); isValid = isValid && errors.Count() == 0; RaiseErrors(errors, $"{Path}[{i}]"); } catch (KeyNotFoundException) { RaiseError(new SchemaValidationError( path: $"{Path}[{i}].{Dicriminator}", message: $"Property {Dicriminator} can only be one of the following: {string.Join(", ", Schemas.Keys)}. (found {discriminatorValue})" )); continue; } } return(isValid); }