2022-05-14 12:35:08 +00:00
|
|
|
use crate::ctx::Context;
|
2022-01-13 17:37:46 +00:00
|
|
|
use crate::err::Error;
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use regex::Regex;
|
2022-09-01 17:46:36 +00:00
|
|
|
use semver::Version;
|
2022-01-13 17:37:46 +00:00
|
|
|
use std::char;
|
2022-09-01 17:46:36 +00:00
|
|
|
use uuid::Uuid;
|
2022-01-13 17:37:46 +00:00
|
|
|
|
|
|
|
#[rustfmt::skip] static LATITUDE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap());
|
|
|
|
#[rustfmt::skip] static LONGITUDE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$").unwrap());
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn alphanum(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().chars().all(char::is_alphanumeric).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn alpha(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().chars().all(char::is_alphabetic).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn ascii(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-09-01 00:27:52 +00:00
|
|
|
Ok(args.remove(0).as_string().is_ascii().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn domain(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-09-01 17:46:36 +00:00
|
|
|
Ok(addr::parse_domain_name(args.remove(0).as_string().as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn email(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-09-01 17:46:36 +00:00
|
|
|
Ok(addr::parse_email_address(args.remove(0).as_string().as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn hexadecimal(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().chars().all(|x| char::is_ascii_hexdigit(&x)).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn latitude(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(LATITUDE_RE.is_match(args.remove(0).as_string().as_str()).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn longitude(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(LONGITUDE_RE.is_match(args.remove(0).as_string().as_str()).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn numeric(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-05-05 04:30:32 +00:00
|
|
|
Ok(args.remove(0).as_string().chars().all(char::is_numeric).into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn semver(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-09-01 17:46:36 +00:00
|
|
|
Ok(Version::parse(args.remove(0).as_string().as_str()).is_ok().into())
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 17:46:36 +00:00
|
|
|
#[inline]
|
2022-05-14 12:35:08 +00:00
|
|
|
pub fn uuid(_: &Context, mut args: Vec<Value>) -> Result<Value, Error> {
|
2022-09-18 13:08:23 +00:00
|
|
|
Ok(match args.remove(0) {
|
|
|
|
Value::Strand(v) => Uuid::parse_str(v.as_string().as_str()).is_ok().into(),
|
|
|
|
Value::Uuid(_) => true.into(),
|
|
|
|
_ => false.into(),
|
|
|
|
})
|
2022-09-01 17:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::sql::value::Value;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alphanum() {
|
|
|
|
let value = super::alphanum(&Default::default(), vec!["abc123".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::alphanum(&Default::default(), vec!["y%*".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alpha() {
|
|
|
|
let value = super::alpha(&Default::default(), vec!["abc".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::alpha(&Default::default(), vec!["1234".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ascii() {
|
|
|
|
let value = super::ascii(&Default::default(), vec!["abc".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::ascii(&Default::default(), vec!["中国".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn domain() {
|
|
|
|
let value = super::domain(&Default::default(), vec!["食狮.中国".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::domain(&Default::default(), vec!["example-.com".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn email() {
|
|
|
|
let input = vec!["user@[fd79:cdcb:38cc:9dd:f686:e06d:32f3:c123]".into()];
|
|
|
|
let value = super::email(&Default::default(), input).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let input = vec!["john..doe@example.com".into()];
|
|
|
|
let value = super::email(&Default::default(), input).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hexadecimal() {
|
|
|
|
let value = super::hexadecimal(&Default::default(), vec!["00FF00".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::hexadecimal(&Default::default(), vec!["SurrealDB".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn latitude() {
|
|
|
|
let value = super::latitude(&Default::default(), vec!["-0.118092".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::latitude(&Default::default(), vec![12345.into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn longitude() {
|
|
|
|
let value = super::longitude(&Default::default(), vec!["51.509865".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::longitude(&Default::default(), vec![12345.into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn numeric() {
|
|
|
|
let value = super::numeric(&Default::default(), vec![12345.into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::numeric(&Default::default(), vec!["abcde".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn semver() {
|
|
|
|
let value = super::semver(&Default::default(), vec!["1.0.0".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let value = super::semver(&Default::default(), vec!["1.0".into()]).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uuid() {
|
|
|
|
let input = vec!["123e4567-e89b-12d3-a456-426614174000".into()];
|
|
|
|
let value = super::uuid(&Default::default(), input).unwrap();
|
|
|
|
assert_eq!(value, Value::True);
|
|
|
|
|
|
|
|
let input = vec!["foo-bar".into()];
|
|
|
|
let value = super::uuid(&Default::default(), input).unwrap();
|
|
|
|
assert_eq!(value, Value::False);
|
|
|
|
}
|
2022-01-13 17:37:46 +00:00
|
|
|
}
|