Struct jsonschema::ValidationOptions
source · pub struct ValidationOptions { /* private fields */ }Expand description
Configuration options for JSON Schema validation.
Implementations§
source§impl ValidationOptions
impl ValidationOptions
sourcepub fn build(
&self,
schema: &Value,
) -> Result<Validator, ValidationError<'static>>
pub fn build( &self, schema: &Value, ) -> Result<Validator, ValidationError<'static>>
Build a JSON Schema validator using the current options.
§Example
use serde_json::json;
let schema = json!({"type": "string"});
let validator = jsonschema::options()
.build(&schema)
.expect("A valid schema");
assert!(validator.is_valid(&json!("Hello")));
assert!(!validator.is_valid(&json!(42)));sourcepub fn compile<'a>(
&self,
schema: &'a Value,
) -> Result<Validator, ValidationError<'a>>
👎Deprecated since 0.20.0: Use ValidationOptions::build instead
pub fn compile<'a>( &self, schema: &'a Value, ) -> Result<Validator, ValidationError<'a>>
ValidationOptions::build insteadBuild a JSON Schema validator using the current options.
DEPRECATED: Use ValidationOptions::build instead.
sourcepub fn with_draft(&mut self, draft: Draft) -> &mut Self
pub fn with_draft(&mut self, draft: Draft) -> &mut Self
Sets the JSON Schema draft version.
use jsonschema::Draft;
let options = jsonschema::options()
.with_draft(Draft::Draft4);sourcepub fn with_content_media_type(
&mut self,
media_type: &'static str,
media_type_check: fn(_: &str) -> bool,
) -> &mut Self
pub fn with_content_media_type( &mut self, media_type: &'static str, media_type_check: fn(_: &str) -> bool, ) -> &mut Self
Add support for a custom content media type validation.
§Example
fn check_custom_media_type(instance_string: &str) -> bool {
instance_string.starts_with("custom:")
}
let options = jsonschema::options()
.with_content_media_type("application/custom", check_custom_media_type);sourcepub fn with_resolver(
&mut self,
resolver: impl SchemaResolver + 'static,
) -> &mut Self
pub fn with_resolver( &mut self, resolver: impl SchemaResolver + 'static, ) -> &mut Self
Set a custom resolver for external references.
sourcepub fn without_content_media_type_support(
&mut self,
media_type: &'static str,
) -> &mut Self
pub fn without_content_media_type_support( &mut self, media_type: &'static str, ) -> &mut Self
Remove support for a specific content media type validation.
sourcepub fn with_content_encoding(
&mut self,
encoding: &'static str,
check: fn(_: &str) -> bool,
converter: fn(_: &str) -> Result<Option<String>, ValidationError<'static>>,
) -> &mut Self
pub fn with_content_encoding( &mut self, encoding: &'static str, check: fn(_: &str) -> bool, converter: fn(_: &str) -> Result<Option<String>, ValidationError<'static>>, ) -> &mut Self
Add support for a custom content encoding.
§Arguments
encoding: Name of the content encoding (e.g., “base64”)check: Validates the input string (returntrueif valid)converter: Converts the input string, returning:Err(ValidationError): For supported errorsOk(None): If input is invalidOk(Some(content)): If valid, with decoded content
§Example
use jsonschema::ValidationError;
fn check(s: &str) -> bool {
s.starts_with("valid:")
}
fn convert(s: &str) -> Result<Option<String>, ValidationError<'static>> {
if s.starts_with("valid:") {
Ok(Some(s[6..].to_string()))
} else {
Ok(None)
}
}
let options = jsonschema::options()
.with_content_encoding("custom", check, convert);sourcepub fn without_content_encoding_support(
&mut self,
content_encoding: &'static str,
) -> &mut Self
pub fn without_content_encoding_support( &mut self, content_encoding: &'static str, ) -> &mut Self
Remove support for a specific content encoding.
§Example
let options = jsonschema::options()
.without_content_encoding_support("base64");sourcepub fn with_meta_schemas(&mut self) -> &mut Self
👎Deprecated since 0.19.0: Meta schemas are now included by default
pub fn with_meta_schemas(&mut self) -> &mut Self
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.
sourcepub fn with_document(&mut self, id: String, document: Value) -> &mut Self
pub fn with_document(&mut self, id: String, document: Value) -> &mut Self
Add a document to the store.
Acts as a cache to avoid network calls for remote schemas referenced by $ref.
sourcepub fn with_format<N, F>(&mut self, name: N, format: F) -> &mut Self
pub fn with_format<N, F>(&mut self, name: N, format: F) -> &mut Self
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 validator = jsonschema::options()
.with_format("custom", my_format)
.build(&schema)
.expect("Valid schema");
assert!(!validator.is_valid(&json!("foo")));
assert!(validator.is_valid(&json!("foo42!")));sourcepub fn should_validate_formats(&mut self, yes: bool) -> &mut Self
pub fn should_validate_formats(&mut self, yes: bool) -> &mut Self
Set whether to validate formats.
Default behavior depends on the draft version. This method overrides the default, enabling or disabling format validation regardless of draft.
sourcepub fn should_ignore_unknown_formats(&mut self, yes: bool) -> &mut Self
pub fn should_ignore_unknown_formats(&mut self, yes: bool) -> &mut Self
Set whether to ignore unknown formats.
By default, unknown formats are silently ignored. Set to false to report
unrecognized formats as validation errors.
sourcepub fn with_keyword<N, F>(&mut self, name: N, factory: F) -> &mut Self
pub fn with_keyword<N, F>(&mut self, name: N, factory: F) -> &mut Self
Register a custom keyword validator.
§Example
struct MyCustomValidator;
impl Keyword for MyCustomValidator {
fn validate<'instance>(
&self,
instance: &'instance Value,
instance_path: &JsonPointerNode,
) -> ErrorIterator<'instance> {
// ... validate instance ...
if !instance.is_object() {
let error = ValidationError::custom(
JsonPointer::default(),
instance_path.into(),
instance,
"Boom!",
);
Box::new(once(error))
} else {
Box::new(None.into_iter())
}
}
fn is_valid(&self, instance: &Value) -> bool {
// ... determine if instance is valid ...
true
}
}
// You can create a factory function, or use a closure to create new validator instances.
fn custom_validator_factory<'a>(
parent: &'a Map<String, Value>,
value: &'a Value,
path: JsonPointer,
) -> Result<Box<dyn Keyword>, ValidationError<'a>> {
Ok(Box::new(MyCustomValidator))
}
let validator = jsonschema::options()
.with_keyword("my-type", custom_validator_factory)
.with_keyword("my-type-with-closure", |_, _, _| Ok(Box::new(MyCustomValidator)))
.build(&json!({ "my-type": "my-schema"}))
.expect("A valid schema");
assert!(validator.is_valid(&json!({ "a": "b"})));Trait Implementations§
source§impl Clone for ValidationOptions
impl Clone for ValidationOptions
source§fn clone(&self) -> ValidationOptions
fn clone(&self) -> ValidationOptions
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for ValidationOptions
impl Debug for ValidationOptions
Auto Trait Implementations§
impl Freeze for ValidationOptions
impl !RefUnwindSafe for ValidationOptions
impl Send for ValidationOptions
impl Sync for ValidationOptions
impl Unpin for ValidationOptions
impl !UnwindSafe for ValidationOptions
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)