【问题标题】:Define Mongo Schema Validation using Spring使用 Spring 定义 Mongo 模式验证
【发布时间】:2018-10-14 14:50:39
【问题描述】:

我想使用带有 JSON Schema 验证器选项的 Spring-boot 在 Mongo 中定义一个 集合 (https://docs.mongodb.com/manual/core/schema-validation/#json-schema),我不想要 JSR-303 Bean 验证(这不是有效答案Spring data mongoDb not null annotation like Spring data Jpa),但在创建集合时定义一个使用 CollectionInfos() 在 JSON 中显示的选项。

例如,如果我定义一个 Account 模型类喜欢:

public class Account {

@Id
private String id;

private String name;

private String surname;

@NotNull
private String username;
}

我希望该集合具有,使用 db.getCollectionInfos(),json 喜欢:

[
{
    "name" : "account",
    "type" : "collection",
    "options" : {
        "validator" : {
            "$jsonSchema" : {
                "bsonType" : "object",
                "required" : [ 
                    "username"
                ]
            }
        }
    },
    "info" : {
        "readOnly" : false,
        "uuid" : UUID("979cdc4b-d6f3-4aef-bc89-3eee812773a5")
    },
    "idIndex" : {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "databaseName.account"
    }
}
]

该过程可能类似于 spring.jpa.hibernate.ddl-auto = create,因为它在模式级别定义规则,而不类似于在应用程序中定义规则的 Bean 验证器水平。

【问题讨论】:

  • MongoDB 中服务器上的模式验证是“非常新的”,$jsonSchema“查询运算符”本身实际上只有几个月的时间在野外流通。向 spring-mongo 添加“新功能”传统上是“慢节奏”的,但它不是一个官方的 MongoDB 项目,因此是一个“爱心的社区劳动”。如果您没有看到文档中提到的功能,那么它不存在。他们可能只是接受自愿的拉取请​​求
  • 你的问题是什么?
  • @BrentR 我希望 spring-data-mongo 从模型开始自动定义 JSON Schema

标签: java spring mongodb spring-boot spring-data-mongodb


【解决方案1】:

Spring Data MongoDB 自版本 2.1 起支持 JsonSchema。详情请参阅Reference Documentation。 您可以使用如下构建器来定义您的架构。

MongoJsonSchema schema = MongoJsonSchema.builder()
    .required("username")
    .properties(JsonSchemaProperty.string("username"))
    .build();

template.createCollection("account", CollectionOptions.empty().schema(schema));

在撰写本文时,不支持从域类型创建 json 模式。 但是,您可能想加入讨论 DATAMONGO-1849 和/或尝试提供 PR#733 的快照。

建议通过调用类似MongoJsonSchema schema = schemaCreator.createSchemaFor(DomainType.class); 的东西将DomainType 变成MongoJsonSchema

public class DomainType {

    private final String requiredCtorArg;
    private final @Nullable String nullableCtorArg;
    private String optionalArg;

    public DomainType(String requiredCtorArg, @Nullable String nullableCtorArg) {

        this.requiredCtorArg = requiredCtorArg;
        this.nullableCtorArg = nullableCtorArg;
    }

    // gettter / setter omitted 
}
{
    'type' : 'object',
    'required' : ['requiredCtorArg'],
    'properties' : {
        'requiredCtorArg' : { 'type' : 'string' },
        'nullableCtorArg' : { 'type' : 'string' },
        'optionalArg' : { 'type' : 'string' }
     }
}

【讨论】:

  • 嗨@Christoph,感谢您的回复!我有一个问题:为什么你决定设置 @Nullable 来定义不需要的参数,而不是像在 Hibernate 中那样定义 @NotNull 参数?此外,是否有任何任务正在进行以支持特殊关键字(如 pattern for String)?
  • 请让我们讨论在issue tracker 中派生架构的要求/决定。
猜你喜欢
  • 2014-02-26
  • 2023-04-01
  • 2021-09-22
  • 1970-01-01
  • 2015-10-17
  • 2014-10-16
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多