Schema

Enum Schema 

Source
pub enum Schema {
    String {
        min_length: Option<usize>,
        max_length: Option<usize>,
        pattern: Option<String>,
        allowed_values: Option<SmallVec<[String; 8]>>,
    },
    Integer {
        minimum: Option<i64>,
        maximum: Option<i64>,
    },
    Number {
        minimum: Option<f64>,
        maximum: Option<f64>,
    },
    Boolean,
    Null,
    Array {
        items: Option<Box<Schema>>,
        min_items: Option<usize>,
        max_items: Option<usize>,
        unique_items: bool,
    },
    Object {
        properties: HashMap<String, Schema>,
        required: Vec<String>,
        additional_properties: bool,
    },
    OneOf {
        schemas: SmallVec<[Box<Schema>; 4]>,
    },
    AllOf {
        schemas: SmallVec<[Box<Schema>; 4]>,
    },
    Any,
}
Expand description

JSON Schema representation for validation

Supports a practical subset of JSON Schema Draft 2020-12 focused on validation needs for streaming JSON data.

§Design Philosophy

  • Focused on validation, not full JSON Schema specification
  • Performance-oriented with pre-compiled validation rules
  • Zero-copy where possible using Arc for shared data
  • Type-safe with strongly-typed enum variants

§Examples

let schema = Schema::Object {
    properties: vec![
        ("id".to_string(), Schema::Integer { minimum: Some(1), maximum: None }),
        ("name".to_string(), Schema::String {
            min_length: Some(1),
            max_length: Some(100),
            pattern: None,
            allowed_values: None,
        }),
    ].into_iter().collect(),
    required: vec!["id".to_string(), "name".to_string()],
    additional_properties: false,
};

Variants§

§

String

String type with optional constraints

Fields

§min_length: Option<usize>

Minimum string length (inclusive)

§max_length: Option<usize>

Maximum string length (inclusive)

§pattern: Option<String>

Pattern to match (regex)

§allowed_values: Option<SmallVec<[String; 8]>>

Enumeration of allowed values

§

Integer

Integer type with optional range constraints

Fields

§minimum: Option<i64>

Minimum value (inclusive)

§maximum: Option<i64>

Maximum value (inclusive)

§

Number

Number type (float/double) with optional range constraints

Fields

§minimum: Option<f64>

Minimum value (inclusive)

§maximum: Option<f64>

Maximum value (inclusive)

§

Boolean

Boolean type (no constraints)

§

Null

Null type (no constraints)

§

Array

Array type with element schema and size constraints

Fields

§items: Option<Box<Schema>>

Schema for array elements (None = any type)

§min_items: Option<usize>

Minimum array length (inclusive)

§max_items: Option<usize>

Maximum array length (inclusive)

§unique_items: bool

Whether all items must be unique

§

Object

Object type with property schemas

Fields

§properties: HashMap<String, Schema>

Property name to schema mapping

§required: Vec<String>

List of required property names

§additional_properties: bool

Whether additional properties are allowed

§

OneOf

Union type (one of multiple schemas)

Fields

§schemas: SmallVec<[Box<Schema>; 4]>

List of possible schemas

§

AllOf

Intersection type (all of multiple schemas)

Fields

§schemas: SmallVec<[Box<Schema>; 4]>

List of schemas that must all match

§

Any

Any type (no validation)

Implementations§

Source§

impl Schema

Source

pub fn allows_type(&self, schema_type: SchemaType) -> bool

Check if schema allows a specific type

Used for quick type compatibility checks before full validation.

§Arguments
  • schema_type - The type to check compatibility for
§Returns

true if the schema allows the type, false otherwise

Source

pub fn validation_cost(&self) -> usize

Get estimated validation cost for performance optimization

Higher cost indicates more expensive validation operations. Used by validation scheduler to optimize validation order.

§Returns

Validation cost estimate (0-1000 range)

Source

pub fn string(min_length: Option<usize>, max_length: Option<usize>) -> Schema

Create a simple string schema with length constraints

Source

pub fn integer(minimum: Option<i64>, maximum: Option<i64>) -> Schema

Create a simple integer schema with range constraints

Source

pub fn number(minimum: Option<f64>, maximum: Option<f64>) -> Schema

Create a simple number schema with range constraints

Source

pub fn array(items: Option<Schema>) -> Schema

Create an array schema with item type

Source

pub fn object( properties: HashMap<String, Schema>, required: Vec<String>, ) -> Schema

Create an object schema with properties

Trait Implementations§

Source§

impl Clone for Schema

Source§

fn clone(&self) -> Schema

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Schema

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Schema, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&Schema> for SchemaDefinitionDto

Source§

fn from(schema: &Schema) -> Self

Converts to this type from the input type.
Source§

impl From<&Schema> for SchemaType

Source§

fn from(schema: &Schema) -> SchemaType

Converts to this type from the input type.
Source§

impl From<SchemaDefinitionDto> for Schema

Source§

fn from(dto: SchemaDefinitionDto) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Schema

Source§

fn eq(&self, other: &Schema) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Schema

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Schema

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,