Skip to content

Commit c6d99d8

Browse files
author
BrunoSilvaAndrade
committed
Relaxing schema using, re-writing schema tests
1 parent e9f3cf2 commit c6d99d8

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

pyconfigparser.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ def get_config(cls, schema: dict = None, config_dir: str = 'config', file_name:
7171

7272
@classmethod
7373
def __create_new_instance(cls, schema, config_dir, file_name):
74-
cls.__check_schema(schema)
7574
file_path = cls.__get_file_path(config_dir, file_name)
7675
parser = cls.__get_file_parser(file_path)
7776
file_buff = cls.__get_file_buff(file_path)
7877

7978
try:
80-
config = Schema(schema).validate(parser(file_buff))
79+
config = cls.__validate_schema(schema, parser(file_buff))
8180
return cls.__dict_2_obj(config)
8281
except SchemaError as e:
8382
raise ConfigError('Schema validation error', e)
@@ -103,11 +102,13 @@ def __get_file_path(cls, config_dir, file_name):
103102
raise ConfigFileNotFoundError(f'Config file {file_path}{file_name} was not found')
104103

105104
@classmethod
106-
def __check_schema(cls, schema):
105+
def __validate_schema(cls, schema, config_obj):
107106
if schema is None:
108-
raise ConfigError('The schema config can not be None')
109-
if type(schema) is not dict:
110-
raise ConfigError('The first config\'s schema element should be a Map')
107+
return config_obj
108+
elif type(schema) not in (dict, list):
109+
raise ConfigError('The first config\'s schema element should be a Map or a List')
110+
111+
return Schema(schema).validate(config_obj)
111112

112113
@classmethod
113114
def __get_file_buff(cls, path_file: str):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name='python-config-parser',
8-
version='2.0.4',
8+
version='2.0.5',
99
author='Bruno Silva de Andrade',
1010
author_email='brunojf.andrade@gmail.com',
1111
description='Project created to given the possibility of create dynamics config files',

test_configparser.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def test_new(self):
1717
self.assertRaises(RuntimeError, Config)
1818

1919
def test_schema_checking(self):
20-
self.assertRaises(ConfigError, Config.get_config)
21-
self.assertRaises(ConfigError, Config.get_config, [])
20+
self.assertRaises(ConfigError, Config.get_config, 1)
2221

2322
def test_config_without_file(self):
2423
self.assertRaises(ConfigFileNotFoundError, Config.get_config, SIMPLE_SCHEMA_CONFIG,
@@ -27,7 +26,7 @@ def test_config_without_file(self):
2726

2827
def test_undefined_env_var(self):
2928
try:
30-
Config.get_config(SIMPLE_SCHEMA_CONFIG, file_name='config.yaml')
29+
Config.get_config(file_name='config.yaml')
3130
except Exception as e:
3231
self.assertIn('Environment', str(e))
3332

0 commit comments

Comments
 (0)