Skip to content

Commit

Permalink
fix: Ignoring $schema in resolved references
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Sep 10, 2024
1 parent 9c12d24 commit d27f9be
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 150 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Fixed

- Ignoring `$schema` in resolved references.

### Deprecated

- `with_meta_schemas()` method. Meta schemas are included by default.

## [0.18.1] - 2024-08-24

### Added
Expand Down
8 changes: 8 additions & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## [Unreleased]

### Fixed

- Ignoring ``$schema`` in resolved references.

### Deprecated

- `with_meta_schemas` argument. Meta schemas are included by default.

## [0.18.1] - 2024-08-24

### Changed
Expand Down
8 changes: 4 additions & 4 deletions bindings/python/benches/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ def args(request, variant, is_compiled):
schema, instance = request.node.get_closest_marker("data").args
if variant == "jsonschema-rs-is-valid":
if is_compiled:
return jsonschema_rs.JSONSchema(schema, with_meta_schemas=True).is_valid, instance
return jsonschema_rs.JSONSchema(schema).is_valid, instance
else:
return (
partial(jsonschema_rs.is_valid, with_meta_schemas=True),
jsonschema_rs.is_valid,
schema,
instance,
)
if variant == "jsonschema-rs-validate":
if is_compiled:
return jsonschema_rs.JSONSchema(schema, with_meta_schemas=True).validate, instance
return jsonschema_rs.JSONSchema(schema).validate, instance
else:
return (
partial(jsonschema_rs.validate, with_meta_schemas=True),
jsonschema_rs.validate,
schema,
instance,
)
Expand Down
19 changes: 10 additions & 9 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,12 @@ thread_local! {

fn make_options(
draft: Option<u8>,
with_meta_schemas: Option<bool>,
formats: Option<&Bound<'_, PyDict>>,
) -> PyResult<jsonschema::CompilationOptions> {
let mut options = jsonschema::JSONSchema::options();
if let Some(raw_draft_version) = draft {
options.with_draft(get_draft(raw_draft_version)?);
}
if with_meta_schemas == Some(true) {
options.with_meta_schemas();
}
if let Some(formats) = formats {
for (name, callback) in formats.iter() {
if !callback.is_callable() {
Expand Down Expand Up @@ -284,6 +280,7 @@ fn to_error_message(error: &jsonschema::ValidationError<'_>) -> String {
/// If your workflow implies validating against the same schema, consider using `JSONSchema.is_valid`
/// instead.
#[pyfunction]
#[allow(unused_variables)]
#[pyo3(signature = (schema, instance, draft=None, with_meta_schemas=false, formats=None))]
fn is_valid(
py: Python<'_>,
Expand All @@ -293,7 +290,7 @@ fn is_valid(
with_meta_schemas: Option<bool>,
formats: Option<&Bound<'_, PyDict>>,
) -> PyResult<bool> {
let options = make_options(draft, with_meta_schemas, formats)?;
let options = make_options(draft, formats)?;
let schema = ser::to_value(schema)?;
match options.compile(&schema) {
Ok(compiled) => {
Expand All @@ -317,6 +314,7 @@ fn is_valid(
/// If your workflow implies validating against the same schema, consider using `JSONSchema.validate`
/// instead.
#[pyfunction]
#[allow(unused_variables)]
#[pyo3(signature = (schema, instance, draft=None, with_meta_schemas=false, formats=None))]
fn validate(
py: Python<'_>,
Expand All @@ -326,7 +324,7 @@ fn validate(
with_meta_schemas: Option<bool>,
formats: Option<&Bound<'_, PyDict>>,
) -> PyResult<()> {
let options = make_options(draft, with_meta_schemas, formats)?;
let options = make_options(draft, formats)?;
let schema = ser::to_value(schema)?;
match options.compile(&schema) {
Ok(compiled) => raise_on_error(py, &compiled, instance),
Expand All @@ -345,6 +343,7 @@ fn validate(
/// If your workflow implies validating against the same schema, consider using `JSONSchema.iter_errors`
/// instead.
#[pyfunction]
#[allow(unused_variables)]
#[pyo3(signature = (schema, instance, draft=None, with_meta_schemas=false, formats=None))]
fn iter_errors(
py: Python<'_>,
Expand All @@ -354,7 +353,7 @@ fn iter_errors(
with_meta_schemas: Option<bool>,
formats: Option<&Bound<'_, PyDict>>,
) -> PyResult<ValidationErrorIter> {
let options = make_options(draft, with_meta_schemas, formats)?;
let options = make_options(draft, formats)?;
let schema = ser::to_value(schema)?;
match options.compile(&schema) {
Ok(compiled) => iter_on_error(py, &compiled, instance),
Expand Down Expand Up @@ -403,6 +402,7 @@ fn handle_format_checked_panic(err: Box<dyn Any + Send>) -> PyErr {
#[pymethods]
impl JSONSchema {
#[new]
#[allow(unused_variables)]
#[pyo3(signature = (schema, draft=None, with_meta_schemas=false, formats=None))]
fn new(
py: Python<'_>,
Expand All @@ -411,7 +411,7 @@ impl JSONSchema {
with_meta_schemas: Option<bool>,
formats: Option<&Bound<'_, PyDict>>,
) -> PyResult<Self> {
let options = make_options(draft, with_meta_schemas, formats)?;
let options = make_options(draft, formats)?;
let raw_schema = ser::to_value(schema)?;
match options.compile(&raw_schema) {
Ok(schema) => Ok(JSONSchema {
Expand All @@ -429,6 +429,7 @@ impl JSONSchema {
///
/// Use it if you have your schema as a string and want to utilize Rust JSON parsing.
#[classmethod]
#[allow(unused_variables)]
#[pyo3(signature = (string, draft=None, with_meta_schemas=false, formats=None))]
fn from_str(
_: &Bound<'_, PyType>,
Expand All @@ -453,7 +454,7 @@ impl JSONSchema {
let slice = unsafe { std::slice::from_raw_parts(ptr.cast::<u8>(), str_size as usize) };
let raw_schema = serde_json::from_slice(slice)
.map_err(|error| PyValueError::new_err(format!("Invalid string: {}", error)))?;
let options = make_options(draft, with_meta_schemas, formats)?;
let options = make_options(draft, formats)?;
match options.compile(&raw_schema) {
Ok(schema) => Ok(JSONSchema {
schema,
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/tests-py/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def pytest_generate_tests(metafunc):
def test_draft(filename, draft, schema, instance, expected, description):
error_message = f"[{filename}] {description}: {schema} | {instance}"
try:
result = jsonschema_rs.is_valid(schema, instance, int(draft), with_meta_schemas=True)
result = jsonschema_rs.is_valid(schema, instance, int(draft))
assert result is expected, error_message
except ValueError:
pytest.fail(error_message)
3 changes: 1 addition & 2 deletions jsonschema/benches/jsonschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use serde_json::Value;
macro_rules! jsonschema_bench {
($c:tt, $name:expr, $schema:ident, $instance:ident) => {{
let compiled = JSONSchema::options()
.with_meta_schemas()
.compile(&$schema)
.expect("Invalid schema");
assert!(compiled.is_valid(&$instance), "Invalid instance");
assert!(compiled.validate(&$instance).is_ok(), "Invalid instance");
$c.bench_function(&format!("{} jsonschema/compile", $name), |b| {
b.iter(|| JSONSchema::options().with_meta_schemas().compile(&$schema))
b.iter(|| JSONSchema::options().compile(&$schema))
});
$c.bench_function(&format!("{} jsonschema/is_valid", $name), |b| {
b.iter(|| compiled.is_valid(&$instance))
Expand Down
Loading

0 comments on commit d27f9be

Please sign in to comment.