Exemplo n.º 1
0
    public void CreateValidators_FromModelMetadataType_MergedValidators()
    {
        // Arrange
        var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
        var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

        var metadata = metadataProvider.GetMetadataForProperty(
            typeof(ProductViewModel),
            nameof(ProductViewModel.Name));
        var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

        // Act
        validatorProvider.CreateValidators(context);

        // Assert
        var validatorItems = context.Results;

        Assert.Equal(2, validatorItems.Count);
        Assert.Single(validatorItems, v => v.Validator is RegularExpressionAttributeAdapter);
        Assert.Single(validatorItems, v => v.Validator is StringLengthAttributeAdapter);
    }
Exemplo n.º 2
0
    public void CreateValidators_ClientValidatorAttribute_SpecificAdapter()
    {
        // Arrange
        var metadataProvider  = TestModelMetadataProvider.CreateDefaultProvider();
        var validatorProvider = TestClientModelValidatorProvider.CreateDefaultProvider();

        var metadata = metadataProvider.GetMetadataForProperty(
            typeof(RangeAttributeOnProperty),
            nameof(RangeAttributeOnProperty.Property));
        var context = new ClientValidatorProviderContext(metadata, GetClientValidatorItems(metadata));

        // Act
        validatorProvider.CreateValidators(context);

        // Assert
        var validatorItems = context.Results;

        Assert.Equal(2, validatorItems.Count);
        Assert.Single(validatorItems, v => v.Validator is RangeAttributeAdapter);
        Assert.Single(validatorItems, v => v.Validator is RequiredAttributeAdapter);
    }