Skip to content

Commit

Permalink
update schema
Browse files Browse the repository at this point in the history
  • Loading branch information
woutervh committed Dec 20, 2024
1 parent cddc41b commit a67a282
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 53 deletions.
122 changes: 78 additions & 44 deletions src/libranet_logging/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,92 @@

log = logging.getLogger(__name__)


logging_schema = {
"logdir": {"type": "string", "required": False},
"version": {"type": "integer", "required": True},
"disable_existing_loggers": {"type": "integer"},
"formatters": {
"type": "dict",
"required": True,
"valuesrules": {
"type": "dict",
"schema": {
"format": {"type": "string", "required": False},
"datefmt": {"type": "string", "required": False},
"type": "object",
"properties": {
# "logdir": {"type": "string", "required": False}, # custom field
"version": {"type": "integer", "enum": [1]}, # Version is required and must be 1
"disable_existing_loggers": {"type": "boolean"},
"formatters": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"format": {"type": "string"},
"datefmt": {"type": "string"},
},
"required": ["format"], # At least "format" is required in formatters
},
},
},
"handlers": {
"type": "dict",
"required": True,
"valuesrules": {
"type": "dict",
"schema": {
"class": {"type": "string", "required": True},
# logstash-handler sets the correct formatter via code
# therefore the formatter-key in not required in logging.yml
"formatter": {"type": "string", "required": False},
"level": {"type": "string", "required": True},
"encoding": {"type": "string", "required": False},
"filename": {"type": "string", "required": False},
"stream": {"type": "string", "required": False},
"maxBytes": {"type": "integer", "required": False},
"backupCount": {"type": "integer", "required": False},
"handlers": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"class": {"type": "string"}, # Required
"level": {"type": "string"},
"formatter": {"type": "string"},
"stream": {"type": "string"},
"filename": {"type": "string"},
"encoding": {"type": "string"},
"mode": {"type": "string"},
"maxBytes": {"type": "integer"},
"backupCount": {"type": "integer"},
"delay": {"type": "boolean"},
"filters": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["class"], # "class" is always required for handlers
},
},
},
"loggers": {
"type": "dict",
"required": True,
"valuesrules": {
"type": "dict",
"schema": {
"handlers": {"type": "list", "required": False},
"level": {"type": "string", "required": False},
"propagate": {"type": "boolean", "required": False},
"loggers": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"level": {"type": "string"},
"handlers": {
"type": "array",
"items": {"type": "string"},
},
"propagate": {"type": "boolean"},
"filters": {
"type": "array",
"items": {"type": "string"},
},
},
},
},
"root": {
"type": "object",
"properties": {
"level": {"type": "string"},
"handlers": {
"type": "array",
"items": {"type": "string"},
},
"filters": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["level", "handlers"], # "level" and "handlers" are required for root
},
"filters": {
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"name": {"type": "string"},
"args": {"type": "object"},
},
},
},
},
"root": {
"type": "dict",
"required": True,
"schema": {"handlers": {"type": "list", "required": True}, "level": {"type": "string"}},
},
"required": ["version"], # "version" is always required
"additionalProperties": True, # Disallow unknown properties at the top level
}


Expand Down
13 changes: 4 additions & 9 deletions tests/test_logconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ def test_initialize_with_invalid_yaml2(tests_dir):

logging_yml = tests_dir / "logging_invalid_schema.yaml"

# with pytest.raises(SchemaValidationError) as excinfo:
with pytest.raises(ValueError) as excinfo:
with pytest.raises(SchemaValidationError) as excinfo:
initialize(logging_yml)

# expected = (
# f"logconfig {logging_yml} contains errors: [('formatters', ['required field']), "
# "('handlers', ['required field']), ('loggers', ['required field']), "
# "('root', ['required field']), ('version', ['required field'])]"
# )
# assert excinfo.value.args[0] == expected
assert excinfo.value.args[0] == "dictionary doesn't specify a version"
expected = f"logconfig {logging_yml} contains errors: 'version' is a required property"
assert excinfo.value.args[0] == expected



def test_initialize_with_valid_yaml(env, tests_dir):
Expand Down

0 comments on commit a67a282

Please sign in to comment.