Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-validator

Typed fluent validation rules for Pudu

0.1.1Apache-2.01

InstallClose

Ordering.pudu

Pudu96 lines5.0 KB

GitHub ↗
1/** @Validation.Rules.Ordering.Module — checks ordered property bounds */2module PuduLangValidator.Rules.Ordering34import Std.Order {Ord}5import PuduLangValidator.Rule as Rule67/// Require a property strictly after the bound.8export fn greaterThan[T, V: Ord](builder: Rule.RuleBuilder[T, V], bound: V) -> Rule.RuleBuilder[T, V] {9  Rule.withArgument(Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| bound.before(&value)),10        "Value must be greater than \{ComparisonValue\}."), "GreaterThan"), "ComparisonValue", bound.toText())11}1213/// Require a property at or after the bound.14export fn greaterThanOrEqualTo[T, V: Ord](builder: Rule.RuleBuilder[T, V], bound: V) -> Rule.RuleBuilder[T, V] {15  Rule.withArgument(Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| !value.before(&bound)),16        "Value must be at least \{ComparisonValue\}."), "GreaterThanOrEqualTo"), "ComparisonValue", bound.toText())17}1819/// Require a property strictly before the bound.20export fn lessThan[T, V: Ord](builder: Rule.RuleBuilder[T, V], bound: V) -> Rule.RuleBuilder[T, V] {21  Rule.withArgument(Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| value.before(&bound)),22        "Value must be less than \{ComparisonValue\}."), "LessThan"), "ComparisonValue", bound.toText())23}2425/// Require a property at or before the bound.26export fn lessThanOrEqualTo[T, V: Ord](builder: Rule.RuleBuilder[T, V], bound: V) -> Rule.RuleBuilder[T, V] {27  Rule.withArgument(Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| !bound.before(&value)),28        "Value must be at most \{ComparisonValue\}."), "LessThanOrEqualTo"), "ComparisonValue", bound.toText())29}3031/// Require a property strictly after another selected property.32export fn greaterThanProperty[T, V: Ord](33  builder: Rule.RuleBuilder[T, V], path: Str, select: fn(T) -> V34) -> Rule.RuleBuilder[T, V] {35  let checked = Rule.withCode(Rule.withMessage(Rule.mustWithArguments(builder,36        fn(root: T, value: V) -> Option[Array[(Str, Str)]] {37          let bound = select(root)38          if bound.before(&value) { None } else { Some([("ComparisonValue", bound.toText())]) }39        }),40      "Value must be greater than \{ComparisonValue\} from \{ComparisonProperty\}."), "GreaterThan")41  Rule.withArgument(checked, "ComparisonProperty", path)42}4344/// Require a property at or after another selected property.45export fn greaterThanOrEqualToProperty[T, V: Ord](46  builder: Rule.RuleBuilder[T, V], path: Str, select: fn(T) -> V47) -> Rule.RuleBuilder[T, V] {48  let checked = Rule.withCode(Rule.withMessage(Rule.mustWithArguments(builder,49        fn(root: T, value: V) -> Option[Array[(Str, Str)]] {50          let bound = select(root)51          if !value.before(&bound) { None } else { Some([("ComparisonValue", bound.toText())]) }52        }),53      "Value must be at least \{ComparisonValue\} from \{ComparisonProperty\}."), "GreaterThanOrEqualTo")54  Rule.withArgument(checked, "ComparisonProperty", path)55}5657/// Require a property strictly before another selected property.58export fn lessThanProperty[T, V: Ord](59  builder: Rule.RuleBuilder[T, V], path: Str, select: fn(T) -> V60) -> Rule.RuleBuilder[T, V] {61  let checked = Rule.withCode(Rule.withMessage(Rule.mustWithArguments(builder,62        fn(root: T, value: V) -> Option[Array[(Str, Str)]] {63          let bound = select(root)64          if value.before(&bound) { None } else { Some([("ComparisonValue", bound.toText())]) }65        }),66      "Value must be less than \{ComparisonValue\} from \{ComparisonProperty\}."), "LessThan")67  Rule.withArgument(checked, "ComparisonProperty", path)68}6970/// Require a property at or before another selected property.71export fn lessThanOrEqualToProperty[T, V: Ord](72  builder: Rule.RuleBuilder[T, V], path: Str, select: fn(T) -> V73) -> Rule.RuleBuilder[T, V] {74  let checked = Rule.withCode(Rule.withMessage(Rule.mustWithArguments(builder,75        fn(root: T, value: V) -> Option[Array[(Str, Str)]] {76          let bound = select(root)77          if !bound.before(&value) { None } else { Some([("ComparisonValue", bound.toText())]) }78        }),79      "Value must be at most \{ComparisonValue\} from \{ComparisonProperty\}."), "LessThanOrEqualTo")80  Rule.withArgument(checked, "ComparisonProperty", path)81}8283/// Require a property from low through high, including endpoints.84export fn inclusiveBetween[T, V: Ord](builder: Rule.RuleBuilder[T, V], low: V, high: V) -> Rule.RuleBuilder[T, V] {85  let checked = Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| !value.before(&low) && !high.before(&value)),86      "Value must be from \{From\} through \{To\}."), "InclusiveBetween")87  Rule.withArgument(Rule.withArgument(checked, "From", low.toText()), "To", high.toText())88}8990/// Require a property strictly between low and high.91export fn exclusiveBetween[T, V: Ord](builder: Rule.RuleBuilder[T, V], low: V, high: V) -> Rule.RuleBuilder[T, V] {92  let checked = Rule.withCode(Rule.withMessage(Rule.must(builder, |value: V| low.before(&value) && value.before(&high)),93      "Value must be strictly between \{From\} and \{To\}."), "ExclusiveBetween")94  Rule.withArgument(Rule.withArgument(checked, "From", low.toText()), "To", high.toText())95}96