pub struct ArraySchema<S> { /* private fields */ }Expand description
A schema for validating array values.
ArraySchema validates that values are arrays, validates each item against
an item schema, and applies constraints like length and uniqueness. All
validation errors are accumulated rather than short-circuiting on the first failure.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
// Create a schema for an array of strings
let schema = Schema::array(Schema::string().min_len(1))
.non_empty()
.max_len(10);
// Validate an array
let result = schema.validate(&json!(["hello", "world"]), &JsonPath::root());
assert!(result.is_success());
// Empty array fails non_empty constraint
let result = schema.validate(&json!([]), &JsonPath::root());
assert!(result.is_failure());Implementations§
Source§impl<S: SchemaLike> ArraySchema<S>
impl<S: SchemaLike> ArraySchema<S>
Sourcepub fn min_len(self, min: usize) -> Self
pub fn min_len(self, min: usize) -> Self
Adds a minimum length constraint.
The array must have at least min items.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::array(Schema::integer()).min_len(2);
let result = schema.validate(&json!([1, 2, 3]), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!([1]), &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 array must have at most max items.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::array(Schema::integer()).max_len(3);
let result = schema.validate(&json!([1, 2]), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!([1, 2, 3, 4]), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn non_empty(self) -> Self
pub fn non_empty(self) -> Self
Adds a non-empty constraint.
The array must have at least one item. This is a convenience method
equivalent to .min_len(1).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::array(Schema::string()).non_empty();
let result = schema.validate(&json!(["hello"]), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!([]), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn unique(self) -> Self
pub fn unique(self) -> Self
Adds a uniqueness constraint.
All items in the array must be distinct (by JSON equality).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::array(Schema::string()).unique();
let result = schema.validate(&json!(["a", "b", "c"]), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!(["a", "b", "a"]), &JsonPath::root());
assert!(result.is_failure());Sourcepub fn unique_by<F>(self, key_fn: F) -> Self
pub fn unique_by<F>(self, key_fn: F) -> Self
Adds a uniqueness-by-key constraint.
All items in the array must have distinct values for the given key function. This is useful for arrays of objects where you want uniqueness by a specific field.
§Example
use postmortem::{Schema, JsonPath};
use serde_json::{json, Value};
let schema = Schema::array(
Schema::object()
.field("id", Schema::integer())
.field("name", Schema::string())
).unique_by(|item| item.get("id").cloned().unwrap_or(Value::Null));
let result = schema.validate(&json!([
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]), &JsonPath::root());
assert!(result.is_success());
let result = schema.validate(&json!([
{"id": 1, "name": "Alice"},
{"id": 1, "name": "Bob"}
]), &JsonPath::root());
assert!(result.is_failure());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 an array).
§Example
use postmortem::{Schema, JsonPath};
use serde_json::json;
let schema = Schema::array(Schema::string())
.min_len(1)
.error("at least one tag is required");
let result = schema.validate(&json!([]), &JsonPath::root());
// Error message will be "at least one tag is required"Sourcepub fn validate(
&self,
value: &Value,
path: &JsonPath,
) -> Validation<Vec<Value>, SchemaErrors>
pub fn validate( &self, value: &Value, path: &JsonPath, ) -> Validation<Vec<Value>, SchemaErrors>
Validates a value against this schema.
Returns Validation::Success with a Vec<Value> containing the validated
items if all validations pass, or Validation::Failure with all accumulated
errors if any validations fail.
§Validation Process
- Check that the value is an array (type check)
- Check length constraints (min/max)
- Validate each item against the item schema
- Check uniqueness constraints
All errors from all steps are accumulated and returned together.
Trait Implementations§
Source§impl<S: SchemaLike> SchemaLike for ArraySchema<S>
impl<S: SchemaLike> SchemaLike for ArraySchema<S>
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<S: SchemaLike + ToJsonSchema> ToJsonSchema for ArraySchema<S>
impl<S: SchemaLike + ToJsonSchema> ToJsonSchema for ArraySchema<S>
Source§fn to_json_schema(&self) -> Value
fn to_json_schema(&self) -> Value
Auto Trait Implementations§
impl<S> Freeze for ArraySchema<S>where
S: Freeze,
impl<S> !RefUnwindSafe for ArraySchema<S>
impl<S> Send for ArraySchema<S>where
S: Send,
impl<S> Sync for ArraySchema<S>where
S: Sync,
impl<S> Unpin for ArraySchema<S>where
S: Unpin,
impl<S> !UnwindSafe for ArraySchema<S>
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
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