com.devops4j.skeleton4j.validate.Skeleton4jValidator.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of skeleton4j-validator Show documentation
Show all versions of skeleton4j-validator Show documentation
skeleton4j validator, use skeleton4j annoation to validate.
function SkeletonHashTable() {
var size = 0;
var entry = new Object();
this.put = function (key, value) {
if (!this.containsKey(key)) {
size++;
}
entry[key] = value;
}
this.get = function (key) {
return this.containsKey(key) ? entry[key] : null;
}
this.remove = function (key) {
if (this.containsKey(key) && (delete entry[key])) {
size--;
}
}
this.containsKey = function (key) {
return (key in entry);
}
this.containsValue = function (value) {
for (var prop in entry) {
if (entry[prop] == value) {
return true;
}
}
return false;
}
this.getValues = function () {
var values = new Array();
for (var prop in entry) {
values.push(entry[prop]);
}
return values;
}
this.getKeys = function () {
var keys = new Array();
for (var prop in entry) {
keys.push(prop);
}
return keys;
}
this.size = function () {
return size;
}
this.clear = function () {
size = 0;
entry = new Object();
}
}
/**
* 定义校验上下文
* @param value0 校验值
* @param required0 是否必输
* @param minLen0 最小长度
* @param maxLen0 最大长度
* @returns {Object} 上下文
* @constructor 上下文
*/
var ValidateContext = function (name0, value0, required0, minLen0, maxLen0) {
var instance = new Object();
var name = null;
var value = null;
var required = true;
var regexes = [];
var minLen = 0;
var maxLen = 255;
var enums = new SkeletonHashTable();
var format = null;
var code = "0";
var desc = "成功";
var causes = new SkeletonHashTable();
if (name0 != undefined) {
name = name0;
}
if (value0 != undefined) {
value = value0;
}
if (required0 != undefined) {
required = required0;
}
if (minLen0 != undefined && minLen0 != null) {
minLen = minLen0;
}
if (maxLen0 != undefined && maxLen0 != null) {
maxLen = maxLen0;
}
instance.setName = function (name0) {
name = name0;
};
instance.name = function () {
return name;
};
instance.setValue = function (value0) {
value = value0;
};
instance.value = function () {
return value;
};
instance.setRequired = function (required0) {
required = required0;
};
instance.required = function () {
return required;
};
instance.addRegex = function (regex) {
regexes.push(regex);
}
instance.regexes = function () {
return regexes;
}
instance.setMinLen = function (minLen0) {
minLen = minLen0;
};
instance.minLen = function () {
return minLen;
};
instance.setMaxLen = function (maxLen0) {
maxLen = maxLen0;
};
instance.maxLen = function () {
return maxLen;
};
instance.enums = function () {
return enums;
}
instance.defineEnum = function (code, desc) {
enums.put(code, desc);
}
instance.setFormat = function (format0) {
format = format0;
};
instance.format = function () {
return format;
};
instance.setCode = function (code0) {
code = code0;
};
instance.code = function () {
return code;
};
instance.setDesc = function (desc0) {
desc = desc0;
};
instance.desc = function () {
return desc;
};
instance.causes = function () {
return causes;
};
instance.cause = function (code, desc) {
causes.put(code, desc);
};
causes.put("0", "校验通过");
causes.put("1", "校验数据为空");
causes.put("2", "字段为必输");
causes.put("3", "字段小于最小长度");
causes.put("4", "字段大于最大长度");
causes.put("5", "字段不为固定长度");
causes.put("6", "字段不能匹配表达式");
causes.put("7", "字段不能匹配枚举定义");
causes.put("8", "枚举未实现枚举接口");
causes.put("9", "不支持的JAVA数据类型");
causes.put("10", "枚举未定义工厂方法");
return instance;
};
/**
* 校验器
* @returns {Object} 校验器
* @constructor 构建一个校验器
*/
var Skeleton4jValidator = function () {
var instance = new Object();
/**
* 校验字段
* @param ctx 校验上下文
* @returns {boolean} 是否通过校验
*/
instance.validate = function (ctx) {
//如果是非必输且值为null,通过
if (!ctx.required() && ctx.value() == null) {
return true;
}
//如果是非必输且值为"",通过
if (!ctx.required() && ctx.value() == "") {
return true;
}
//如果是必输且值为null,失败
if (ctx.required() && ctx.value() == null) {
ctx.setCode("1");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
//如果是必输且值为"",失败
if (ctx.required() && ctx.value() == "") {
ctx.setCode("1");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
//如果长度大于最大长度,失败
if (ctx.minLen() == ctx.maxLen() && ctx.value().toString().length != ctx.maxLen()) {
ctx.setCode("5");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
//如果长度小于最小长度,失败
if (ctx.value().toString().length < ctx.minLen()) {
ctx.setCode("3");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
//如果长度大于最大长度,失败
if (ctx.value().toString().length > ctx.maxLen()) {
ctx.setCode("4");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
if (ctx.enums().size() > 0) {
var desc = ctx.enums().get(ctx.value());
if (desc == undefined || desc == null) {
ctx.setCode("7");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
} else if (ctx.regexes().length > 0) {
var found = false;
//如果不能匹配正则,失败
for (var idx in ctx.regexes()) {
var regx = new RegExp(ctx.regexes()[idx]);
if (regx.test(ctx.value())) {
found = true;
}
}
if (!found) {
ctx.setCode("6");
ctx.setDesc("'" + ctx.name() + ctx.causes().get(ctx.code()));
return false;
}
} else {
//TODO
}
};
return instance;
};