Struct jsonschema::CompilationOptions[][src]

pub struct CompilationOptions { /* fields omitted */ }

Full configuration to guide the JSONSchema compilation.

Using a CompilationOptions instance you can configure the supported draft, content media types and more (check the exposed methods)

Implementations

impl CompilationOptions[src]

pub fn compile<'a>(
    &self,
    schema: &'a Value
) -> Result<JSONSchema<'a>, CompilationError>
[src]

Compile schema into JSONSchema using the currently defined options.

pub fn with_draft(&mut self, draft: Draft) -> &mut Self[src]

Ensure that the schema is going to be compiled using the defined Draft.

options.with_draft(Draft::Draft4);

pub fn with_content_media_type(
    &mut self,
    media_type: &'static str,
    media_type_check: fn(_: &str) -> bool
) -> &mut Self
[src]

Ensure that compiled schema is going to support the provided content media type.

Arguments:

  • media_type: Name of the content media type to support (ie. “application/json”)
  • media_type_check: Method checking the validity of the input string according to the media type. The method should return true if the input is valid, false otherwise.

Example:

fn check_custom_media_type(instance_string: &str) -> bool {
    // In reality the check might be a bit more different ;)
    instance_string != "not good"
}
// Add support for application/jsonschema-test
options.with_content_media_type("application/jsonschema-test", check_custom_media_type);

pub fn without_content_media_type_support(
    &mut self,
    media_type: &'static str
) -> &mut Self
[src]

Ensure that compiled schema is not supporting the provided content media type.

// Disable support for application/json (which is supported by jsonschema crate)
options.without_content_media_type_support("application/json");

pub fn with_content_encoding(
    &mut self,
    content_encoding: &'static str,
    content_encoding_check: fn(_: &str) -> bool,
    content_encoding_converter: fn(_: &str) -> Result<Option<String>, ValidationError<'static>>
) -> &mut Self
[src]

Ensure that compiled schema is going to support the provided content encoding.

Arguments:

  • content_encoding: Name of the content encoding to support (ie. “base64”)
  • content_encoding_check: Method checking the validity of the input string according to content encoding. The method should return true if the input is valid, false otherwise.
  • content_encoding_converter: Method converting the input string into a string representation (generally output of the decoding of the content encoding). The method should return:
    • Err(ValidationError instance): in case of a jsonschema crate supported error (obtained via ? or From::from APIs)
    • Ok(None): if the input string is not valid according to the content encoding
    • Ok(Some(content)): if the input string is valid according to the content encoding, content will contain the string representation of the decoded input

Example:

// The instance_string contains a number (representing the length of the string)
// a space and then the string (whose length should match the expectation).
// Example: "3 The" or "4  123"
fn check_custom_encoding(instance_string: &str) -> bool {
    if let Some(first_space_index) = instance_string.find(' ') {
        if let Ok(value) = instance_string[..first_space_index].parse::<u64>() {
            return instance_string[first_space_index + 1..].chars().count() == value as usize;
        }
    }
    false
}
fn converter_custom_encoding(instance_string: &str) -> Result<Option<String>, ValidationError<'static>> {
    if let Some(first_space_index) = instance_string.find(' ') {
        if let Ok(value) = instance_string[..first_space_index].parse::<u64>() {
            if instance_string[first_space_index + 1..].chars().count() == value as usize {
                return Ok(Some(instance_string[first_space_index + 1..].to_string()));
            }
        }
    }
    Ok(None)
}
// Add support for prefix-length-string
options.with_content_encoding("prefix-length-string", check_custom_encoding, converter_custom_encoding);

pub fn without_content_encoding_support(
    &mut self,
    content_encoding: &'static str
) -> &mut Self
[src]

Ensure that compiled schema is not supporting the provided content encoding.

// Disable support for base64 (which is supported by jsonschema crate)
options.without_content_encoding_support("base64");

pub fn with_meta_schemas(&mut self) -> &mut Self[src]

Add meta schemas for supported JSON Schema drafts. It is helpful if your schema has references to JSON Schema meta-schemas:

{
    "schema": {
        "multipleOf": {
            "$ref": "http://json-schema.org/draft-04/schema#/properties/multipleOf"
        },
        "maximum": {
            "$ref": "http://json-schema.org/draft-04/schema#/properties/maximum"
        }
    }
}

The example above is taken from the Swagger 2.0 JSON schema.

pub fn with_document(&mut self, id: String, document: Value) -> &mut Self[src]

Add a new document to the store. It works as a cache to avoid making additional network calls to remote schemas via the $ref keyword.

Trait Implementations

impl Clone for CompilationOptions[src]

impl Debug for CompilationOptions[src]

impl Default for CompilationOptions[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> Instrument 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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,