All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.devops4j.skeleton4j.validate.DefaultSkeleton4jValidator Maven / Gradle / Ivy

There is a newer version: 1.0.0-PRE7
Show newest version
package com.devops4j.skeleton4j.validate;

import com.devops4j.logtrace4j.ErrorContextFactory;
import com.devops4j.reflection4j.GlobalSystemMetadata;
import com.devops4j.reflection4j.MetaObject;
import com.devops4j.reflection4j.Reflector;

import javax.web.doc.RspCodeWritable;
import javax.web.doc.annotation.ApidocElement;
import javax.web.validate.Skeleton4jValidator;
import javax.web.validate.ValidateCause;
import javax.web.validate.ValidateContext;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Collection;

/**
 * Created by devops4j on 2018/1/4.
 */
public class DefaultSkeleton4jValidator implements Skeleton4jValidator {
    @Override
    public boolean validate(Object value, boolean throwException) {
        ValidateContext context = ValidateContext.builder().value(value).throwException(throwException).build();
        return validate(context);
    }

    @Override
    public boolean validate(Object value, RspCodeWritable response) {
        ValidateContext context = ValidateContext.builder().value(value).throwException(response == null).build();
        if (!validate(context)) {
            response.setRspCode("" + context.getCode());
            response.setRspDesc(context.getFailureField() + " " + context.getDesc());
            return false;
        }
        return true;
    }

    /**
     * 校验请求对象是否符合校验规则
     * 1.检查字段是否为支持的String,int,long
     * 2.检查字段是否必输
     * 3.如果字段为null,使用defaults进行赋值
     * 4.检查字段是否满足最短长度,最长长度
     * 5.如果字段为枚举类型,检查字段值是否符合枚举定义
     * 6.检查字段是否满足正则规则定义
     *
     * @param context 校验上下文
     */
    @Override
    public boolean validate(ValidateContext context) {
        MetaObject metaObject = context.getMetaObject();
        //校验输入是否为空
        for (String getterName : metaObject.getGetterNames()) {
            MetaObject getterMetaObject = metaObject.metaObjectForProperty(getterName);
            context.setFailureField(getterName);
            Reflector reflector = GlobalSystemMetadata.reflector(metaObject.getType());
            Field field = reflector.getField(getterName);
            ApidocElement apidocElement = field.getAnnotation(ApidocElement.class);
            if (apidocElement == null){
                continue;
            }
            if(!apidocElement.required() && getterMetaObject.getObject() == null){
                continue;
            }
            if(!apidocElement.required() && getterMetaObject.getObject().toString().isEmpty()){
                continue;
            }
            if(apidocElement.required() && getterMetaObject.getObject() == null){
                context.setCode(ValidateCause.IS_REQUIRED.getCode());
                context.setDesc(context.getCauses().get(ValidateCause.IS_REQUIRED));
                context.setFailureField(getterMetaObject.getFullName());
                if (context.isThrowException()) {
                    throw ErrorContextFactory.instance().code(Integer.toString(context.getCode()), context.getDesc()).solution("检查{}类的字段{},不允许为null", context.getValue().getClass(), getterName).runtimeException();
                }
                return false;
            }
            if (Collection.class.isAssignableFrom(getterMetaObject.getType())) {
                //进行集合校验,校验通过校验下一个字段,否则中断
                if (!CollectionValidator.validate(this, context, metaObject, getterMetaObject, getterName)) {
                    return false;
                }
            } else {
                if (PrimitiveTypeValidator.support(getterMetaObject.getType())) {//对象含有原生数据类型
                    //进行原生数据类型校验,校验通过校验下一个字段,否则中断
                    if (!PrimitiveTypeValidator.validate(this, context, metaObject, getterMetaObject, getterName)) {
                        return false;
                    }
                } else if (Serializable.class.isAssignableFrom(getterMetaObject.getType())) {//如果字段为Bean对象,则进行递归校验

                    ValidateContext context1 = new ValidateContext(context, getterMetaObject);
                    if (!validate(context1)) {//如果校验值为假,则阻断
                        context.setFailureField(context1.getFailureField());
                        context.setCode(context1.getCode());
                        context.setDesc(context1.getDesc());
                        context.setMetaObject(context1.getMetaObject());
                        context.setThrowException(context1.isThrowException());
                        context.setValue(context1.getValue());
                        return false;
                    }
                } else {
                    context.setCode(ValidateCause.NOT_SUPPORT_JAVA_TYPE.getCode());
                    context.setDesc(context.getCauses().get(ValidateCause.NOT_SUPPORT_JAVA_TYPE));
                    context.setFailureField(getterMetaObject.getFullName());
                    if (context.isThrowException()) {
                        throw ErrorContextFactory.instance().code(Integer.toString(context.getCode()), context.getDesc()).solution("修改{}类的字段{}的数据类型,或者检查是否未实现 'java.io.Serializable' 接口", context.getMetaObject().getType(), getterMetaObject.getFullName()).runtimeException();
                    }
                    return false;
                }
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy