コード例 #1
0
        public static string GetAllJsValidations()
        {
            var assembly       = AssemblyIdentifier.Get();
            var validatorTypes = assembly.GetTypes()
                                 .Where(x =>
                                        (x.BaseType?.IsGenericType ?? false) &&
                                        x.BaseType.GetGenericTypeDefinition() == typeof(AbstractValidator <>))
                                 .Select(x => x);

            var jsValidatorObjectBuilder = new StringBuilder();

            jsValidatorObjectBuilder.AppendLine("{");

            foreach (var type in validatorTypes)
            {
                try
                {
                    dynamic validator = Activator.CreateInstance(type);
                    jsValidatorObjectBuilder.Append(JsConverter.GetJavascript(validator));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            jsValidatorObjectBuilder.AppendLine("};");
            return(jsValidatorObjectBuilder.ToString());
        }
コード例 #2
0
        public static List <string> GetActualErrors <T>(T seedData, AbstractValidator <T> abstractValidator)
        {
            var errorsList = new List <string>();

            var abstractValidatorName = abstractValidator.GetType().Name;
            var js = JsConverter.GetJavascript(abstractValidator);

            js = js.Replace(abstractValidatorName + ":function", "function " + abstractValidatorName)
                 .Remove(js.LastIndexOf(",", StringComparison.Ordinal));

            var engine = new Engine().Execute(js);

            dynamic errorsResult = (engine.Invoke(abstractValidatorName, seedData)
                                    .ToObject() as IDictionary <string, object>)?
                                   .Values
                                   .FirstOrDefault();

            if (errorsResult != null)
            {
                errorsList.Add(errorsResult[0].errorKey.ToString());
            }

            return(errorsList);
        }