Jul-16-2021, 08:37 AM
Hi All,
I wanted to catch the schema error from yaml schema validation, at the same time it should not throw on console as well. I have written some sample code here, from here I could able to catch the error, but then it also throw on console when any exception happened.
Any idea would really appreciate. Thanks.
Regards,
Maiya
I wanted to catch the schema error from yaml schema validation, at the same time it should not throw on console as well. I have written some sample code here, from here I could able to catch the error, but then it also throw on console when any exception happened.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import sys import os import oyaml as yaml from pykwalify.core import Core, SchemaError from pykwalify.errors import RuleError yaml_schema = Core(source_file = "config/config_full.yml" , schema_files = [ "config/schema.yml" ]) try : yaml_schema.validate(raise_exception = True ) #except (SchemaError, RuleError) as er: except SchemaError as error: print ( 'printing schema error' ) print (error) |
Output:validation.invalid
--- All found errors ---
["Cannot find required key 'name'. Path: '/config/data'", "Cannot find required key 'name'. Path: '/config/data'"]
printing schema error
<SchemaError: error code 2: Schema validation failed:
- Cannot find required key 'name'. Path: '/config/data'.
- Cannot find required key 'name'. Path: '/config/data'.: Path: '/'>
I could able to catch the exception and also able to print those errors using print method, but then also it throws it on console when exception happened, which I do not want. I wanted to suppress this automatically throw it on console, since I already captured the exception/error and can print later on.Any idea would really appreciate. Thanks.
Regards,
Maiya