private IPropertyValidationRule <T>?CreateRule <T>(IProperty <T> property, IMetadata validationMetadata) { return(validationMetadata switch { IAllowedValues allowedValues => new OnlyAllowedValuesRule <T>(property, (IAllowedValues <T>?)allowedValues), INumericInterval numericInterval => new ShouldBeInInterval <T>(property, numericInterval), _ => null, });
/// <summary> /// Adds <see cref="IAllowedValues{T}"/> metadata to property. /// </summary> /// <typeparam name="T">Property value type.</typeparam> /// <param name="property">Source property.</param> /// <param name="allowedValues">Allowed values.</param> /// <returns>The same property.</returns> public static TSchema SetAllowedValues <TSchema, T>(this TSchema property, IAllowedValues <T> allowedValues) where TSchema : ISchema <T> { property.AssertArgumentNotNull(nameof(property)); allowedValues.AssertArgumentNotNull(nameof(allowedValues)); return(property.SetMetadata((IAllowedValues)allowedValues)); }
/// <summary> /// Checks that property value is in allowed values list. /// If <paramref name="allowedValues"/> is not set, then <see cref="SchemaExtensions.GetAllowedValues{T}"/> will be used. /// Set allowed values for property with one of SetAllowedValues methods. /// </summary> /// <typeparam name="T">Property type.</typeparam> /// <typeparam name="TValidationRule">Combined validation rule type.</typeparam> /// <param name="linker">Rule linker.</param> /// <param name="allowedValues">Optional <see cref="IAllowedValues{T}"/>.</param> /// <returns>Validation rule.</returns> public static TValidationRule OnlyAllowedValues <T, TValidationRule>(this IValidationRuleLinker <T, TValidationRule> linker, IAllowedValues <T>?allowedValues = null) where TValidationRule : IPropertyValidationRule <T> { return(linker.CombineWith(OnlyAllowedValues(linker.FirstRule.Property, allowedValues))); }
/// <summary> /// Checks that property value is in allowed values list. /// If <paramref name="allowedValues"/> is not set, then <see cref="SchemaExtensions.GetAllowedValues{T}"/> will be used. /// Set allowed values for property with one of SetAllowedValues methods. /// </summary> /// <typeparam name="T">Value type.</typeparam> /// <param name="property">Property to check.</param> /// <param name="allowedValues">Optional <see cref="IAllowedValues{T}"/>.</param> /// <returns>Validation rule.</returns> public static OnlyAllowedValuesRule <T> OnlyAllowedValues <T>(this IProperty <T> property, IAllowedValues <T>?allowedValues = null) { return(new OnlyAllowedValuesRule <T>(property, allowedValues)); }
/// <summary> /// Initializes a new instance of the <see cref="OnlyAllowedValuesRule{T}"/> class. /// </summary> /// <param name="property">Property to check.</param> /// <param name="allowedValues">Optional <see cref="IAllowedValues{T}"/>.</param> public OnlyAllowedValuesRule(IProperty <T> property, IAllowedValues <T>?allowedValues = null) : base(property, "{propertyName} can not be '{value}' because it is not in allowed values list.") { _allowedValues = allowedValues ?? Property.GetAllowedValues(); }