Skip to content

Commit 035c04f

Browse files
committed
schemas: cli - record validation - markdown export
Signed-off-by: Pamfilos Fokianos <pamfilosf@gmail.com>
1 parent 5a115b2 commit 035c04f

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

cap/modules/schemas/cli.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@
6363
@click.option('--export', '-e',
6464
type=click.Path(),
6565
help='A file where, the validation errors can be saved.')
66+
@click.option('--export-type', '-et',
67+
default=None,
68+
type=click.Choice(['md'], case_sensitive=False),
69+
help='The export type that will be used for output.')
6670
@with_appcontext
67-
def validate(schema_url, ana_type, ana_version, compare_with, status, export):
71+
def validate(schema_url, ana_type, ana_version, compare_with,
72+
status, export, export_type):
6873
"""
6974
Validate deposit or record metadata based on their schema. Provide the
7075
schema url OR ana-type and version, as well as the schema version that you
@@ -116,7 +121,9 @@ def validate(schema_url, ana_type, ana_version, compare_with, status, export):
116121
total_errors = []
117122
for pid in pids:
118123
cap_record = cap_record_class.get_record(pid)
119-
124+
cap_record_pid = cap_record.get('_deposit', {}).get('id')
125+
cap_record_cadi_id = cap_record.get('basic_info', {}).get('cadi_id')
126+
cap_host = 'https://analysispreservation.cern.ch/drafts'
120127
# get the url of the schema version, used for validation
121128
if compare_with:
122129
cap_record['$schema'] = schema_name_to_url(
@@ -125,8 +132,22 @@ def validate(schema_url, ana_type, ana_version, compare_with, status, export):
125132
cap_record.validate()
126133
click.secho(f'No errors found in record {pid}', fg='green')
127134
except DepositValidationError as exc:
128-
error_list = '\n'.join(str(err.res) for err in exc.errors)
129-
msg = f'Errors in {pid}:\n{error_list}'
135+
if export_type == 'md':
136+
msg = '- [ ] Errors in **CADI ID:** ' + \
137+
f'{cap_record_cadi_id or "?"}' + \
138+
f' - **[link]({cap_host}/{cap_record_pid})** :\n'
139+
msg += "\n| Field Path | Error | \n| ---------- | ----- | \n"
140+
for err in exc.errors:
141+
_err = err.res
142+
msg += f"| ```{_err.get('field')}``` |"
143+
msg += f" {_err.get('message')} | \n"
144+
msg += "----\n"
145+
else:
146+
error_list = '\n'.join(str(err.res) for err in exc.errors)
147+
msg = f'Errors in {pid} - CADI ' + \
148+
f'id: {cap_record_cadi_id or "?"}' + \
149+
f' - {cap_host}/{cap_record_pid} :\n{error_list}'
150+
130151
click.secho(msg, fg='red')
131152

132153
if export:

tests/integration/cli/test_schema_records_validation.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_validate_with_different_version_records(
125125
'title': 'test-schema',
126126
'type': 'object',
127127
'properties': {
128-
'basic_info': {
128+
'basic': {
129129
'type': 'string',
130130
'enum': ['test1', 'test2']
131131
}
@@ -134,7 +134,7 @@ def test_validate_with_different_version_records(
134134
create_deposit(users['cms_user'], 'test',
135135
{
136136
"$schema": "https://analysispreservation.cern.ch/schemas/deposits/records/test-v1.0.0.json",
137-
'basic_info': 'test1'
137+
'basic': 'test1'
138138
},
139139
experiment='CMS')
140140

@@ -144,15 +144,14 @@ def test_validate_with_different_version_records(
144144
'title': 'test-schema',
145145
'type': 'object',
146146
'properties': {
147-
'basic_info': {
147+
'basic': {
148148
'type': 'string',
149149
'enum': ['test3', 'test4']
150150
}
151151
}
152152
})
153153

154154
res = cli_runner('fixtures validate -a test -v 1.0.0 -c 2.0.0')
155-
156155
assert res.exit_code == 0
157156
assert '1 record(s) of test found.' in res.output
158157
assert "'test1' is not one of ['test3', 'test4']" in res.output
@@ -166,7 +165,7 @@ def test_validate_with_different_version_records_and_exported_file(
166165
'title': 'test-schema',
167166
'type': 'object',
168167
'properties': {
169-
'basic_info': {
168+
'basic': {
170169
'type': 'string',
171170
'enum': ['test1', 'test2']
172171
}
@@ -175,7 +174,7 @@ def test_validate_with_different_version_records_and_exported_file(
175174
create_deposit(users['cms_user'], 'test',
176175
{
177176
"$schema": "https://analysispreservation.cern.ch/schemas/deposits/records/test-v1.0.0.json",
178-
'basic_info': 'test1'
177+
'basic': 'test1'
179178
},
180179
experiment='CMS')
181180

@@ -185,7 +184,7 @@ def test_validate_with_different_version_records_and_exported_file(
185184
'title': 'test-schema',
186185
'type': 'object',
187186
'properties': {
188-
'basic_info': {
187+
'basic': {
189188
'type': 'string',
190189
'enum': ['test3', 'test4']
191190
}

0 commit comments

Comments
 (0)