Skip to content

Commit

Permalink
feat: Rework public API
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Sep 16, 2024
1 parent d8eb69d commit 42ecbe7
Show file tree
Hide file tree
Showing 2 changed files with 315 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/jsonschema/src/keywords/unevaluated_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ mod tests {
"blah": 1
});

let validator = crate::compile(&schema).expect("Schema should compile");
let validator = crate::validator_for(&schema).expect("Schema should compile");

assert!(validator.validate(&valid).is_ok(), "Validation should pass");
assert!(validator.is_valid(&valid), "Instance should be valid");
Expand Down
314 changes: 314 additions & 0 deletions crates/jsonschema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,324 @@ pub fn is_valid(schema: &Value, instance: &Value) -> bool {
}

/// Compile the input schema for faster validation.
///
/// # Deprecated
/// This function is deprecated since version 0.20.0. Use `validator_for` instead.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::compile(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
#[deprecated(since = "0.20.0", note = "Use `validator_for` instead")]
pub fn compile(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::compile(schema)
}

/// Create a validator for the input schema with automatic draft detection.
///
/// # Examples
///
/// ```
/// use serde_json::json;
/// use jsonschema::validator_for;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::validator_for(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn validator_for(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::compile(schema)
}

/// Functionality specific to JSON Schema Draft 4.
///
/// This module provides functions for creating validators and performing validation
/// according to the JSON Schema Draft 4 specification.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"type": "number", "multipleOf": 2});
/// let instance = json!(4);
///
/// assert!(jsonschema::draft4::is_valid(&schema, &instance));
/// ```
pub mod draft4 {
use super::*;

/// Create a new JSON Schema validator using Draft 4 specifications.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::draft4::new(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn new(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::options()
.with_draft(Draft::Draft4)
.compile(schema)
}
/// Validate an instance against a schema using Draft 4 specifications without creating a validator.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let valid_instance = json!(42);
/// let invalid_instance = json!(3);
///
/// assert!(jsonschema::draft4::is_valid(&schema, &valid_instance));
/// assert!(!jsonschema::draft4::is_valid(&schema, &invalid_instance));
/// ```
#[must_use]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
new(schema).expect("Invalid schema").is_valid(instance)
}
}

/// Functionality specific to JSON Schema Draft 6.
///
/// This module provides functions for creating validators and performing validation
/// according to the JSON Schema Draft 6 specification.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"type": "string", "format": "uri"});
/// let instance = json!("https://www.example.com");
///
/// assert!(jsonschema::draft6::is_valid(&schema, &instance));
/// ```
pub mod draft6 {
use super::*;

/// Create a new JSON Schema validator using Draft 6 specifications.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::draft6::new(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn new(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::options()
.with_draft(Draft::Draft6)
.compile(schema)
}
/// Validate an instance against a schema using Draft 6 specifications without creating a validator.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let valid_instance = json!(42);
/// let invalid_instance = json!(3);
///
/// assert!(jsonschema::draft6::is_valid(&schema, &valid_instance));
/// assert!(!jsonschema::draft6::is_valid(&schema, &invalid_instance));
/// ```
#[must_use]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
new(schema).expect("Invalid schema").is_valid(instance)
}
}

/// Functionality specific to JSON Schema Draft 7.
///
/// This module provides functions for creating validators and performing validation
/// according to the JSON Schema Draft 7 specification.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"type": "string", "pattern": "^[a-zA-Z0-9]+$"});
/// let instance = json!("abc123");
///
/// assert!(jsonschema::draft7::is_valid(&schema, &instance));
/// ```
pub mod draft7 {
use super::*;

/// Create a new JSON Schema validator using Draft 7 specifications.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::draft7::new(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn new(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(schema)
}
/// Validate an instance against a schema using Draft 7 specifications without creating a validator.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let valid_instance = json!(42);
/// let invalid_instance = json!(3);
///
/// assert!(jsonschema::draft7::is_valid(&schema, &valid_instance));
/// assert!(!jsonschema::draft7::is_valid(&schema, &invalid_instance));
/// ```
#[must_use]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
new(schema).expect("Invalid schema").is_valid(instance)
}
}

/// Functionality specific to JSON Schema Draft 2019-09.
///
/// This module provides functions for creating validators and performing validation
/// according to the JSON Schema Draft 2019-09 specification.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"type": "array", "minItems": 2, "uniqueItems": true});
/// let instance = json!([1, 2]);
///
/// assert!(jsonschema::draft201909::is_valid(&schema, &instance));
/// ```
pub mod draft201909 {
use super::*;

/// Create a new JSON Schema validator using Draft 2019-09 specifications.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::draft201909::new(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn new(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::options()
.with_draft(Draft::Draft201909)
.compile(schema)
}
/// Validate an instance against a schema using Draft 2019-09 specifications without creating a validator.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let valid_instance = json!(42);
/// let invalid_instance = json!(3);
///
/// assert!(jsonschema::draft201909::is_valid(&schema, &valid_instance));
/// assert!(!jsonschema::draft201909::is_valid(&schema, &invalid_instance));
/// ```
#[must_use]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
new(schema).expect("Invalid schema").is_valid(instance)
}
}

/// Functionality specific to JSON Schema Draft 2020-12.
///
/// This module provides functions for creating validators and performing validation
/// according to the JSON Schema Draft 2020-12 specification.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]});
/// let instance = json!({"name": "John Doe"});
///
/// assert!(jsonschema::draft202012::is_valid(&schema, &instance));
/// ```
pub mod draft202012 {
use super::*;

/// Create a new JSON Schema validator using Draft 2020-12 specifications.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let instance = json!(42);
///
/// let validator = jsonschema::draft202012::new(&schema).expect("Invalid schema");
/// assert!(validator.is_valid(&instance));
/// ```
pub fn new(schema: &Value) -> Result<JSONSchema, ValidationError> {
JSONSchema::options()
.with_draft(Draft::Draft202012)
.compile(schema)
}
/// Validate an instance against a schema using Draft 2020-12 specifications without creating a validator.
///
/// # Examples
///
/// ```
/// use serde_json::json;
///
/// let schema = json!({"minimum": 5});
/// let valid_instance = json!(42);
/// let invalid_instance = json!(3);
///
/// assert!(jsonschema::draft202012::is_valid(&schema, &valid_instance));
/// assert!(!jsonschema::draft202012::is_valid(&schema, &invalid_instance));
/// ```
#[must_use]
pub fn is_valid(schema: &Value, instance: &Value) -> bool {
new(schema).expect("Invalid schema").is_valid(instance)
}
}

#[cfg(test)]
pub(crate) mod tests_util {
use super::JSONSchema;
Expand Down

0 comments on commit 42ecbe7

Please sign in to comment.