@@ -84,32 +84,37 @@ class Subprotocol(Enum):
8484def uses_thing_context (v : ThingContextType ) -> None :
8585 """Check the URLs in the ThingContextType are valid.
8686
87- This function makes ``assert`` statements, so will fail with an exception
88- if it is not valid. This module is hard coded to use valid URLs, so it is
89- not an expected error .
87+ This function checks a valid context URL is provided. See the
88+ JSONSchema for Thing Description (`td-json-schema-validation.json`)
89+ for more details .
9090
9191 See https://www.w3.org/TR/wot-thing-description11/#thing
92- This refers to the ``@context`` property.
92+ Specifically, the ``@context`` property is what this function
93+ validates.
9394
9495 :param v: the ThingContextType object.
9596
9697 :raises ValueError: if the URL is not correct.
9798 """
9899 if not isinstance (v , list ):
99- if v is not THING_CONTEXT_URL :
100- raise ValueError (f"{ v } must be { THING_CONTEXT_URL } " ) # pragma: no cover
101- # excluded from coverage as this is hardcoded, so we shouldn't ever
102- # see the error.
100+ if v != THING_CONTEXT_URL :
101+ raise ValueError (f"{ v } must be { THING_CONTEXT_URL } " )
103102 else :
104- if not (
105- v [0 ] == THING_CONTEXT_URL
106- or v [1 ] == THING_CONTEXT_URL
107- and v [0 ] == THING_CONTEXT_URL_v1
108- ):
109- raise ValueError (
110- f"{ v } must contain { THING_CONTEXT_URL } "
111- ) # pragma: no cover
112- # This is hard-coded, so is not an error we ever expect to see.
103+ if len (v ) == 0 :
104+ raise ValueError ("The context can't be an empty list." )
105+ if v [0 ] == THING_CONTEXT_URL :
106+ if THING_CONTEXT_URL_v1 in v [1 :]:
107+ raise ValueError ("An old context is given after the current one." )
108+ return # If the old URL isn't in the list, it's OK.
109+ if v [0 ] == THING_CONTEXT_URL_v1 :
110+ # It's OK to start with the old URL, provided the new URL follows.
111+ if len (v ) > 1 and v [1 ] == THING_CONTEXT_URL :
112+ return # Old URL followed by new URL is OK.
113+ else :
114+ raise ValueError ("The thing context URL is outdated, should be v1.1." )
115+ raise ValueError (
116+ f"{ v } must contain { THING_CONTEXT_URL } before { THING_CONTEXT_URL_v1 } ."
117+ )
113118
114119
115120ThingContext = Annotated [
0 commit comments