예제 #1
0
        public ValidatorResult Validate(string name, object value)
        {
            var result = new ValidatorResult();

            if (value == null)
            {
                return(result);
            }

            var script = value.ToString();

            if (script == string.Empty)
            {
                return(result);
            }

            try {
                var program = _jint.Parse(script, _options);
                if (program != null && program.Errors != null && program.Errors.Any())
                {
                    result.Valid = false;
                    foreach (var e in program.Errors)
                    {
                        result.Error("{0}, script: {1}...", e.Message, script.Left(30));
                    }
                }
            } catch (ParserException ex) {
                result.Valid = false;
                result.Error("{0}, script: {1}...", ex.Message, script.Left(30));
            }
            return(result);
        }
        public static ClassRegistration Process(RegistrationRequest request, IEnumerable <Validator <RegistrationRequest> > validations)
        {
            ClassRegistration registrationResult = new ClassRegistration();

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                registrationResult.Errors.Add(ValidatorResult.Error("Name", "Name is required"));
            }

            if (validations != null)
            {
                foreach (var validator in validations)
                {
                    var validationResult = validator.Validate(request);

                    if (!validationResult.IsValid)
                    {
                        registrationResult.Errors.Add(validationResult);
                    }
                }
            }

            if (registrationResult.IsValid)
            {
                registrationResult.ParseName(request.Name);
                registrationResult.ParsePhone(request.Phone);
                decimal amount;
                decimal.TryParse(request.PaymentAmount, out amount);
                registrationResult.PaymentAmount       = amount;
                registrationResult.Email               = request.Email;
                registrationResult.Address1            = request.Address;
                registrationResult.City                = request.Town;
                registrationResult.SpecialInstructions = request.SpecialInstructions;
                registrationResult.EventName           = request.EventName;
                registrationResult.EventDescription    = request.EventDescription;
            }

            return(registrationResult);
        }