public async Task Multi_Common_Validator() { var cv1 = CommonValidator.Create <string>(v => v.String(5).Must(x => x.Value != "XXXXX")); var cv2 = CommonValidator.Create <string>(v => v.String(2).Must(x => x.Value != "YYY")); var vx = Validator.Create <TestItem>() .HasProperty(x => x.Code, p => p.Common(cv2)) .HasProperty(x => x.Text, p => p.Common(cv1)); var r = await vx.ValidateAsync(new TestItem { Code = "YYY", Text = "XXXXX" }); Assert.IsTrue(r.HasErrors); Assert.AreEqual(2, r.Messages.Count); }
public async Task Dict_Validator_KeyError() { var kv = CommonValidator.Create <string>(x => x.Text("Key").Mandatory().String(2)); var vxd = Validator.CreateDictionary <Dictionary <string, TestItem>, string, TestItem>(minCount: 2, item: DictionaryRuleItem.Create(key: kv, value: new TestItemValidator())); var tc = new Dictionary <string, TestItem> { { "k1", new TestItem { Code = "A", Text = "A" } }, { "k2x", new TestItem { Code = "B", Text = "B" } } }; var r = await vxd.ValidateAsync(tc); Assert.IsTrue(r.HasErrors); Assert.AreEqual(1, r.Messages.Count); Assert.AreEqual(MessageType.Error, r.Messages[0].Type); Assert.AreEqual("Key must not exceed 2 characters in length.", r.Messages[0].Text); Assert.AreEqual("Value[k2x]", r.Messages[0].Property); }
public async Task Entity_ValueOverrideAndDefault() { var vc = CommonValidator.Create <decimal>(v => v.Default(100)); var ti = new TestData { Text = "ABC", CountA = 1 }; var vx = await Validator.Create <TestData>() .HasProperty(x => x.Text, p => p.Override("XYZ")) .HasProperty(x => x.CountA, p => p.Default(x => 10)) .HasProperty(x => x.CountB, p => p.Default(x => 20)) .HasProperty(x => x.AmountA, p => p.Common(vc)) .ValidateAsync(ti); Assert.IsFalse(vx.HasErrors); Assert.AreEqual(0, vx.Messages.Count); Assert.AreEqual("XYZ", ti.Text); Assert.AreEqual(1, ti.CountA); Assert.AreEqual(20, ti.CountB); Assert.AreEqual(100, ti.AmountA); }
public PersonArgsValidator() { Property(x => x.FirstName).Common(CommonValidator.Create <string?>(v => v.String(50))); Property(x => x.LastName).Common(CommonValidator.Create <string?>(v => v.String(50))); //Property(x => x.Genders).AreValid(); }