Skip to content

BizArk.Core.DataAnnotations.CustomAttribute

Brian Brewder edited this page Apr 1, 2017 · 1 revision

Back to BizArk.Core API Reference

Allows for custom validation without having to implement a validation attribute.

Constructors

  • public CustomAttribute(Func<object, bool> validate)

Properties

  • public Func<object, bool> Validator { get; } - Gets the function used to validate the value.

Methods

  • public override bool IsValid(object value) - Checks that the value of the data field is valid.

Examples

var validator = new CustomAttribute((val) =>
{
	// Leave null and invalid type checks to other rules.
	if (val == null) return true;
	if (!(val is int)) return true;

	var nbr = (int)val;

	// Super awesome validation rules here.
	if (nbr == 123) return false;
	return true;
});

// Prints "Is Valid: False"
Console.WriteLine($"Is Valid: {validator.IsValid(123)}");

Clone this wiki locally