public static void Apply(IEnumerable <Patch> patches, DynamicModel model) { foreach (var patch in patches) { model.Replace(patch.Field, patch.Value); } }
public ValidationResult Validate(Patch[] patches, DynamicModel model) { Model = model; foreach (var patch in patches) { _isAllowed = false; Patch = patch; ValidatePatch(); if (!_isAllowed) { var error = new Error { Code = 684257, Message = $"The field '{patch.Field}' is not patchable.", Values = new { Field = patch.Field } }; Result.Errors.Add(error); } } Model = model.Copy(); ModelPatcher.Apply(patches, Model); Validate(Model); // NOTE: This call sets Model to null at the end. It doesn't // matter, because the next line does the same, but if that ever // changes, you need to fix this. Model = null; Patch = null; return(Result); }
public ValidationResult Validate(DynamicModel model) { Model = model; _fields = new Dictionary <string, FieldType>(); foreach (var property in model.Properties) { Property = property; ValidateModel(); if (!_fields.ContainsKey(property.Key)) { var error = new Error { Code = 954374, Message = $"'{property.Key}' is not a valid field.", Values = new { Field = property.Key } }; Result.Errors.Add(error); } } foreach (var field in _fields) { if (field.Value == FieldType.Missing) { var error = new Error { Code = 548456, Message = $"The field '{field.Key}' is required.", Values = new { Field = field.Key } }; Result.Errors.Add(error); } } Model = null; return(Result); }