From df932681367167dfc03ab53587b08be3784032a9 Mon Sep 17 00:00:00 2001 From: Junjie Mao Date: Wed, 25 May 2022 16:02:54 +0800 Subject: [PATCH] config_tools: pretty-print JSON schema violations The default error messages provided by the ajv validation library read like this: * must have required property MEMORY.STACK_SIZE * must match pattern "" Such messages may look confusing as users are not supposed to understand the internal naming of the config items or the regular expressions used to validate strings. This patch enables the XML schema to include 'acrn:errormsg' annotations which is a dictionary from error types to customized error messages. This mechanism is used to show more user-friendly messages upon common errors such as missing or invalid data in required config item. Tracked-On: #6691 Signed-off-by: Junjie Mao --- .../scenario_config/jsonschema/converter.py | 19 +++++++++++++++++-- misc/config_tools/schema/VMtypes.xsd | 15 ++++++++++----- misc/config_tools/schema/config.xsd | 15 +++++++++------ misc/config_tools/schema/types.xsd | 6 ++++-- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/misc/config_tools/scenario_config/jsonschema/converter.py b/misc/config_tools/scenario_config/jsonschema/converter.py index 086aed148..8274c1bf0 100644 --- a/misc/config_tools/scenario_config/jsonschema/converter.py +++ b/misc/config_tools/scenario_config/jsonschema/converter.py @@ -151,6 +151,17 @@ class XS2JS: if '@acrn:widget-options' in annotation: js_ele['ui:options'] = eval(f"{{{annotation['@acrn:widget-options']}}}") + def convert_errormsg_config(self, annotation, js_ele): + if '@acrn:errormsg' in annotation: + opts = eval(f"{{{annotation['@acrn:errormsg']}}}") + + # An items of an error schema requires an "err:" prefix. + keys = list(opts.keys()) + for key in keys: + opts[f"err:{key}"] = opts.pop(key) + + js_ele.update(opts) + def xst2jst(self, type_name) -> str: """convert xml schema type name to json schema type name""" if type_name in self.xst2jst_mapping: @@ -195,9 +206,10 @@ class XS2JS: enum_names.append(enum_name) js_st["enumNames"] = enum_names - # widget and its options + # widget configs and error messages if 'xs:annotation' in obj: self.convert_widget_config(obj['xs:annotation'], js_st) + self.convert_errormsg_config(obj['xs:annotation'], js_st) js_st.update(self.xsa2jsa(restriction)) return js_st @@ -298,7 +310,7 @@ class XS2JS: if '@maxOccurs' in element: # ui:options seen at this moment are copied from the annotation of the type. - possible_keys = ['type', '$ref', 'oneOf', 'ui:options'] + possible_keys = ['type', '$ref', 'oneOf', 'ui:options', 'err:required', 'err:pattern'] convert_to_items_success = False js_ele['items'] = {} for possible_key in possible_keys: @@ -353,6 +365,9 @@ class XS2JS: # widget and its options self.convert_widget_config(element['xs:annotation'], js_ele) + # Error messages + self.convert_errormsg_config(element['xs:annotation'], js_ele) + properties[name] = js_ele # build result diff --git a/misc/config_tools/schema/VMtypes.xsd b/misc/config_tools/schema/VMtypes.xsd index 5bcff78c0..603d63c26 100644 --- a/misc/config_tools/schema/VMtypes.xsd +++ b/misc/config_tools/schema/VMtypes.xsd @@ -71,12 +71,14 @@ - + Specify the enclave page cache (EPC) section base for Intel Software Guard Extensions (SGX). Must be page aligned. - + Specify the enclave page cache (EPC) section size in bytes for Intel Software Guard Extensions (SGX). Must be page aligned. @@ -86,12 +88,14 @@ - + Specify the starting address for non-contiguous allocation. - + Specify the physical memory size for non-contiguous allocation in megabytes. The size is a subset of the VM's total memory size specified on the Basic tab. @@ -275,7 +279,8 @@ The size is a subset of the VM's total memory size specified on the Basic tab. + acrn:options="//usb_device/@description" acrn:options-sorted-by="lambda s: s" + acrn:errormsg="'required': 'USB device required. If no USB device is available, click the X at the top right corner of this entry to remove.'"> Select the USB physical bus and port number that will be emulated by the ACRN Device Model for this VM. USB 3.0, 2.0, and 1.0 are supported. diff --git a/misc/config_tools/schema/config.xsd b/misc/config_tools/schema/config.xsd index 61e8d18c2..07777522e 100644 --- a/misc/config_tools/schema/config.xsd +++ b/misc/config_tools/schema/config.xsd @@ -124,7 +124,8 @@ - + Specify the size of the memory stack in bytes for each physical CPU. For example, if you specify 8 kilobytes, each CPU will get its own 8-kilobyte stack. @@ -299,9 +300,10 @@ Refer to :ref:`vuart_config` for detailed vUART settings. - - Specify the name used to identify this VM. The VM name will be shown in the hypervisor console vm_list command. - + + Specify the name used to identify this VM. The VM name will be shown in the hypervisor console vm_list command. + @@ -484,8 +486,9 @@ mouse, and tablet. It sends Linux input layer events over virtio.The virtio-blk device presents a block device to the VM. Each virtio-blk device appears as a disk inside the VM. - - + + diff --git a/misc/config_tools/schema/types.xsd b/misc/config_tools/schema/types.xsd index a637654dc..8450fffae 100644 --- a/misc/config_tools/schema/types.xsd +++ b/misc/config_tools/schema/types.xsd @@ -24,7 +24,8 @@ - + An Integer value in hexadecimal format (with a leading ``0x``). @@ -177,7 +178,8 @@ Read more about the available scheduling options in :ref:`cpu_sharing`. - + A string of up to 15 letters, digits, ``_``, or ``-``.