入参校验产品化 schema_个人渣记录仅为自己搜索用的博客-CSDN博客
java端jsonSchema相关
目录
每个key一个validater , 独立分析且可能会被忽略.
支持自定义的key.
官网文档自定义custom validater ,
自定义format
format能否有多个值?
format 仅支持string
初始化 DateTimeValidator
例子
日志
所以值是long, 你也可以配置format和maxLength.
format 内部会分析值类型, 如果不是string , 会忽略该key. 所以时间格式,配置了格式,如果传了数字会被忽略掉. 但是会被类型validater给报错, 但是设置了某个格式,就必须传long, 对于python这种来说, 无强类型, 就比较麻烦. 工具类不会自动帮你转.
,需要初始化的时候设入: https://github.com/networknt/json-schema-validator/blob/master/doc/validators.md
AbstractJsonValidator 和 BaseJsonValidator ( 包含自带validator共有的函数, 要传ValidatorTypeCode.)
结论不能.
自定义的string format可以使用. 详见
fpe的: addFormatValidator
networknt: FormatKeyword formatKeyword = new FormatKeyword(ValidatorTypeCode.FORMAT, map);
JsonMetaSchema metaSchema = jsonSchemaVersion.getInstance()
metaSchema.getKeywords().put(formatKeyword.getValue(), formatKeyword);
ajv: 的 addFormat能力
借鉴自chatgpt , 谷歌了半天没答案.
Q: "networknt JsonSchemaFactory Keyword "
A: 如下
static String schema = "{\n"
+ " \"$ref\": \"#/definitions/Welcome\",\n"
+ " \"definitions\": {\n"
+ " \"Welcome\": {\n"
+ " \"type\": \"object\",\n"
+ " \"additionalProperties\": false,\n"
+ " \"properties\": {\n"
+ " \"timePartition\": {\n"
+ " \"groovy\": 131,\n"
+ " \"type\": \"string\""
+ " }\n"
+ " },\n"
+ "
+ " \"title\": \"Welcome\"\n"
+ " }
+ " }\n"
+ "}\n";
static String requestJson = "{\n"
+ " \"timePartition\": \"2023-02-08 19:30:00\"\n"
+ "}";
自定义format的重要类AbstractFormat和format接口
相关类 FormatKeyword
FormatKeyword中new DateTimeValidator和FormatValidator, 其中 DateTimeValidator 自带, FormatValidator封装了 format , format的实现类有, 含接口用于校验.
FormatKeyword 运行期validate时初始化DateTimeValidator
validate时,动态将format转成Validator)
protected static JsonSchema getJsonSchemaFromJsonNodeVer(JsonNode jsonNode, SpecVersion.VersionFlag versionFlag) {
JsonSchemaVersion jsonSchemaVersion = JsonSchemaFactory.checkVersion(versionFlag);
JsonMetaSchema metaSchema = jsonSchemaVersion.getInstance();
GroovyKeyword value = new GroovyKeyword();
metaSchema.getKeywords().put(value.getValue(), value);
DateFormat dateFormat = new DateFormat();
Map map=new HashMap<>();
map.put(dateFormat.getName(),dateFormat);
;
for (Format o : JsonSchemaVersion.BUILTIN_FORMATS) {
map.put(o.getName(),o);
}
//FormatKeyword keyword = (FormatKeyword)metaSchema.getKeywords().get(ValidatorTypeCode.FORMAT.getValue());
FormatKeyword formatKeyword = new FormatKeyword(ValidatorTypeCode.FORMAT, map);
metaSchema.getKeywords().put(formatKeyword.getValue(), formatKeyword);
JsonSchemaFactory factory= JsonSchemaFactory.builder().defaultMetaSchemaURI(metaSchema.getUri()).addMetaSchema(metaSchema).build();
return factory.getSchema(jsonNode);
}
public static class DateFormat extends AbstractFormat {
public DateFormat() {
super("datetime", "(正确案例 2022-10-11 12:01:00)");
}
@Override
public boolean matches(String s) {
return s.matches("\\d{4}-\\d{1,2}-\\d{1,2} 11\\d{1,2}:\\d{1,2}:\\d{1,2}");
}
}
public static class GroovyKeyword extends AbstractKeyword {
private final Logger logger = LoggerFactory.getLogger(GroovyKeyword.class);
public GroovyKeyword() {
super("groovy");
}
@Override
public AbstractJsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) throws
JsonSchemaException, Exception {
System.out.println("GroovyKeyword_schemaPath:"+schemaPath+",schemaNodeValue:"+schemaNode+" parentSchemaNodeKv:"+ parentSchema+",validationContext="+validationContext);
String config = schemaNode.asText();
AbstractJsonValidator abstractJsonValidator = new AbstractJsonValidator() {
@Override
public Set validate(JsonNode node, JsonNode rootNode, String at) {
System.out.println("GroovyKeyword.validate_config:"+config+",path:"+at+",node:"+ node);
System.out.println("GroovyKeyword.validate_rootNode="+rootNode);
return Collections.emptySet();
}
};
return abstractJsonValidator;
}
}
GroovyKeyword_schemaPath:#/definitions/Welcome/properties/timePartition,schemaNodeValue:131 parentSchemaNodeKv:"#/definitions/Welcome/properties/timePartition" : {"groovy":131,"type":"string"},validationContext=com.networknt.schema.ValidationContext@205d38da
GroovyKeyword.validate_config:131 ,path:$.timePartition, node:"2023-02-08 19:30:00"
GroovyKeyword.validate_rootNode={"timePartition":"2023-02-08 19:30:00"}
validate_time=6