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.MetaObject;

import javax.web.validate.Skeleton4jValidator;
import javax.web.validate.ValidateCause;
import javax.web.validate.ValidateContext;
import java.io.Serializable;
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);
    }

    /**
     * 校验请求对象是否符合校验规则
     * 1.检查字段是否为支持的String,int,long
     * 2.检查字段是否必输
     * 3.如果字段为null,使用defaults进行赋值
     * 4.检查字段是否满足最短长度,最长长度
     * 5.如果字段为枚举类型,检查字段值是否符合枚举定义
     * 6.检查字段是否满足正则规则定义
     *
     * @param context 校验上下文
     */
    @Override
    public boolean validate(ValidateContext context) {
        if (context == null) {

        }
        MetaObject metaObject = context.getMetaObject();
        //校验输入是否为空
        for (String getterName : metaObject.getGetterNames()) {
            MetaObject getterMetaObject = metaObject.metaObjectForProperty(getterName);
            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)) {//如果校验值为假,则阻断
                        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("修改{}类的字段{}的数据类型", context.getMetaObject().getType(), getterMetaObject.getFullName()).runtimeException();
                    }
                    return false;
                }
            }
        }
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy