Beispiel #1
0
    public void test_fields()
    {
        var f1 = Type.Fields(this);
        var f2 = Type.Fields <TypeTests>();
        var f3 = Type.Fields(typeof(TypeTests));

        Assert(Array.IndexOf(f1, "foo") != -1);
        Assert(Array.IndexOf(f2, "bar") != -1);
        Assert(Array.IndexOf(f3, "foo") != -1);
        Assert(Array.IndexOf(f3, "Bar3") != -1);

        // Filtered out invalid proeprties
        Assert(Array.IndexOf(f3, "Bar") == -1);
        Assert(Array.IndexOf(f3, "Bar2") == -1);

        // Only getters
        var f4 = Type.Fields(typeof(TypeTests), true, false);

        Assert(Array.IndexOf(f4, "Bar") != -1);
        Assert(Array.IndexOf(f4, "Bar2") == -1);

        // Only setters
        var f5 = Type.Fields(typeof(TypeTests), false, true);

        Assert(Array.IndexOf(f5, "Bar") == -1);
        Assert(Array.IndexOf(f5, "Bar2") != -1);
    }
Beispiel #2
0
        /// Validate an object and return a set of validation errors on failure
        public Result <bool, ValidationError[]> Validate(object instance, string parent = "")
        {
            var errors = new List <ValidationError>();

            foreach (var validator in this.validators)
            {
                var fields = Type.Fields(instance.GetType());
                for (var i = 0; i < fields.Length; ++i)
                {
                    if (!ignored.Contains(fields[i]))
                    {
                        var prop = Type.Field(instance.GetType(), fields[i]).Unwrap();
                        if (!ignoredTypes.Contains(prop.FieldType))
                        {
                            // Console.Log("{0}: Validate field: {1}.{2} of type {3}", validator, parent, fields[i], prop.FieldType);
                            var valid = validator.Validate(this, parent, prop, instance);
                            if (valid.IsErr)
                            {
                                errors.AddRange(valid.Err.Unwrap());
                            }
                        }
                    }
                }
            }
            if (errors.Count > 0)
            {
                return(Result.Err <bool, ValidationError[]>(errors.ToArray()));
            }
            return(Result.Ok <bool, ValidationError[]>(true));
        }