Example #1
0
 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,
     });
        public static string ToIntervalString(this INumericInterval interval, string?intervalDelimeter = "..")
        {
            intervalDelimeter ??= "..";
            string left         = interval.Minimum.HasValue ? interval.Minimum.Value.ToString(NumberFormatInfo.InvariantInfo) : "-Infinity";
            string right        = interval.Maximum.HasValue ? interval.Maximum.Value.ToString(NumberFormatInfo.InvariantInfo) : "+Infinity";
            string leftBracket  = interval.IsLeftIncluded() ? "[" : "(";
            string rightBracket = interval.IsRightIncluded() ? "]" : ")";

            return($"{leftBracket}{left}{intervalDelimeter}{right}{rightBracket}");
        }
        /// <summary>
        /// Adds <see cref="INumericInterval"/> metadata to numeric property or schema.
        /// </summary>
        /// <typeparam name="TSchema">Schema type.</typeparam>
        /// <param name="schema">Source property.</param>
        /// <param name="numericInterval">Numeric interval for property values.</param>
        /// <returns>The same property.</returns>
        public static TSchema SetNumericInterval <TSchema>(this TSchema schema, INumericInterval numericInterval, MetadataMergeMode mergeMode = MetadataMergeMode.Set)
            where TSchema : ISchema
        {
            schema.AssertArgumentNotNull(nameof(schema));
            schema.AssertPropertyIsNumeric();

            INumericInterval interval = numericInterval;

            if (mergeMode == MetadataMergeMode.Merge && schema.GetNumericInterval() is { } existing)
            {
                interval = new NumericInterval(
                    minimum: numericInterval.Minimum ?? existing.Minimum,
                    exclusiveMinimum: numericInterval.ExclusiveMinimum ?? existing.ExclusiveMinimum,
                    maximum: numericInterval.Maximum ?? existing.Maximum,
                    exclusiveMaximum: numericInterval.ExclusiveMaximum ?? existing.ExclusiveMaximum);
            }

            return(schema.SetMetadata(interval));
        }
 public static bool IsRightIncluded(this INumericInterval interval)
 {
     return(!interval.ExclusiveMaximum.HasValue || interval.ExclusiveMaximum == false);
 }
 public static TSchema MergeNumericInterval <TSchema>(this TSchema schema, INumericInterval numericInterval)
     where TSchema : ISchema
 {
     return(SetNumericInterval(schema, numericInterval, MetadataMergeMode.Merge));
 }