utopia-validators 0.1.1

The checks of Utopia's request validators: booleans, integers, floats, numbers, ranges, ports, hex colors, text, identifiers, white lists, substrings, DNS labels, hostnames, domains, hosts, IP addresses, URLs, phone numbers, JSON, FCM service accounts, globstar patterns and the validators that compose others.
Documentation

utopia-validators

The checks of utopia-php/validators as PHP runs them: booleans, integers, floats, numbers, ranges, ports, hex colors, text, identifiers, white lists, substrings, DNS labels, hostnames, domains, hosts, IP addresses, URLs and phone numbers (over php-std's ports of filter_var(), parse_url() and Uri\Rfc3986\Uri). Each function is the body of a PHP validator's method, over a value as PHP sees it (Input), with the same coercions (is_numeric(), $value + 0), comparisons and messages. The validator classes, and the state their constructors keep, live in the PHP extension (crates/extension), which passes it in.

use utopia_validators::{Input, Integer, Number, range};

let int8 = Integer::new(8, false).unwrap();
assert!(int8.accepts(true, Input::Str(b" 127 ")));
assert!(!int8.accepts(true, Input::Float(1.0)));
assert!(range(Number::Int(1), Number::Int(10), b"integer", Input::Str(b"5")));

A Rust caller that wants the value back in its type uses a Validator instead: Boolean, Integer, Float, Range, Port, Text, Hostname and WhiteList run the same checks and return what they checked, accepting a value in its own type or as its text.

use utopia_validators::{Input, Port, Range, Validator};

assert_eq!(Port.validate(Input::Str(b"6379")), Some(6379));
assert_eq!(Range::new(0, 10).validate(Input::Int(11)), None);
assert_eq!(Range::new(0, 10).description(), "Value must be a valid range between 0 and 10");