pub struct CompilationOptions { /* private fields */ }
Expand description

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

Compile schema into JSONSchema using the currently defined options.

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

options.with_draft(Draft::Draft4);

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

Use a custom resolver for resolving external schema references.

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");

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

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");

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.

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.

Register a custom “format” validator.

Example
fn my_format(s: &str) -> bool {
   // Your awesome format check!
   s.ends_with("42!")
}
let schema = json!({"type": "string", "format": "custom"});
let compiled = JSONSchema::options()
    .with_format("custom", my_format)
    .compile(&schema)
    .expect("Valid schema");
// Invalid string
assert!(!compiled.is_valid(&json!("foo")));
// Valid string
assert!(compiled.is_valid(&json!("foo42!")));

The format check function should receive &str and return bool.

Force enable or disable format validation. The default behavior is dependent on draft version, but the default behavior can be overridden to validate or not regardless of draft.

Set the false if unrecognized formats should be reported as a validation error. By default unknown formats are silently ignored.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more