ArraySchema

Struct ArraySchema 

Source
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>

Source

pub fn new(item_schema: S) -> Self

Creates a new array schema with the given item schema.

Source

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());
Source

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());
Source

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());
Source

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());
Source

pub fn unique_by<F>(self, key_fn: F) -> Self
where F: Fn(&Value) -> Value + Send + Sync + 'static,

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());
Source

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"
Source

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
  1. Check that the value is an array (type check)
  2. Check length constraints (min/max)
  3. Validate each item against the item schema
  4. Check uniqueness constraints

All errors from all steps are accumulated and returned together.

Trait Implementations§

Source§

impl<S: SchemaLike> SchemaLike for ArraySchema<S>

Source§

type Output = Vec<Value>

The output type produced by successful validation.
Source§

fn validate( &self, value: &Value, path: &JsonPath, ) -> Validation<Self::Output, SchemaErrors>

Validates a value against this schema. Read more
Source§

fn validate_to_value( &self, value: &Value, path: &JsonPath, ) -> Validation<Value, SchemaErrors>

Validates a value and returns the result as a serde_json::Value. Read more
Source§

fn validate_with_context( &self, value: &Value, path: &JsonPath, context: &ValidationContext, ) -> Validation<Self::Output, SchemaErrors>

Validates a value with registry context for schema reference resolution. Read more
Source§

fn validate_to_value_with_context( &self, value: &Value, path: &JsonPath, context: &ValidationContext, ) -> Validation<Value, SchemaErrors>

Validates a value with context and returns the result as a serde_json::Value. Read more
Source§

fn collect_refs(&self, refs: &mut Vec<String>)

Collects all schema reference names used by this schema. Read more
Source§

impl<S: SchemaLike + ToJsonSchema> ToJsonSchema for ArraySchema<S>

Source§

fn to_json_schema(&self) -> Value

Converts this schema to a JSON Schema representation. Read more

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S> ValueValidator for S

Source§

fn validate_value( &self, value: &Value, path: &JsonPath, ) -> Validation<Value, SchemaErrors>

Validates a value and returns the result as a serde_json::Value.
Source§

fn validate_value_with_context( &self, value: &Value, path: &JsonPath, context: &ValidationContext, ) -> Validation<Value, SchemaErrors>

Validates a value with context and returns the result as a serde_json::Value. Read more
Source§

fn collect_refs(&self, refs: &mut Vec<String>)

Collects schema reference names. Read more
Source§

fn to_json_schema(&self) -> Value

Converts this schema to JSON Schema format. Read more