Search in sources :

Example 1 with AttrValMap

use of com.qlangtech.tis.util.AttrValMap in project plugins by qlangtech.

the class TestRockMqPluginValidate method validatePluginPostForm.

private void validatePluginPostForm(String jsonPath, Context context) throws IOException {
    // DefaultFieldErrorHandler fieldErrorHandler = new DefaultFieldErrorHandler();
    DelegateControl4JsonPostMsgHandler fieldErrorHandler = null;
    List<AttrValMap> attrValMaps = null;
    try {
        try (InputStream reader = this.getClass().getResourceAsStream(jsonPath)) {
            JSONArray itemsArray = JSONArray.parseArray(IOUtils.toString(reader, TisUTF8.get()));
            attrValMaps = AttrValMap.describableAttrValMapList(fieldErrorHandler, itemsArray, Optional.empty());
        }
    } catch (Exception e) {
        throw new IllegalStateException("jsonPath:" + jsonPath, e);
    }
    assertNotNull(attrValMaps);
    assertEquals(1, attrValMaps.size());
    AttrValMap attrValMap = attrValMaps.get(0);
    Descriptor.PluginValidateResult validateResult = attrValMap.validate(context, true);
    assertFalse("validate false", validateResult.isValid());
}
Also used : DelegateControl4JsonPostMsgHandler(com.qlangtech.tis.runtime.module.misc.impl.DelegateControl4JsonPostMsgHandler) InputStream(java.io.InputStream) JSONArray(com.alibaba.fastjson.JSONArray) Descriptor(com.qlangtech.tis.extension.Descriptor) AttrValMap(com.qlangtech.tis.util.AttrValMap) IOException(java.io.IOException)

Example 2 with AttrValMap

use of com.qlangtech.tis.util.AttrValMap in project tis by qlangtech.

the class Descriptor method isValid.

private boolean isValid(IControlMsgHandler msgHandler, Context context, boolean bizValidate, Optional<IPropertyType.SubFormFilter> subFormFilter, PluginFormProperties propertyTypes, PostFormVals postFormVals) {
    Objects.requireNonNull(postFormVals, "postFormVals can not be null");
    Map<String, JSONObject> formData = postFormVals.rawFormData;
    boolean valid = true;
    String attr;
    PropertyType attrDesc;
    JSONObject valJ;
    String impl;
    String attrVal;
    for (Map.Entry<String, PropertyType> entry : propertyTypes.getKVTuples()) {
        attr = entry.getKey();
        attrDesc = entry.getValue();
        valJ = formData.get(attr);
        if (valJ == null && attrDesc.isInputRequired()) {
            addFieldRequiredError(msgHandler, context, attr);
            valid = false;
            continue;
        }
        if (valJ == null) {
            valJ = new JSONObject();
        }
        if (attrDesc.isDescribable()) {
            JSONObject descVal = valJ.getJSONObject("descVal");
            impl = descVal.getString("impl");
            if (StringUtils.isBlank(impl)) {
                addFieldRequiredError(msgHandler, context, attr);
                valid = false;
                continue;
            }
            AttrValMap attrValMap = AttrValMap.parseDescribableMap(msgHandler, subFormFilter, descVal);
            pushFieldStack(context, attr, 0);
            try {
                if (!attrValMap.validate(context, bizValidate).isValid()) {
                    valid = false;
                    continue;
                }
            } finally {
                popFieldStack(context);
            }
        } else {
            if (attrDesc.typeIdentity() == FormFieldType.MULTI_SELECTABLE.getIdentity()) {
                List<FormFieldType.SelectedItem> selectedItems = getSelectedMultiItems(valJ);
                // }
                if (selectedItems.size() < 1) {
                    // 没有选中
                    Validator[] validators = attrDesc.getValidator();
                    for (Validator v : validators) {
                        if (v == Validator.require) {
                            v.validate(msgHandler, context, attr, StringUtils.EMPTY);
                        }
                    }
                } else if (this instanceof FormFieldType.IMultiSelectValidator) {
                    FormFieldType.IMultiSelectValidator multiSelectValidator = (FormFieldType.IMultiSelectValidator) this;
                    if (!multiSelectValidator.validate(msgHandler, subFormFilter, context, attr, selectedItems)) {
                        valid = false;
                        break;
                    }
                }
            } else {
                // single value
                boolean containVal = valJ.containsKey(KEY_primaryVal);
                if (!containVal && attrDesc.isInputRequired()) {
                    addFieldRequiredError(msgHandler, context, attr);
                    valid = false;
                    continue;
                }
                if (containVal) {
                    attrVal = valJ.getString(KEY_primaryVal);
                    postFormVals.fieldVals.put(attr, attrVal);
                    Validator[] validators = attrDesc.getValidator();
                    for (Validator v : validators) {
                        if (!v.validate(msgHandler, context, attr, attrVal)) {
                            valid = false;
                            break;
                        }
                    }
                    try {
                        Method validateMethod = this.validateMethodMap.get(attr);
                        if (validateMethod != null && StringUtils.isNotEmpty(attrVal)) {
                            if (!(boolean) validateMethod.invoke(this, msgHandler, context, attr, attrVal)) {
                                valid = false;
                            }
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    // end for
    return valid;
}
Also used : FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) AttrValMap(com.qlangtech.tis.util.AttrValMap) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) ImmutableMap(com.google.common.collect.ImmutableMap) AttrValMap(com.qlangtech.tis.util.AttrValMap) Validator(com.qlangtech.tis.plugin.annotation.Validator)

Aggregations

AttrValMap (com.qlangtech.tis.util.AttrValMap)2 IOException (java.io.IOException)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Descriptor (com.qlangtech.tis.extension.Descriptor)1 FormFieldType (com.qlangtech.tis.plugin.annotation.FormFieldType)1 Validator (com.qlangtech.tis.plugin.annotation.Validator)1 DelegateControl4JsonPostMsgHandler (com.qlangtech.tis.runtime.module.misc.impl.DelegateControl4JsonPostMsgHandler)1 InputStream (java.io.InputStream)1