[][src]Struct schemars::gen::SchemaGenerator

pub struct SchemaGenerator { /* fields omitted */ }

The main type used to generate JSON Schemas.

Example

use schemars::{JsonSchema, gen::SchemaGenerator};

#[derive(JsonSchema)]
struct MyStruct {
    foo: i32,
}

let gen = SchemaGenerator::default();
let schema = gen.into_root_schema_for::<MyStruct>();

Implementations

impl SchemaGenerator[src]

pub fn new(settings: SchemaSettings) -> SchemaGenerator[src]

Creates a new SchemaGenerator using the given settings.

pub fn settings(&self) -> &SchemaSettings[src]

Borrows the SchemaSettings being used by this SchemaGenerator.

Example

use schemars::gen::SchemaGenerator;

let gen = SchemaGenerator::default();
let settings = gen.settings();

assert_eq!(settings.option_add_null_type, true);

pub fn make_extensible(&self, schema: &mut SchemaObject)[src]

Modifies the given SchemaObject so that it may have validation, metadata or other properties set on it.

If schema is not a $ref schema, then this does not modify schema. Otherwise, depending on this generator's settings, this may wrap the $ref in another schema. This is required because in many JSON Schema implementations, a schema with $ref set may not include other properties.

Example

use schemars::{gen::SchemaGenerator, schema::SchemaObject};

let gen = SchemaGenerator::default();

let ref_schema = SchemaObject::new_ref("foo".to_owned());
assert!(ref_schema.is_ref());

let mut extensible_schema = ref_schema.clone();
gen.make_extensible(&mut extensible_schema);
assert_ne!(ref_schema, extensible_schema);
assert!(!extensible_schema.is_ref());

let mut extensible_schema2 = extensible_schema.clone();
gen.make_extensible(&mut extensible_schema);
assert_eq!(extensible_schema, extensible_schema2);

pub fn schema_for_any(&self) -> Schema[src]

Returns a Schema that matches everything, such as the empty schema {}.

The exact value returned depends on this generator's BoolSchemas setting.

pub fn schema_for_none(&self) -> Schema[src]

Returns a Schema that matches nothing, such as the schema { "not":{} }.

The exact value returned depends on this generator's BoolSchemas setting.

pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema[src]

Generates a JSON Schema for the type T, and returns either the schema itself or a $ref schema referencing T's schema.

If T is referenceable, this will add T's schema to this generator's definitions, and return a $ref schema referencing that schema. Otherwise, this method behaves identically to JsonSchema::json_schema.

If T's schema depends on any referenceable schemas, then this method will add them to the SchemaGenerator's schema definitions.

pub fn definitions(&self) -> &Map<String, Schema>[src]

Returns the collection of all referenceable schemas that have been generated.

The keys of the returned Map are the schema names, and the values are the schemas themselves.

pub fn into_definitions(self) -> Map<String, Schema>[src]

Consumes self and returns the collection of all referenceable schemas that have been generated.

The keys of the returned Map are the schema names, and the values are the schemas themselves.

pub fn root_schema_for<T: ?Sized + JsonSchema>(&mut self) -> RootSchema[src]

Generates a root JSON Schema for the type T.

If T's schema depends on any referenceable schemas, then this method will add them to the SchemaGenerator's schema definitions and include them in the returned SchemaObject's definitions

pub fn into_root_schema_for<T: ?Sized + JsonSchema>(self) -> RootSchema[src]

Consumes self and generates a root JSON Schema for the type T.

If T's schema depends on any referenceable schemas, then this method will include them in the returned SchemaObject's definitions

pub fn dereference<'a>(&'a self, schema: &Schema) -> Option<&'a Schema>[src]

Attemps to find the schema that the given schema is referencing.

If the given schema has a $ref property which refers to another schema in self's schema definitions, the referenced schema will be returned. Otherwise, returns None.

Example

use schemars::{JsonSchema, gen::SchemaGenerator};

#[derive(JsonSchema)]
struct MyStruct {
    foo: i32,
}

let mut gen = SchemaGenerator::default();
let ref_schema = gen.subschema_for::<MyStruct>();

assert!(ref_schema.is_ref());

let dereferenced = gen.dereference(&ref_schema);

assert!(dereferenced.is_some());
assert!(!dereferenced.unwrap().is_ref());
assert_eq!(dereferenced, gen.definitions().get("MyStruct"));

Trait Implementations

impl Clone for SchemaGenerator[src]

impl Debug for SchemaGenerator[src]

impl Default for SchemaGenerator[src]

impl From<SchemaSettings> for SchemaGenerator[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.