Esempio n. 1
0
        public TestValidator()
        {
            // from: https://jsonplaceholder.typicode.com/

            When(Any)
            .Then(Field("id", Is.Required() & Must.Be.Number() & Must.Be.GreaterThan(0))
                  & Field("username", Is.Required() & Must.Be.String() & Must.Have.MinLength(2))
                  & Field("email", Is.Required() & Must.Match(@"^[^@]+@[^@]+\.[^@]+$")));

            When("name", Is.Defined())
            .Then(It, Must.Be.String() & Have.MaxLength(256));

            When(Field("company", Is.Defined()) | Field("address", Is.Defined()))
            .Then("address", Is.Required());
            //.Describe("");

            When("address", Is.Defined() & Is.Object())
            .Use <AddressValidator>()
            .For(It);

            When("company", Is.Defined())
            .Then("company.name", Is.Required() & Must.Be.String() & Have.LengthBetween(3, 256));

            When(Any)
            .Then("three", ComparedTo(All("one", "two"), context => Is.EqualTo(context.Sum())));
            When(Any)
            .Then(All("id", "username", "email"), Is.Required());

            When(Any)
            .Use <AddressValidator>()
            .ForEachIn("arr");

            /*
             *
             * {
             * "id": 1,
             * "name": "Leanne Graham",
             * "username": "******",
             * "email": "*****@*****.**",
             * "address":
             * {
             *  "street": "Kulas Light",
             *  "suite": "Apt. 556",
             *  "city": "Gwenborough",
             *  "zipcode": "92998-3874",
             *  "geo": {
             *          "lat": "-37.3159",
             *          "lng": "81.1496"
             *  }
             * },
             * "phone": "1-770-736-8031 x56442",
             * "website": "hildegard.org",
             * "company": {
             *  "name": "Romaguera-Crona",
             *  "catchPhrase": "Multi-layered client-server neural-net",
             *  "bs": "harness real-time e-markets"
             * }
             * }
             *
             */


            //When(Any).Then("test", Is.Required() & Must.Have.LengthBetween(16,32));
            //When("other", x => (string)x == "0", "When other is 0").Then(Field("a", Must.Match("\\w{3}")));
            //Use<ChildValidator>().For("sub");

            //When("soimething", Is.Defined()).Use<ChildValidator>().ForEachIn(It);

            //When("doNotProvideDpg", !Is.EqualTo(true)).Then(Field( "dpg.confirmDPGListOnBoard", (context, token) => !(bool)token,
            //    "Confirmation that a list, manifest or appropriate loading plan, giving details of the dangerous or polluting goods carried, and of their location on the ship, is on board must be provided"));

            //When(Any).Then("depend.min", ComparedTo("depend.max", max => Must.Be.LessThan((int)max)));

            //if (entity["doNotProvideDpg"] != null && !(bool)entity["doNotProvideDpg"] && !(bool)entity["dpg"]?["confirmDPGListOnBoard"])
            //{
            //    ValidationCollector collector = new ValidationCollector();
            //    collector.AddError("Confirmation that a list, manifest or appropriate loading plan, giving details of the dangerous or polluting goods carried, and of their location on the ship, is on board must be provided");
            //    baseResult.Add(new FieldValidationResults("dpg.confirmDPGListOnBoard", entity.SelectToken("dpg.confirmDPGListOnBoard"), collector));
            //}
            //Must.Have.AtLeast(1).Items();

            //When(Any).Then("x", Must.Have.MinLength(3));

            //When(Any).Then("something", Must.Match(token => (bool?)token == true, "somthing must be boolean and true!"));
            //When("name", x => true, "is true").Then(It, Must.Match(x => true, "be true"));
            //When(Field("name", x => true, "is true")).Then(It.MustHave);

            //When(Any).Use<ChildValidator>().For(It);
            //When(Any).Use<ChildValidator>().For("ASDASDASD");
            //When(Any).Use<ChildValidator>().For(Each(""));

            //NOTE: Old Syntax proposal
            //dynamic s = this;
            //s.For("items", s.Use<TestValidator>());
            //s.When("items").Then(s.For("items").Use<TestValidator>());

            //NOTE: Syntax proposal
            //s.Use<TestValidator>().For(Field("x"));
            //s.Use<TestValidator>().For(All("items"));

            //NOTE: Syntax proposal
            //s.When("x", Is.Defined()).Use<TestValidator>().For(It);
            //s.When("x", Is.Defined()).Use<TestValidator>().For(Field("x"));
            //s.When("x", Is.Defined()).Use<TestValidator>().For(All("items"));

            //When(x => true);

            //NOTE: Old Syntax proposal
            //s.ForEach("items",
            //    When("name", Is.LengthBetween(2,5)).Then(It, Must.Match("[A-Z]{2}\\d{3}")),
            //    When("name", Is.LengthBetween(6, 7)).Then(It, Must.Match("[A-Z]{2}\\d{3}\\-Z[1-6]"))
            //    );

            //NOTE: Syntax proposal
            //When(Any).Then(All("", Should.Have.Length(42)));
            //When(Any).Then(Some("", Should.Have.Length(42)));
            //When(Any).Then(None("", Should.Have.Length(42)));

            //When validating content in arrays, the framework should support some additional hooks:
            // - For each item in the array, if the item matches rule A then it must fulfill rule B (contextual validation)
            // - When A contains X elements(or another broad precondition) each element MUST...
            // - Some Elements must...
            // - At least one element must...
            //etc.

            //When("name", Is.Defined()).Then("test", Must.Have.MaxLength(200));
            //When("surname", Is.Defined()).Then("test", Must.Have.MaxLength(25));

            //When(Any).Then("$created", (Must.Be.DateTime() & Must.Be.DateAfter(new DateTime(2000, 1, 1))) | Must.Match("akshdakshd"));

            //When("x", x => (DateTime)x == DateTime.Now, "").Then(Must.Be.DateLessThan(Field("y")));

            //When(Any).Then("$updated", Must.Match(x => x.));

            // When Any, Then A must be greter than field B
            // When Any, Then A compared to B, must be greater than B

            //NOTE: Syntax Sugestion for using one field to validate another in a non-static way.
            //When(Any).Then(Field("$updated", ComparedTo("B", b => Must.Be.GreaterThan(b)) ));

            //When(Field("test", Has.MinLength(5)))
            //    .Then(Field("other", Should.Be.Equal("0"))
            //        | Field("other", Should.Be.Equal("1")));

            //When(Field("test", Has.MinLength(5)))
            //    .Then(Field("other", Should.Be.Equal("0") | Should.Be.Equal("1")));


            //When(Field("A", Is.Defined()) | Field("B", Is.Defined()))
            //    .Then(
            //          Field("A", Must.Be.Equal("") | Must.Be.Equal(""))
            //        & Field("B", Must.Match(".*")));
        }