1515from typing import Any , Dict , Optional , Union , Tuple , TYPE_CHECKING
1616
1717import jsonschema # type: ignore
18- from referencing import Registry , Resource
19- from referencing .jsonschema import DRAFT7
2018
2119from opentrons_shared_data .labware import load_schema as load_labware_schema
2220from opentrons_shared_data .protocol import (
@@ -628,14 +626,7 @@ def validate_json(protocol_json: Dict[Any, Any]) -> Tuple[int, "JsonProtocolDef"
628626 # Check if this is actually a labware
629627 labware_schema_v2 = load_labware_schema ()
630628 try :
631- # Use referencing library for JSON pointer resolution
632- # Labware schemas contain JSON pointer references like #/definitions/vector
633- labware_registry = Registry ().with_resource (
634- labware_schema_v2 .get ("$id" , "labware_schema" ),
635- Resource .from_contents (labware_schema_v2 , default_specification = DRAFT7 ),
636- )
637- validator = jsonschema .Draft7Validator (labware_schema_v2 , registry = labware_registry )
638- validator .validate (protocol_json )
629+ jsonschema .validate (protocol_json , labware_schema_v2 )
639630 except jsonschema .ValidationError :
640631 pass
641632 else :
@@ -660,29 +651,16 @@ def validate_json(protocol_json: Dict[Any, Any]) -> Tuple[int, "JsonProtocolDef"
660651 raise JSONSchemaVersionTooNewError (attempted_schema_version = version_num )
661652 protocol_schema = _get_schema_for_protocol (version_num )
662653
663- # Use referencing library for JSON pointer resolution
664- # Protocol schemas reference both same-document (#/definitions/slot) and
665- # cross-schema (opentronsLabwareSchemaV2) references.
666- # The referencing library handles both automatically when schemas are in the registry.
667- registry = Registry ()
668- # Add labware schema for cross-schema references
669- labware_schema_id = labware_schema_v2 .get ("$id" , "opentronsLabwareSchemaV2" )
670- registry = registry .with_resource (
671- labware_schema_id ,
672- Resource .from_contents (labware_schema_v2 , default_specification = DRAFT7 ),
654+ # instruct schema how to resolve all $ref's used in protocol schemas
655+ resolver = jsonschema .RefResolver (
656+ protocol_schema .get ("$id" , "" ),
657+ protocol_schema ,
658+ store = {"opentronsLabwareSchemaV2" : labware_schema_v2 },
673659 )
674- # Add protocol schema to registry (needed for same-document and cross-schema refs)
675- protocol_schema_id = protocol_schema .get ("$id" , "" )
676- if protocol_schema_id :
677- registry = registry .with_resource (
678- protocol_schema_id ,
679- Resource .from_contents (protocol_schema , default_specification = DRAFT7 ),
680- )
681660
682- # Validate using jsonschema with the referencing registry
661+ # do the validation
683662 try :
684- validator = jsonschema .Draft7Validator (protocol_schema , registry = registry )
685- validator .validate (protocol_json )
663+ jsonschema .validate (protocol_json , protocol_schema , resolver = resolver )
686664 except jsonschema .ValidationError :
687665 MODULE_LOG .exception ("JSON protocol validation failed" )
688666 raise RuntimeError (
0 commit comments