@@ -27,7 +27,7 @@ def test_jschema(validator, pattern):
2727 if not validator .is_valid (pattern ):
2828 es = validator .iter_errors (pattern )
2929 for e in es :
30- warnings .warn (" => " . join ([ str (e .schema_path ), str ( e . message ), str ( e . context )] ))
30+ warnings .warn (str (e .message ))
3131 is_valid = False
3232
3333 return is_valid
@@ -200,6 +200,29 @@ def test_multi_clause_multi_list(pattern):
200200 return stat
201201
202202
203+ def test_annotation_properties (pattern ):
204+ """
205+ Structurally tests whether an annotation property is declared before use.
206+ Args:
207+ pattern: schema in yaml format to validate
208+
209+ Returns: True if all used annotation properties are declared, False otherwise.
210+ """
211+ declared_annotations = set ()
212+ if 'annotationProperties' in pattern .keys (): declared_annotations .update (set (pattern ['annotationProperties' ].keys ()))
213+ expr = parse ('annotations.[*].annotationProperty' )
214+ used_annotations = [match for match in expr .find (pattern )]
215+
216+ stat = True
217+ if used_annotations :
218+ for annotation_prop in used_annotations :
219+ val = annotation_prop .value
220+ if val not in declared_annotations :
221+ warnings .warn ("Annotation property '%s' didn't declared before use." % val )
222+ stat = False
223+ return stat
224+
225+
203226def format_warning (message , category , filename , lineno , line = None ):
204227 return '%s:%s: %s:%s\n ' % (filename , lineno , category .__name__ , message )
205228
@@ -239,7 +262,8 @@ def validate(pattern):
239262
240263 stat = True
241264 for pattern_doc in pattern_docs :
242- logging .info ("Checking %s" % pattern_doc )
265+ if len (pattern_docs ) > 1 :
266+ logging .info ("Checking %s" % pattern_doc )
243267 with open (pattern_doc , "r" ) as stream :
244268 try :
245269 pattern = ryaml .load (stream )
@@ -249,6 +273,7 @@ def validate(pattern):
249273 if not test_clause_nesting (pattern ): stat = False
250274 if not test_axiom_separator (pattern ): stat = False
251275 if not test_multi_clause_multi_list (pattern ): stat = False
276+ if not test_annotation_properties (pattern ): stat = False
252277 except YAMLError as exc :
253278 stat = False
254279 logging .error ('Failed to load pattern file: ' + pattern_doc )
0 commit comments