public Task <IValidationResult> HandleAsync(ModelValidatorContext context) { IValidationResultBuilder resultBuilder = CreateResultBuilder(); ValidateInternal(context.Definition, context.Getter, resultBuilder); return(Task.FromResult(resultBuilder.ToResult())); }
public FieldMetadataValidatorContext(IFieldDefinition fieldDefinition, IModelValueGetter getter, IValidationResultBuilder resultBuilder) { Ensure.NotNull(fieldDefinition, "fieldDefinition"); Ensure.NotNull(getter, "getter"); Ensure.NotNull(resultBuilder, "resultBuilder"); FieldDefinition = fieldDefinition; Getter = getter; ResultBuilder = resultBuilder; }
public UsuarioController(IServiceProvider serviceProvider, IValidationResultBuilder validationResultBuilder, IMapper mapper, IHandler handler) : base(validationResultBuilder) { this.serviceProvider = serviceProvider; this.mapper = mapper; this.handler = handler; }
protected override void Handle(IValidationResultBuilder builder, string model) { base.Handle(builder, model); if (model != "Hello, World!" && model != "Hall Welt!") { builder.AddTextMessage(null, "Message be a 'Hello, World!' kind of message."); } }
// --- Constructors --- /// <remarks>Use this constructor if an instance of an object is being validated.</remarks> /// <param name="instanceToManage">An instance of the object to be validated.</param> /// <exception cref="ArgumentNullException"><paramref name="instanceToManage"/> is null.</exception> protected ValidationManger(object instanceToManage) { Guard.ArgumentNotNull(instanceToManage, "targetObject"); ManagedObject = instanceToManage; #if(WindowsCE || SILVERLIGHT) TargetHandle = instanceToManage.GetType().TypeHandle; #else TargetHandle = Type.GetTypeHandle(instanceToManage); #endif resultBuilder = new DefaultValidationResultBuilder(); }
public BaseController(IValidationResultBuilder validationResultBuilder) { _validationResultBuilder = validationResultBuilder; }
/// <summary> /// Adds <see cref="KeyValuesMustMatchMessage"/> to the <paramref name="builder" /> /// </summary> /// <param name="builder">Result builder.</param> /// <param name="key">Target field key.</param> /// <param name="otherKey">Other key that is required to be equal to the value of <paramref name="key"/>.</param> /// <returns><paramref name="builder"/>.</returns> public static IValidationResultBuilder AddKeyValuesMustMatch(this IValidationResultBuilder builder, string key, string otherKey) { Ensure.NotNull(builder, "builder"); return(builder.Add(new KeyValuesMustMatchMessage(key, otherKey))); }
/// <summary> /// Adds <see cref="StringNullOrEmptyMessage"/> to the <paramref name="builder" /> /// </summary> /// <param name="builder">Result builder.</param> /// <param name="key">Validation message key.</param> /// <returns><paramref name="builder"/>.</returns> public static IValidationResultBuilder AddStringNullOrEmpty(this IValidationResultBuilder builder, string key) { Ensure.NotNull(builder, "builder"); return(builder.Add(new StringNullOrEmptyMessage(key))); }
/// <summary> /// Adds <see cref="StringLengthMessage"/> to the <paramref name="builder" /> /// </summary> /// <param name="builder">Result builder.</param> /// <param name="key">Validation message key.</param> /// <param name="minLength">Minimal required string length.</param> /// <param name="maxLength">Maximal allowed string length.</param> /// <returns><paramref name="builder"/>.</returns> public static IValidationResultBuilder AddStringLength(this IValidationResultBuilder builder, string key, int?minLength, int?maxLength) { Ensure.NotNull(builder, "builder"); return(builder.Add(new StringLengthMessage(key, minLength, maxLength))); }
/// <summary> /// Adds <see cref="TextValidationMessage"/> to the <paramref name="builder"/>. /// </summary> /// <param name="builder">Result builder.</param> /// <param name="key">Message key.</param> /// <param name="message">Text format.</param> /// <param name="parameters">Optional parameters for <paramref name="message"/>.</param> /// <returns><paramref name="builder"/>.</returns> public static IValidationResultBuilder AddTextMessage(this IValidationResultBuilder builder, string key, string message, params object[] parameters) { Ensure.NotNull(builder, "builder"); return(builder.Add(new TextValidationMessage(key, String.Format(message, parameters)))); }
/// <summary> /// Validates <paramref name="models"/> to builds the result using <paramref name="builder"/>. /// </summary> /// <param name="builder">A validation result builder.</param> /// <param name="model">An instance to validate.</param> protected virtual void Handle(IValidationResultBuilder builder, TModel model) { }
/// <summary> /// Provides behavior when metadata key is not found in field definition. /// By default, does nothing. /// </summary> /// <param name="fieldDefinition">Defines field to validate.</param> /// <param name="getter">Provides current values.</param> /// <param name="resultBuilder">Validation result builder.</param> protected virtual void MissingMetadataKey(IFieldDefinition fieldDefinition, IModelValueGetter getter, IValidationResultBuilder resultBuilder) { }
/// <summary> /// Reads metadata value from <paramref name="fieldDefinition"/> and current value from <paramref name="getter"/> and passes these values to <see cref="Validate"/>. /// If metadata key is not found, <see cref="MissingMetadataKey"/> is called. /// Returns <c>true</c> if field is valid; false otherwise. /// </summary> /// <param name="fieldDefinition">Defines field to validate.</param> /// <param name="getter">Provides current values.</param> /// <param name="resultBuilder">Validation result builder.</param> /// <returns><c>true</c> if field is valid; false otherwise.</returns> public void Validate(IFieldDefinition fieldDefinition, IModelValueGetter getter, IValidationResultBuilder resultBuilder) { Ensure.NotNull(fieldDefinition, "fieldDefinition"); Ensure.NotNull(getter, "getter"); Ensure.NotNull(resultBuilder, "resultBuilder"); object metadataValue; if (fieldDefinition.Metadata.TryGet(MetadataKey, out metadataValue)) { TFieldValue fieldValue = getter.GetValueOrDefault(fieldDefinition.Identifier, default(TFieldValue)); TMetadataValue metadata = (TMetadataValue)metadataValue; Validate(fieldValue, metadata, new FieldMetadataValidatorContext(fieldDefinition, getter, resultBuilder)); } else { MissingMetadataKey(fieldDefinition, getter, resultBuilder); } }
/// <summary> /// Provides logic for validating model. /// </summary> /// <param name="modelDefinition">Model definition.</param> /// <param name="fieldDefinition">Defines field to validate.</param> /// <param name="getter">Provides current values.</param> /// <param name="resultBuilder">Validation result builder.</param> protected virtual void ValidateInternal(IModelDefinition modelDefinition, IModelValueGetter getter, IValidationResultBuilder resultBuilder) { foreach (IFieldDefinition fieldDefinition in modelDefinition.Fields) { FieldValidator.Validate(modelDefinition, fieldDefinition, getter, resultBuilder); } }
public void Validate(IModelDefinition modelDefinition, IFieldDefinition fieldDefinition, IModelValueGetter getter, IValidationResultBuilder resultBuilder) { foreach (string key in fieldDefinition.Metadata.Keys) { IFieldMetadataValidator validator; if (Validators.TryGet(modelDefinition.Identifier, fieldDefinition.Identifier, key, out validator)) { validator.Validate(fieldDefinition, getter, resultBuilder); } } }