|
| 1 | +import '../localization/l10n.dart'; |
| 2 | +import 'constants.dart'; |
| 3 | + |
| 4 | +Validator<T> containsElement<T extends Object>( |
| 5 | + List<T> values, { |
| 6 | + String? containsElementMsg, |
| 7 | +}) { |
| 8 | + return (T value) { |
| 9 | + return values.contains(value) |
| 10 | + ? null |
| 11 | + : containsElementMsg ?? |
| 12 | + FormBuilderLocalizations.current.containsElementErrorText; |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +Validator<T> isEqual<T extends Object?>(T value, |
| 17 | + {String Function(String)? equalMsg}) { |
| 18 | + return (input) { |
| 19 | + final valueString = value.toString(); |
| 20 | + return value == input |
| 21 | + ? null |
| 22 | + : equalMsg?.call(valueString) ?? |
| 23 | + FormBuilderLocalizations.current.equalErrorText(valueString); |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +/// Returns a [Validator] function that checks if its `T` input is a `true` |
| 28 | +/// boolean or a [String] parsable to a `true` boolean. If the input satisfies |
| 29 | +/// this condition, the validator returns `null`. Otherwise, it returns the |
| 30 | +/// default error message |
| 31 | +/// `FormBuilderLocalizations.current.mustBeTrueErrorText`, if [isTrueMsg] |
| 32 | +/// is not provided. |
| 33 | +/// |
| 34 | +/// # Parsing rules |
| 35 | +/// If the input of the validator is a [String], it will be parsed using |
| 36 | +/// the rules specified by the combination of [caseSensitive] and [trim]. |
| 37 | +/// [caseSensitive] indicates whether the lowercase must be considered equal to |
| 38 | +/// uppercase or not, and [trim] indicates whether whitespaces from both sides |
| 39 | +/// of the string will be ignored or not. |
| 40 | +/// |
| 41 | +/// The default behavior is to ignore leading and trailing whitespaces and be |
| 42 | +/// case insensitive. |
| 43 | +/// |
| 44 | +/// # Examples |
| 45 | +/// ```dart |
| 46 | +/// // The following examples must pass: |
| 47 | +/// assert(isTrue()(' true ') == null); |
| 48 | +/// assert(isTrue(trim:false)(' true ') != null); |
| 49 | +/// assert(isTrue()('TRue') == null); |
| 50 | +/// assert(isTrue(caseSensitive:true)('TRue') != null); |
| 51 | +/// ``` |
| 52 | +Validator<T> isTrue<T extends Object>( |
| 53 | + {String? isTrueMsg, bool caseSensitive = false, bool trim = true}) { |
| 54 | + return (value) { |
| 55 | + final (isValid, typeTransformedValue) = _isBoolValidateAndConvert( |
| 56 | + value, |
| 57 | + caseSensitive: caseSensitive, |
| 58 | + trim: trim, |
| 59 | + ); |
| 60 | + if (isValid && typeTransformedValue! == true) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + return isTrueMsg ?? FormBuilderLocalizations.current.mustBeTrueErrorText; |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/// Returns a [Validator] function that checks if its `T` input is a `false` |
| 68 | +/// boolean or a [String] parsable to a `false` boolean. If the input satisfies |
| 69 | +/// this condition, the validator returns `null`. Otherwise, it returns the |
| 70 | +/// default error message |
| 71 | +/// `FormBuilderLocalizations.current.mustBeFalseErrorText`, if [isFalseMsg] |
| 72 | +/// is not provided. |
| 73 | +/// |
| 74 | +/// # Parsing rules |
| 75 | +/// If the input of the validator is a [String], it will be parsed using |
| 76 | +/// the rules specified by the combination of [caseSensitive] and [trim]. |
| 77 | +/// [caseSensitive] indicates whether the lowercase must be considered equal to |
| 78 | +/// uppercase or not, and [trim] indicates whether whitespaces from both sides |
| 79 | +/// of the string will be ignored or not. |
| 80 | +/// |
| 81 | +/// The default behavior is to ignore leading and trailing whitespaces and be |
| 82 | +/// case insensitive. |
| 83 | +/// |
| 84 | +/// # Examples |
| 85 | +/// ```dart |
| 86 | +/// // The following examples must pass: |
| 87 | +/// assert(isFalse()(' false ') == null); |
| 88 | +/// assert(isFalse(trim:false)(' false ') != null); |
| 89 | +/// assert(isFalse()('FAlse') == null); |
| 90 | +/// assert(isFalse(caseSensitive:true)('FAlse') != null); |
| 91 | +/// ``` |
| 92 | +Validator<T> isFalse<T extends Object>( |
| 93 | + {String? isFalseMsg, bool caseSensitive = false, bool trim = true}) { |
| 94 | + return (value) { |
| 95 | + final (isValid, typeTransformedValue) = _isBoolValidateAndConvert( |
| 96 | + value, |
| 97 | + caseSensitive: caseSensitive, |
| 98 | + trim: trim, |
| 99 | + ); |
| 100 | + if (isValid && typeTransformedValue! == false) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + return isFalseMsg ?? FormBuilderLocalizations.current.mustBeFalseErrorText; |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +(bool, bool?) _isBoolValidateAndConvert<T extends Object>(T value, |
| 108 | + {bool caseSensitive = false, bool trim = true}) { |
| 109 | + if (value is bool) { |
| 110 | + return (true, value); |
| 111 | + } |
| 112 | + if (value is String) { |
| 113 | + final bool? candidateValue = bool.tryParse(trim ? value.trim() : value, |
| 114 | + caseSensitive: caseSensitive); |
| 115 | + if (candidateValue != null) { |
| 116 | + return (true, candidateValue); |
| 117 | + } |
| 118 | + } |
| 119 | + return (false, null); |
| 120 | +} |
0 commit comments