Skip to content

Commit

Permalink
Merge pull request #125 from kba/stricter-param-checking
Browse files Browse the repository at this point in the history
ocrd ocrd-tool .. tool parse-params does validation now
  • Loading branch information
kba authored Jun 19, 2018
2 parents 4188bdd + 71a6ae8 commit 2e2bca9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Added:
* CLI: `ocrd workspace set-id MODS_IDENTIFIER_PURL` to set mods:identifier
* CLI: `ocrd workspace get-id` to get mods:identifier

Fixed:

* CLI: `cord ocrd-tool tool parse-params` validate as well as merges with default
* Parameter validation: Check whether parameter is `required: true`

## [0.3.1] - 2018-06-18

Added
Expand Down
6 changes: 5 additions & 1 deletion ocrd/cli/ocrd_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from json import dumps, loads
import codecs
import sys

import click

Expand Down Expand Up @@ -124,7 +125,10 @@ def ocrd_tool_tool_parse_params(ctx, parameters, json):
with open(parameters, 'r') as f:
parameters = loads(f.read())
parameterValidator = ParameterValidator(ctx.json['tools'][ctx.tool_name])
parameterValidator.validate(parameters)
report = parameterValidator.validate(parameters)
if not report.is_valid:
print(report.to_xml())
sys.exit(1)
if json:
print(dumps(parameters))
else:
Expand Down
10 changes: 9 additions & 1 deletion ocrd/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,17 @@ class ParameterValidator(JsonValidator):

def __init__(self, ocrd_tool):
# TODO grep required properties
required = []
p = ocrd_tool['parameters']
for n in p:
if 'required' in p[n]:
if p[n]['required']:
required.append(n)
del(p[n]['required'])
super(ParameterValidator, self).__init__({
"type": "object",
"properties": ocrd_tool['parameters']
"required": required,
"properties": p
}, DefaultValidatingDraft4Validator)


Expand Down

0 comments on commit 2e2bca9

Please sign in to comment.