util library
Helpers for custom rules: the checks and conversions the built-in rules use.
Classes
- SingleFieldMap
- An unmodifiable map with one entry, cheaper than a map literal: for custom rules that validate a value on its own with a field around it.
Functions
-
camelize(
String name) → String -
The camelCase form of an underscore rule name:
min_lengthbecomesminLength. -
codePointLength(
String str) → int -
The number of characters (Unicode code points) of the string. Unlike
String.length, a character outside of the Basic Multilingual Plane, like most emoji, counts once. -
deepCopy(
Object? value) → Object? - A deep copy of maps and lists, other values as they are.
-
isNoValue(
Object? value) → bool -
Whether the value is empty for LIVR: missing (
null) or an empty string. All rules exceptrequired,not_emptyandnot_empty_listskip empty values. -
isPrimitive(
Object? value) → bool - Whether the value is a primitive value: a string, a boolean or a finite number.
-
livrToString(
Object? value) → String -
Converts a primitive value to a string like JavaScript does, so values
are compared the same way in all implementations:
10.0becomes'10'(not'10.0'),-0.0becomes'0'. -
numberArg(
List< Object?> args, int index, String ruleName) → num -
A number argument of a rule at
index: a number or a string with a number. Throws LivrSchemaException otherwise. -
parseNumber(
String str) → num? -
Parses a decimal number: an optional sign, digits with an optional
fraction (
12,-1.5,.5,5.) and an optional exponent (1e3). Leading and trailing ASCII whitespace is allowed. Returns anintfor integers that fit, adoubleotherwise, andnullif the string is not a number. Hexadecimal numbers,InfinityandNaNare not numbers. -
parseReference(
Object? reference, String ruleName) → FieldReference -
Parses a reference to another field for a rule builder:
'name', a field of the object, or'a/b/0', a path down from it through objects and lists, where~1stands for/and~0for~. Throws LivrSchemaException for a reference that is not a string, is empty, has an empty segment (a leading or a trailing/too) or a wrong escape. -
ruleArg(
List< Object?> args, int index, String ruleName) → Object? -
The argument of a rule at
index, for rule builders. Throws LivrSchemaException when the schema does not have it. -
stringArg(
List< Object?> args, int index, String ruleName) → String -
A string argument of a rule at
index. Numbers and booleans are converted with livrToString. Throws LivrSchemaException for other values. -
trimDeep(
Object? value) → Object? - Trims all strings in the value, in nested maps and lists too. Returns the value itself when there is nothing to trim, and a trimmed copy otherwise: the value is never changed.
Typedefs
-
FieldReference
= Object? Function(Map<
Object?, Object?> data) -
Reads the field that a reference points to (LIVR 2.5,
required_ifandwhen) in the object a rule gets asallValues. Returns the input value, ornullwhen the field is missing.