schema: add basic api types feature

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-01-31 13:46:37 +01:00
parent f813e8d866
commit a8bd8fca15
3 changed files with 33 additions and 0 deletions

View File

@ -33,6 +33,7 @@ default = []
api-macro = ["dep:proxmox-api-macro"]
upid-api-impl = [ "dep:libc", "dep:nix" ]
api-types = []
# Testing only
test-harness = []

View File

@ -0,0 +1,28 @@
//! The "basic" api types we generally require along with some of their macros.
use crate::{ApiStringFormat, Schema, StringSchema};
#[rustfmt::skip]
#[macro_export]
macro_rules! SAFE_ID_REGEX_STR { () => { r"(?:[A-Za-z0-9_][A-Za-z0-9._\-]*)" }; }
const_regex! {
/// Regex for safe identifiers.
///
/// This
/// [article](https://dwheeler.com/essays/fixing-unix-linux-filenames.html)
/// contains further information why it is reasonable to restict
/// names this way. This is not only useful for filenames, but for
/// any identifier command line tools work with.
pub SAFE_ID_REGEX = concat!(r"^", SAFE_ID_REGEX_STR!(), r"$");
pub PASSWORD_REGEX = r"^[[:^cntrl:]]*$"; // everything but control characters
}
pub const SAFE_ID_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&SAFE_ID_REGEX);
pub const PASSWORD_FORMAT: ApiStringFormat = ApiStringFormat::Pattern(&PASSWORD_REGEX);
pub const PASSWORD_SCHEMA: Schema = StringSchema::new("Password.")
.format(&PASSWORD_FORMAT)
.min_length(1)
.max_length(1024)
.schema();

View File

@ -13,6 +13,7 @@ pub use proxmox_api_macro::api;
mod api_type_macros;
#[macro_use]
mod const_regex;
pub use const_regex::ConstRegexPattern;
@ -33,3 +34,6 @@ pub mod upid;
pub mod semver_exempt {
pub use lazy_static::lazy_static;
}
#[cfg(feature = "api-types")]
pub mod api_types;