From d79e7198043f8b2a2c8c4e5651d2ce323fd84963 Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Thu, 21 May 2020 15:40:57 +0200 Subject: schema: Allow objects registering their own schema types Change-Id: I998c8674a55531909bfeac420064c3f238cea126 --- selftest/schema_test/schema_case_06.conf | 29 +++++++++++++++++++++++++++++ selftest/schema_test/schema_test.ok | 17 +++++++++++++++++ selftest/schema_test/schema_test.py | 7 +++++++ 3 files changed, 53 insertions(+) create mode 100644 selftest/schema_test/schema_case_06.conf (limited to 'selftest') diff --git a/selftest/schema_test/schema_case_06.conf b/selftest/schema_test/schema_case_06.conf new file mode 100644 index 0000000..ea7f45f --- /dev/null +++ b/selftest/schema_test/schema_case_06.conf @@ -0,0 +1,29 @@ +schema: + handover: + threshold: 'uint' + myvar: 'test_type' + anothervar: 'another_type' + +tests: + - foobar: + prefix: + handover: + myvar: 'valid_value1' + anothervar: 'unique_val_ok' + threshold: 2 + - foobar: + prefix: + handover: + myvar: 'valid_value2' + - foobar: + prefix: + handover: + threshold: 0 + - foobar: + prefix: + handover: + myvar: 'invalid_val' + - foobar: + prefix: + handover: + anothervar: 'another_invalid_val' diff --git a/selftest/schema_test/schema_test.ok b/selftest/schema_test/schema_test.ok index 2c4cd6a..846caae 100644 --- a/selftest/schema_test/schema_test.ok +++ b/selftest/schema_test/schema_test.ok @@ -61,3 +61,20 @@ validating tests[6] --- -: ERR: ValueError: config item is a list, should be 'str': 'foobar.prefix.hey.ho.letsgo[]' Validation: Error ---------------------- +schema_case_06.conf: +{'foobar.prefix.handover.anothervar': 'another_type', + 'foobar.prefix.handover.myvar': 'test_type', + 'foobar.prefix.handover.threshold': 'uint'} +validating tests[0] +Validation: OK +validating tests[1] +Validation: OK +validating tests[2] +Validation: OK +validating tests[3] +--- foobar.prefix.handover.myvar: ERR: ValueError: Invalid value 'invalid_val' for schema type 'test_type' (validator: test_validator) +Validation: Error +validating tests[4] +--- foobar.prefix.handover.anothervar: ERR: ValueError: Invalid value 'another_invalid_val' for schema type 'another_type' (validator: ) +Validation: Error +---------------------- diff --git a/selftest/schema_test/schema_test.py b/selftest/schema_test/schema_test.py index 3cf2799..bffa601 100755 --- a/selftest/schema_test/schema_test.py +++ b/selftest/schema_test/schema_test.py @@ -25,6 +25,13 @@ def get_case_list(dir): li.append(f) return sorted(li) +def test_validator(val): + return val in ('valid_value1', 'valid_value2') + +schema.register_schema_types({'test_type': test_validator, + 'another_type': lambda val: val == 'unique_val_ok'}) + + print('==== Testing dynamically generated schemas ====') for f in get_case_list(_prep.script_dir): print('%s:' % f) -- cgit v1.2.3