import glob import yaml from cerberus import Validator def validate_schema(params): # config_files = glob.glob('**/config.yaml', recursive=True) # fname = config_files[0] # with open(fname) as stream: # params = yaml.load(stream, Loader=yaml.FullLoader) # print(params) schema = { 'daq': { 'type': 'dict', 'schema': { 'n_channels_max': {'required': True, 'type': 'integer', 'min': 1, 'max': 128}, 'daq_sleep': {'required': True, 'type': 'float', 'min': 0, 'max': 1.}, 'fs': {'required': True, 'type': 'float', 'min': 1., 'max': 30000.}, 'smpl_fct': {'required': True, 'type': 'integer', 'min': 1, 'max': 30000}, } }, 'speller': { 'type': 'dict', 'schema': { 'type': {'required': True, 'type': 'string', 'allowed': ['question', 'exploration', 'training_color', 'color', 'feedback']} } }, 'recording': { 'type': 'dict', 'schema': { 'timing': { 'type': 'dict', 'schema': { 't_baseline_1': {'required': True, 'type': 'float', 'min': 0., 'max': 600.}, 't_baseline_all': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 't_baseline_rand': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 't_after_stimulus': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 't_response': {'required': True, 'type': 'float', 'min': 0., 'max': 60.}, 'decoder_refresh_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 'bci_loop_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 'recording_loop_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, 'recording_loop_interval_data': {'required': True, 'type': 'float', 'min': 0., 'max': 10.}, } } } } } # print(schema) v = Validator() v.schema = schema v.allow_unknown = True validation_passed = v.validate(params, schema) errors = v.errors # print("Validation validation_passed: {}".format(validation_passed)) # print("Errors: {}".format(errors)) return validation_passed, errors if __name__ == '__main__': config_files = glob.glob('**/config.yaml', recursive=True) fname = config_files[0] validation_passed, errors = validate_schema(fname) # print(validation_passed) if validation_passed is True: print("Config file validation success! All values are as expected.") else: print("Config file validation failed with the following errors:") for key, value in errors.items(): print("\nerror in {}: {}".format(key, errors[key]))