pub struct StringSchema { /* private fields */ }Expand description
A schema for validating string values.
StringSchema validates that values are strings and optionally applies
constraints like minimum/maximum length and regex patterns. All constraint
violations are accumulated rather than short-circuiting on the first failure.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
// Create schema with multiple constraints
let schema = Schema::string()
.min_len(3)
.max_len(20)
.pattern(r"^[a-z]+$")
.unwrap();
// Validation accumulates all errors
let result = schema.validate(&json!("AB"), &JsonPath::root());
assert!(result.is_failure());
// Will report both: too short AND pattern mismatchImplementations§
Source§impl StringSchema
impl StringSchema
Sourcepub fn min_len(self, min: usize) -> Self
pub fn min_len(self, min: usize) -> Self
Adds a minimum length constraint.
The string must have at least min characters (Unicode scalar values).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::string().min_len(5);
let result = schema.validate(&json!("hello"), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!("hi"), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn max_len(self, max: usize) -> Self
pub fn max_len(self, max: usize) -> Self
Adds a maximum length constraint.
The string must have at most max characters (Unicode scalar values).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::string().max_len(10);
let result = schema.validate(&json!("hello"), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!("this is too long"), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn pattern(self, pattern: &str) -> Result<Self, Error>
pub fn pattern(self, pattern: &str) -> Result<Self, Error>
Adds a regex pattern constraint.
The string must match the provided regex pattern. Returns an error if the regex pattern is invalid.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::string()
.pattern(r"^\d+$")
.unwrap();
let result = schema.validate(&json!("12345"), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!("abc"), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn starts_with(self, prefix: impl Into<String>) -> Self
pub fn starts_with(self, prefix: impl Into<String>) -> Self
Adds a starts-with constraint.
Sourcepub fn error(self, message: impl Into<String>) -> Self
pub fn error(self, message: impl Into<String>) -> Self
Sets a custom error message for the most recent constraint.
If no constraints have been added yet, this sets the type error message (used when the value is not a string).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::string()
.min_len(5)
.error("username must be at least 5 characters");
let result = schema.validate(&json!("hi"), &JsonPath::root());
// Error message will be "username must be at least 5 characters"Sourcepub fn validate(
&self,
value: &Value,
path: &JsonPath,
) -> Validation<String, SchemaErrors>
pub fn validate( &self, value: &Value, path: &JsonPath, ) -> Validation<String, SchemaErrors>
Validates a value against this schema.
Returns Validation::Success with the validated string if all
constraints pass, or Validation::Failure with all accumulated
errors if any constraints fail.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::string().min_len(1).max_len(10);
match schema.validate(&json!("hello"), &JsonPath::root()) {
stillwater::Validation::Success(s) => println!("Valid: {}", s),
stillwater::Validation::Failure(errors) => {
for error in errors.iter() {
println!("Error: {}", error);
}
}
}Trait Implementations§
Source§impl Clone for StringSchema
impl Clone for StringSchema
Source§fn clone(&self) -> StringSchema
fn clone(&self) -> StringSchema
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for StringSchema
impl Default for StringSchema
Source§impl SchemaLike for StringSchema
impl SchemaLike for StringSchema
Source§fn validate(
&self,
value: &Value,
path: &JsonPath,
) -> Validation<Self::Output, SchemaErrors>
fn validate( &self, value: &Value, path: &JsonPath, ) -> Validation<Self::Output, SchemaErrors>
Source§fn validate_to_value(
&self,
value: &Value,
path: &JsonPath,
) -> Validation<Value, SchemaErrors>
fn validate_to_value( &self, value: &Value, path: &JsonPath, ) -> Validation<Value, SchemaErrors>
serde_json::Value. Read moreSource§fn validate_with_context(
&self,
value: &Value,
path: &JsonPath,
_context: &ValidationContext,
) -> Validation<Self::Output, SchemaErrors>
fn validate_with_context( &self, value: &Value, path: &JsonPath, _context: &ValidationContext, ) -> Validation<Self::Output, SchemaErrors>
Source§fn validate_to_value_with_context(
&self,
value: &Value,
path: &JsonPath,
_context: &ValidationContext,
) -> Validation<Value, SchemaErrors>
fn validate_to_value_with_context( &self, value: &Value, path: &JsonPath, _context: &ValidationContext, ) -> Validation<Value, SchemaErrors>
serde_json::Value. Read moreSource§impl ToJsonSchema for StringSchema
impl ToJsonSchema for StringSchema
Source§fn to_json_schema(&self) -> Value
fn to_json_schema(&self) -> Value
Auto Trait Implementations§
impl Freeze for StringSchema
impl !RefUnwindSafe for StringSchema
impl Send for StringSchema
impl Sync for StringSchema
impl Unpin for StringSchema
impl !UnwindSafe for StringSchema
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<S> ValueValidator for Swhere
S: SchemaLike + ToJsonSchema,
impl<S> ValueValidator for Swhere
S: SchemaLike + ToJsonSchema,
Source§fn validate_value(
&self,
value: &Value,
path: &JsonPath,
) -> Validation<Value, SchemaErrors>
fn validate_value( &self, value: &Value, path: &JsonPath, ) -> Validation<Value, SchemaErrors>
serde_json::Value.Source§fn validate_value_with_context(
&self,
value: &Value,
path: &JsonPath,
context: &ValidationContext,
) -> Validation<Value, SchemaErrors>
fn validate_value_with_context( &self, value: &Value, path: &JsonPath, context: &ValidationContext, ) -> Validation<Value, SchemaErrors>
serde_json::Value. Read more