public override IEnumerable<ValidationResult> Validate(object value, string config, PropertyEditor editor) { //TODO: localize these! Type valueType; //convert the string to a known type switch (editor.ValueEditor.ValueType.ToUpper()) { case "INT": valueType = typeof (int); break; case "STRING": case "TEXT": valueType = typeof (string); break; case "DATETIME": case "DATE": case "TIME": valueType = typeof(string); break; default: throw new FormatException("The valueType parameter does not match a known value type"); } var attempt = value.TryConvertTo(valueType); if (!attempt.Success) { yield return new ValidationResult(string.Format("Value is not of type {0} and cannot be converted", valueType)); } }
public override IEnumerable<ValidationResult> Validate(object value, string preValues, PropertyEditor editor) { var stringVal = value.ToString(); if (preValues.IsNullOrWhiteSpace()) yield break; var asJson = JObject.Parse(preValues); if (asJson["country"] == null) yield break; if (asJson["country"].ToString() == "Australia") { if (!Regex.IsMatch(stringVal, "^\\d{4}$")) { yield return new ValidationResult("Australian postcodes must be a 4 digit number", new[] { //we only store a single value for this editor so the 'member' or 'field' // we'll associate this error with will simply be called 'value' "value" }); } } else { yield return new ValidationResult("Only Australian postcodes are supported for this validator"); } }
public override IEnumerable<ValidationResult> Validate(object value, string config, PropertyEditor editor) { //TODO: localize these! if (value == null) { yield return new ValidationResult("Value cannot be null"); } if (value is string && ((string)value).IsNullOrWhiteSpace()) { yield return new ValidationResult("Value cannot be empty"); } }
public override IEnumerable<ValidationResult> Validate(object value, string config, string preValues, PropertyEditor editor) { //TODO: localize these! if (!(value is string)) { throw new InvalidOperationException("The value parameter must be a string for this validator type"); } var regex = new Regex((string) config); if (!regex.IsMatch((string)value)) { yield return new ValidationResult("Value is invalid, it does not match the correct pattern"); } }
/// <summary> /// Performs the validation /// </summary> /// <param name="value"></param> /// <param name="config">Can be a json formatted string containing properties: 'delimiter' and 'pattern'</param> /// <param name="editor"></param> /// <returns></returns> public override IEnumerable<ValidationResult> Validate(object value, string config, PropertyEditor editor) { //TODO: localize these! if (!(value is string)) { throw new InvalidOperationException("The value parameter must be a string for this validator type"); } var delimiter = ","; Regex regex = null; if (!config.IsNullOrWhiteSpace()) { var json = JsonConvert.DeserializeObject<JObject>(config); if (json["delimiter"] != null) { delimiter = json["delimiter"].ToString(); } if (json["pattern"] != null) { var regexPattern = json["pattern"].ToString(); regex = new Regex(regexPattern); } } var valueTypeValidator = new ValueTypeValueValidator(); var stringVal = (string) value; var split = stringVal.Split(new[] {delimiter}, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < split.Length; i++) { var s = split[i]; //validate each item, first start with the value type foreach (var v in valueTypeValidator.Validate(s, "", editor)) { yield return v; } //next if we have a regex statement validate with that if (regex != null) { if (!regex.IsMatch(s)) { yield return new ValidationResult("The item at index " + i + " did not match the expression " + regex); } } } }
/// <summary> /// Performs the validation against the value /// </summary> /// <param name="value"></param> /// <param name="config"> /// An object that is used to configure the validator. An example could be a regex /// expression if the validator was a regex validator. /// </param> /// <param name="editor">The property editor instance that is being validated</param> /// <returns></returns> public abstract IEnumerable<ValidationResult> Validate(object value, string config, PropertyEditor editor);
protected bool Equals(PropertyEditor other) { return Id.Equals(other.Id); }
/// <summary> /// Validates the object with the resolved ValueValidator found for this type /// </summary> /// <param name="value"></param> /// <param name="preValues">The current pre-values stored for the data type</param> /// <param name="editor">The property editor instance that we are validating for</param> /// <returns></returns> public override IEnumerable <ValidationResult> Validate(object value, string preValues, PropertyEditor editor) { return(ValidatorInstance.Validate(value, Config, preValues, editor)); }
/// <summary> /// Validates the object with the resolved ValueValidator found for this type /// </summary> /// <param name="value"></param> /// <param name="preValues">The current pre-values stored for the data type</param> /// <param name="editor">The property editor instance that we are validating for</param> /// <returns></returns> public abstract IEnumerable <ValidationResult> Validate(object value, string preValues, PropertyEditor editor);
protected bool Equals(PropertyEditor other) { return(Id.Equals(other.Id)); }
/// <summary> /// Validates the object with the resolved ValueValidator found for this type /// </summary> /// <param name="value"></param> /// <param name="preValues">The current pre-values stored for the data type</param> /// <param name="editor">The property editor instance that we are validating for</param> /// <returns></returns> public override IEnumerable<ValidationResult> Validate(object value, string preValues, PropertyEditor editor) { return ValidatorInstance.Validate(value, Config, preValues, editor); }
/// <summary> /// Performs the validation /// </summary> /// <param name="value"></param> /// <param name="config">Can be a json formatted string containing properties: 'delimiter' and 'pattern'</param> /// <param name="preValues">The current pre-values stored for the data type</param> /// <param name="editor"></param> /// <returns></returns> public override IEnumerable <ValidationResult> Validate(object value, string config, string preValues, PropertyEditor editor) { //TODO: localize these! if (!(value is string)) { throw new InvalidOperationException("The value parameter must be a string for this validator type"); } var delimiter = ","; Regex regex = null; if (!config.IsNullOrWhiteSpace()) { var json = JsonConvert.DeserializeObject <JObject>(config); if (json["delimiter"] != null) { delimiter = json["delimiter"].ToString(); } if (json["pattern"] != null) { var regexPattern = json["pattern"].ToString(); regex = new Regex(regexPattern); } } var stringVal = (string)value; var split = stringVal.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < split.Length; i++) { var s = split[i]; //next if we have a regex statement validate with that if (regex != null) { if (!regex.IsMatch(s)) { yield return(new ValidationResult("The item at index " + i + " did not match the expression " + regex, new[] { //make the field name called 'value0' where 0 is the index "value" + i })); } } } }
/// <summary> /// Validates the object with the resolved ValueValidator found for this type /// </summary> /// <param name="value"></param> /// <param name="editor">The property editor instance that we are validating for</param> /// <returns></returns> public IEnumerable<ValidationResult> Validate(object value, PropertyEditor editor) { return ValidatorInstance.Validate(value, Config, editor); }
public override IEnumerable <ValidationResult> Validate(object value, string config, string preValues, PropertyEditor editor) { //TODO: localize these! if (!(value is string)) { throw new InvalidOperationException("The value parameter must be a string for this validator type"); } var regex = new Regex((string)config); if (!regex.IsMatch((string)value)) { yield return(new ValidationResult("Value is invalid, it does not match the correct pattern")); } }