-
Notifications
You must be signed in to change notification settings - Fork 497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] 支持JSONSchema #239
Labels
enhancement
New feature or request
Milestone
Comments
收到需求,预计会排到6月份做 |
wenshao
changed the title
[FEATURE] 有没有针对 JSON Schema 相关的 规划(例如:校验,生成,运算等操作)呢?
[FEATURE] 支持JSONSchema
May 15, 2022
已经支持JSONSchema,你可以用2.0.4-SNAPSHOT版本试用。
使用例子1 直接使用JSONSchema校验 JSONSchema jsonSchema = JSON.parseObject("{\n" +
" \"properties\": {\n" +
" \"country\": {\n" +
" \"const\": \"United States of America\"\n" +
" }\n" +
" }\n" +
"}").to(JSONSchema::of);
assertTrue(JSON.parseObject("{ \"country\": \"United States of America\" }")
.isValid(jsonSchema));
assertFalse(JSON.parseObject("{ \"country\": \"Canada\" }")
.isValid(jsonSchema)
); 使用例子2 配置JSONType @Test
public void test26() {
JSON.parseObject("{}", Bean26.class);
JSON.parseObject("{\"id\":123}", Bean26.class);
JSON.parseObject("{\"name\":\"xxx\"}", Bean26.class);
assertThrows(JSONSchemaValidException.class,
() -> JSON.parseObject("{\"id\":\"123\", \"name\":\"xx\"}", Bean26.class)
);
}
@JSONType(schema = "{'maxProperties':1}")
public static class Bean26 {
public Integer id;
public String name;
} 使用例子3 使用JSONField配置Schema校验 @Test
public void test25() {
JSON.parseObject("{\"value\":{}}", Bean25.class);
JSON.parseObject("{\"value\":{\"id\":123}}", Bean25.class);
JSON.parseObject("{\"value\":{\"name\":\"xxx\"}}", Bean25.class);
assertThrows(JSONSchemaValidException.class,
() -> JSON.parseObject("{\"value\":{\"id\":\"123\", \"name\":\"xx\"}}", Bean25.class)
);
}
public static class Bean25 {
@JSONField(schema = "{'maxProperties':1}")
public Item value;
}
public static class Item {
public Integer id;
public String name;
} |
感谢回复,我尝试一下。 |
https://github.com/alibaba/fastjson2/blob/main/docs/json_schema_cn.md
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请描述您的需求或者改进建议
现在 json-schema-validator 方案仅可支持 基本的数据 校验,但是不支持 更多 功能。也无法在JAVA 动态使用(需要前置条件:对应的Bean 类存在)。
请描述你建议的实现方案
参考https://json-schema.org/ 的规范,提供JSONSchema的Validate功能
The text was updated successfully, but these errors were encountered: