Пример #1
0
 public static void AddressMustBeValid <T>(
     this IRuleBuilderInitial <T, string> ruleBuilder)
 {
     ruleBuilder
     .Must(x => Address.ValidateFormatAndChecksum(x, true, true))
     .WithMessage(x => $"Specified address is not valid.");
 }
Пример #2
0
 public static void AssetMustBeSupported <T>(
     this IRuleBuilderInitial <T, string> ruleBuilder)
 {
     ruleBuilder
     .Must(assetId => assetId == Constants.AssetId)
     .WithMessage(x => $"Specified asset is not supported.");
 }
Пример #3
0
 public static void TransactionIdMustBeNonEmptyGuid <T>(
     this IRuleBuilderInitial <T, Guid> ruleBuilder)
 {
     ruleBuilder
     .Must(transactionId => transactionId != Guid.Empty)
     .WithMessage(x => "Specified transaction id is empty.");
 }
Пример #4
0
 public static void MustBeHexString <T>(
     this IRuleBuilderInitial <T, string> ruleBuilder)
 {
     ruleBuilder
     .Must(@string => HexStringExpression.IsMatch(@string))
     .WithMessage(x => "Must be a hex string.");
 }
Пример #5
0
        public static void AssetMustBeValidAndSupported <T>(
            this IRuleBuilderInitial <T, string> ruleBuilder,
            IAssetService assetService)
        {
            ruleBuilder
            .Must((rootObject, assetId, context) =>
            {
                if (string.IsNullOrWhiteSpace(assetId))
                {
                    context.MessageFormatter.AppendArgument("Reason", "Must be specified.");

                    return(false);
                }

                if (assetService.Get().Id != assetId)
                {
                    context.MessageFormatter.AppendArgument("Reason", "Must be supported.");

                    return(false);
                }

                return(true);
            })
            .WithMessage("{Reason}");
        }
        public static IRuleBuilderOptions <T, Guid> IsNotEmptyGuid <T>(this IRuleBuilderInitial <T, Guid> ruleBuilderInitial)
        {
            var ruleBuilderOptions = ruleBuilderInitial
                                     .Must(id => id != Guid.Empty)
                                     .WithMessage($"Cannot be {Guid.Empty}");

            return(ruleBuilderOptions);
        }
        public static IRuleBuilderOptions <T, string> IsGuid <T>(this IRuleBuilderInitial <T, string> ruleBuilderInitial)
        {
            var ruleBuilderOptions = ruleBuilderInitial
                                     .Must(id => Guid.TryParse(id, out _))
                                     .WithMessage("invalid guid");

            return(ruleBuilderOptions);
        }
 public static IRuleBuilderOptions <TContainer, TProperty> UseValidator <TContainer, TProperty>(
     this IRuleBuilderInitial <TContainer, TProperty> options, PropertyValidator <TProperty> validator)
 {
     if (validator == null)
     {
         throw new ArgumentNullException();
     }
     return(validator.Validate(options.Must(x => true)));
 }
Пример #9
0
        public static IRuleBuilderOptions <T, bool?> IsBoolean <T>(this IRuleBuilderInitial <T, bool?> ruleBuilder)
        {
            return(ruleBuilder.Must((rootObject, boolenVal, context) =>
            {
                if (boolenVal is bool)
                {
                    return true;
                }

                return false;
            }).WithMessage("[{PropertyName}] of [{PropertyValue}] must be boolean."));
        }
Пример #10
0
        public static IRuleBuilderOptions <T, TProperty> EntityMustExist <T, TProperty, TEntity>(
            this IRuleBuilderInitial <T, TProperty> ruleBuilder,
            DbContext db)
            where TEntity : class, IEntity
        {
            return(ruleBuilder
                   .Must((message, property) =>
            {
                Guid?value = property as Guid?;
                if (!value.HasValue)
                {
                    throw new Exception("");     // TODO
                }

                return db.Set <TEntity>().Any(x => x.Id == value.Value);
            })
                   .WithHttpStatusCode(HttpStatusCode.NotFound));
        }
Пример #11
0
 public static void StudyMode <T>(
     this IRuleBuilderInitial <T, CourseStudyMode?> field,
     Func <T, bool> studyModeWasSpecified,
     Func <T, CourseDeliveryMode?> getDeliveryMode)
 {
     field
     // Required for classroom based delivery modes
     .Must((t, v) => studyModeWasSpecified(t) && v.HasValue)
     .When(t => getDeliveryMode(t) == CourseDeliveryMode.ClassroomBased, ApplyConditionTo.CurrentValidator)
     .WithMessageFromErrorCode("COURSERUN_STUDY_MODE_REQUIRED")
     // Not allowed for delivery modes other than classroom based
     .Must((t, v) => !studyModeWasSpecified(t))
     .When(
         t =>
     {
         var deliveryMode = getDeliveryMode(t);
         return(deliveryMode.HasValue && deliveryMode != CourseDeliveryMode.ClassroomBased);
     },
         ApplyConditionTo.CurrentValidator)
     .WithMessageFromErrorCode("COURSERUN_STUDY_MODE_NOT_ALLOWED");
 }
Пример #12
0
 public static IRuleBuilderOptions <TDto, string> IsEntityId <TDto>(this IRuleBuilderInitial <TDto, string> rule,
                                                                    IIdentifierFactory identifierFactory)
 {
     return(rule.Must(id => id.HasValue() && identifierFactory.IsValid(Identifier.Create(id))));
 }
Пример #13
0
 public static IRuleBuilderOptions <T, Guid> IsValidGuid <T>(this IRuleBuilderInitial <T, Guid> ruleBuilder)
 {
     return(ruleBuilder.Must(val => val != Guid.Empty)
            .WithErrorCode(nameof(ValidGuidValidator)));
 }
 public static IRuleBuilderOptions <T, TProperty> IsContainedOn <T, TProperty>(this IRuleBuilderInitial <T, TProperty> ruleBuilderInitial, IEnumerable <TProperty> values)
 {
     return(ruleBuilderInitial.Must((p) => values.Contains(p)));
 }
Пример #15
0
 public static IRuleBuilderOptions <TModel, decimal?> IsValidDecimalWithDecimalPlaces <TModel>(this IRuleBuilderInitial <TModel, decimal?> ruleBuilderInitial, int decimalPlaces, ILocalizationService localizationService)
 {
     return(ruleBuilderInitial
            .Must(property => IsValidDecimalWithDecimalPlaces(property, decimalPlaces))
            .WithMessage(AllowedDecimalPlacesValidationMessage(decimalPlaces, localizationService)));
 }