DATAMONGO-1322 - Polishing.
Convert lineendings from CRLF to LF. Reduce validator implementations visibility to package. Extend Javadoc. Slight rewording in reference documentation. Original pull request: #525. Related pull request: #511. Related ticket: DATACMNS-1835.
This commit is contained in:
@@ -150,7 +150,7 @@ public class CollectionOptions {
|
||||
}
|
||||
|
||||
/**
|
||||
* /** Create new {@link CollectionOptions} with already given settings and {@code validationOptions} set to given
|
||||
* Create new {@link CollectionOptions} with already given settings and {@code validationOptions} set to given
|
||||
* {@link Validator}.
|
||||
*
|
||||
* @param validator can be {@literal null}.
|
||||
|
||||
@@ -63,7 +63,6 @@ import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
|
||||
import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
|
||||
import org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext;
|
||||
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
||||
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;
|
||||
@@ -108,7 +107,6 @@ import org.springframework.data.mongodb.core.query.Meta;
|
||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.data.mongodb.core.validation.JsonSchemaValidator;
|
||||
import org.springframework.data.mongodb.core.validation.Validator;
|
||||
import org.springframework.data.mongodb.util.MongoClientVersion;
|
||||
import org.springframework.data.projection.ProjectionInformation;
|
||||
@@ -141,16 +139,7 @@ import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.MongoIterable;
|
||||
import com.mongodb.client.model.CountOptions;
|
||||
import com.mongodb.client.model.CreateCollectionOptions;
|
||||
import com.mongodb.client.model.DeleteOptions;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.mongodb.client.model.FindOneAndDeleteOptions;
|
||||
import com.mongodb.client.model.FindOneAndUpdateOptions;
|
||||
import com.mongodb.client.model.ReturnDocument;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import com.mongodb.client.model.ValidationAction;
|
||||
import com.mongodb.client.model.ValidationLevel;
|
||||
import com.mongodb.client.model.*;
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import com.mongodb.client.result.UpdateResult;
|
||||
import com.mongodb.util.JSONParseException;
|
||||
@@ -2385,11 +2374,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
|
||||
Document doc = convertToDocument(collectionOptions);
|
||||
|
||||
if (collectionOptions != null && collectionOptions.getValidationOptions().isPresent()) {
|
||||
if (collectionOptions != null) {
|
||||
|
||||
ValidationOptions v = collectionOptions.getValidationOptions().get();
|
||||
v.getValidator().ifPresent(val -> doc.put("validator", getMappedValidator(val, targetType)));
|
||||
collectionOptions.getValidationOptions().ifPresent(it -> it.getValidator() //
|
||||
.ifPresent(val -> doc.put("validator", getMappedValidator(val, targetType))));
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
@@ -2402,6 +2392,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
protected Document convertToDocument(@Nullable CollectionOptions collectionOptions) {
|
||||
|
||||
Document document = new Document();
|
||||
|
||||
if (collectionOptions != null) {
|
||||
|
||||
collectionOptions.getCapped().ifPresent(val -> document.put("capped", val));
|
||||
@@ -2409,14 +2400,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
collectionOptions.getMaxDocuments().ifPresent(val -> document.put("max", val));
|
||||
collectionOptions.getCollation().ifPresent(val -> document.append("collation", val.toDocument()));
|
||||
|
||||
if (collectionOptions.getValidationOptions().isPresent()) {
|
||||
collectionOptions.getValidationOptions().ifPresent(it -> {
|
||||
|
||||
CollectionOptions.ValidationOptions v = collectionOptions.getValidationOptions().get();
|
||||
v.getValidationLevel().ifPresent(val -> document.append("validationLevel", val.getValue()));
|
||||
v.getValidationAction().ifPresent(val -> document.append("validationAction", val.getValue()));
|
||||
v.getValidator().ifPresent(val -> document.append("validator", getMappedValidator(val, Object.class)));
|
||||
}
|
||||
it.getValidationLevel().ifPresent(val -> document.append("validationLevel", val.getValue()));
|
||||
it.getValidationAction().ifPresent(val -> document.append("validationAction", val.getValue()));
|
||||
it.getValidator().ifPresent(val -> document.append("validator", getMappedValidator(val, Object.class)));
|
||||
});
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
@@ -2424,7 +2415,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
|
||||
Document validationRules = validator.toDocument();
|
||||
|
||||
if (validator instanceof JsonSchemaValidator || validationRules.containsKey("$jsonSchema")) {
|
||||
if (validationRules.containsKey("$jsonSchema")) {
|
||||
return schemaMapper.mapSchema(validationRules, domainType);
|
||||
}
|
||||
|
||||
|
||||
@@ -100,7 +100,6 @@ import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.NearQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.data.mongodb.core.query.Update;
|
||||
import org.springframework.data.mongodb.core.validation.JsonSchemaValidator;
|
||||
import org.springframework.data.mongodb.core.validation.Validator;
|
||||
import org.springframework.data.mongodb.util.MongoClientVersion;
|
||||
import org.springframework.data.projection.ProjectionInformation;
|
||||
@@ -2033,11 +2032,11 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
return result;
|
||||
}
|
||||
|
||||
Document getMappedValidator(Validator validator, Class<?> domainType) {
|
||||
private Document getMappedValidator(Validator validator, Class<?> domainType) {
|
||||
|
||||
Document validationRules = validator.toDocument();
|
||||
|
||||
if (validator instanceof JsonSchemaValidator || validationRules.containsKey("$jsonSchema")) {
|
||||
if (validationRules.containsKey("$jsonSchema")) {
|
||||
return schemaMapper.mapSchema(validationRules, domainType);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,75 +1,75 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
||||
import org.springframework.data.mongodb.core.query.SerializationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link Validator} implementation based on {@link CriteriaDefinition query expressions}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
* @since 2.1
|
||||
* @see Criteria
|
||||
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/#query-expressions">Schema Validation</a>
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@EqualsAndHashCode
|
||||
public class CriteriaValidator implements Validator {
|
||||
|
||||
private final CriteriaDefinition criteria;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} object, which is basically setup of query operators, based on a
|
||||
* {@link CriteriaDefinition} instance.
|
||||
*
|
||||
* @param criteria the criteria to build the {@code validator} from. Must not be {@literal null}.
|
||||
* @return new instance of {@link CriteriaValidator}.
|
||||
* @throws IllegalArgumentException when criteria is {@literal null}.
|
||||
*/
|
||||
public static CriteriaValidator of(CriteriaDefinition criteria) {
|
||||
|
||||
Assert.notNull(criteria, "Criteria must not be null!");
|
||||
return new CriteriaValidator(criteria);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.validation.Validator#toDocument()
|
||||
*/
|
||||
@Override
|
||||
public Document toDocument() {
|
||||
return criteria.getCriteriaObject();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return SerializationUtils.serializeToJsonSafely(toDocument());
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
||||
import org.springframework.data.mongodb.core.query.SerializationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link Validator} implementation based on {@link CriteriaDefinition query expressions}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
* @since 2.1
|
||||
* @see Criteria
|
||||
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/#query-expressions">Schema Validation</a>
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@EqualsAndHashCode
|
||||
class CriteriaValidator implements Validator {
|
||||
|
||||
private final CriteriaDefinition criteria;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} object, which is basically setup of query operators, based on a
|
||||
* {@link CriteriaDefinition} instance.
|
||||
*
|
||||
* @param criteria the criteria to build the {@code validator} from. Must not be {@literal null}.
|
||||
* @return new instance of {@link CriteriaValidator}.
|
||||
* @throws IllegalArgumentException when criteria is {@literal null}.
|
||||
*/
|
||||
static CriteriaValidator of(CriteriaDefinition criteria) {
|
||||
|
||||
Assert.notNull(criteria, "Criteria must not be null!");
|
||||
|
||||
return new CriteriaValidator(criteria);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mongodb.core.validation.Validator#toDocument()
|
||||
*/
|
||||
@Override
|
||||
public Document toDocument() {
|
||||
return criteria.getCriteriaObject();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return SerializationUtils.serializeToJsonSafely(toDocument());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,25 +28,26 @@ import org.springframework.util.Assert;
|
||||
* which can be either a {@code $jsonSchema} or query expression.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 2.1
|
||||
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/">Schema Validation</a>
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@EqualsAndHashCode
|
||||
public class DocumentValidator implements Validator {
|
||||
class DocumentValidator implements Validator {
|
||||
|
||||
private final Document validatorObject;
|
||||
|
||||
/**
|
||||
* Create new {@link org.springframework.data.mongodb.core.validation.DocumentValidator} defining validation rules via
|
||||
* a plain {@link Document}.
|
||||
* Create new {@link DocumentValidator} defining validation rules via a plain {@link Document}.
|
||||
*
|
||||
* @param validatorObject must not be {@literal null}.
|
||||
* @throws IllegalArgumentException if validatorObject is {@literal null}.
|
||||
*/
|
||||
public static DocumentValidator of(Document validatorObject) {
|
||||
static DocumentValidator of(Document validatorObject) {
|
||||
|
||||
Assert.notNull(validatorObject, "ValidatorObject must not be null!");
|
||||
|
||||
return new DocumentValidator(new Document(validatorObject));
|
||||
}
|
||||
|
||||
@@ -60,7 +61,7 @@ public class DocumentValidator implements Validator {
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
|
||||
@@ -28,23 +28,27 @@ import org.springframework.util.Assert;
|
||||
* {@link Validator} implementation based on {@link MongoJsonSchema JSON Schema}.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
* @since 2.1
|
||||
* @see <a href="https://docs.mongodb.com/manual/core/schema-validation/#json-schema">Schema Validation</a>
|
||||
*/
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@EqualsAndHashCode
|
||||
public class JsonSchemaValidator implements Validator {
|
||||
class JsonSchemaValidator implements Validator {
|
||||
|
||||
private final MongoJsonSchema schema;
|
||||
|
||||
public static JsonSchemaValidator of(MongoJsonSchema schema) {
|
||||
/**
|
||||
* Create new {@link JsonSchemaValidator} defining validation rules via {@link MongoJsonSchema}.
|
||||
*
|
||||
* @param schema must not be {@literal null}.
|
||||
* @throws IllegalArgumentException if schema is {@literal null}.
|
||||
*/
|
||||
static JsonSchemaValidator of(MongoJsonSchema schema) {
|
||||
|
||||
Assert.notNull(schema, "Schema must not be null!");
|
||||
return new JsonSchemaValidator(schema);
|
||||
}
|
||||
|
||||
public MongoJsonSchema getSchema() {
|
||||
return schema;
|
||||
return new JsonSchemaValidator(schema);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -57,7 +61,7 @@ public class JsonSchemaValidator implements Validator {
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
||||
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Provides a {@code validator} object to be used for collection validation via
|
||||
* {@link org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
* @since 2.1
|
||||
* @see <a href="https://docs.mongodb.com/manual/reference/method/db.createCollection/">MongoDB Collection Options</a>
|
||||
*/
|
||||
public interface Validator {
|
||||
|
||||
/**
|
||||
* Get the {@link Document} containing the validation specific rules. The document may contain fields that may require
|
||||
* type and/or field name mapping.
|
||||
*
|
||||
* @return a MongoDB {@code validator} {@link Document}. Never {@literal null}.
|
||||
*/
|
||||
Document toDocument();
|
||||
|
||||
/**
|
||||
* Creates a basic {@link Validator} checking documents against a given set of rules.
|
||||
*
|
||||
* @param validationRules must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if validationRules is {@literal null}.
|
||||
*/
|
||||
static Validator document(Document validationRules) {
|
||||
|
||||
Assert.notNull(validationRules, "ValidationRules must not be null!");
|
||||
return DocumentValidator.of(validationRules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} checking documents against the structure defined in {@link MongoJsonSchema}.
|
||||
*
|
||||
* @param schema must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if schema is {@literal null}.
|
||||
*/
|
||||
static Validator schema(MongoJsonSchema schema) {
|
||||
|
||||
Assert.notNull(schema, "Schema must not be null!");
|
||||
return JsonSchemaValidator.of(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} checking documents against a given query structure expressed by
|
||||
* {@link CriteriaDefinition}. <br />
|
||||
*
|
||||
* @param criteria must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if criteria is {@literal null}.
|
||||
*/
|
||||
static Validator criteria(CriteriaDefinition criteria) {
|
||||
|
||||
Assert.notNull(criteria, "Criteria must not be null!");
|
||||
return CriteriaValidator.of(criteria);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
|
||||
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Provides a {@code validator} object to be used for collection validation via
|
||||
* {@link org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
* @since 2.1
|
||||
* @see <a href="https://docs.mongodb.com/manual/reference/method/db.createCollection/">MongoDB Collection Options</a>
|
||||
*/
|
||||
public interface Validator {
|
||||
|
||||
/**
|
||||
* Get the {@link Document} containing the validation specific rules. The document may contain fields that may require
|
||||
* type and/or field name mapping.
|
||||
*
|
||||
* @return a MongoDB {@code validator} {@link Document}. Never {@literal null}.
|
||||
*/
|
||||
Document toDocument();
|
||||
|
||||
/**
|
||||
* Creates a basic {@link Validator} checking documents against a given set of rules.
|
||||
*
|
||||
* @param validationRules must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if validationRules is {@literal null}.
|
||||
*/
|
||||
static Validator document(Document validationRules) {
|
||||
|
||||
Assert.notNull(validationRules, "ValidationRules must not be null!");
|
||||
return DocumentValidator.of(validationRules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} checking documents against the structure defined in {@link MongoJsonSchema}.
|
||||
*
|
||||
* @param schema must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if schema is {@literal null}.
|
||||
*/
|
||||
static Validator schema(MongoJsonSchema schema) {
|
||||
|
||||
Assert.notNull(schema, "Schema must not be null!");
|
||||
return JsonSchemaValidator.of(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Validator} checking documents against a given query structure expressed by
|
||||
* {@link CriteriaDefinition}. <br />
|
||||
*
|
||||
* @param criteria must not be {@literal null}.
|
||||
* @return new instance of {@link Validator}.
|
||||
* @throws IllegalArgumentException if criteria is {@literal null}.
|
||||
*/
|
||||
static Validator criteria(CriteriaDefinition criteria) {
|
||||
|
||||
Assert.notNull(criteria, "Criteria must not be null!");
|
||||
return CriteriaValidator.of(criteria);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
|
||||
import org.springframework.data.mongodb.core.CollectionOptions.ValidationOptions;
|
||||
import org.springframework.data.mongodb.core.mapping.Field;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.validation.DocumentValidator;
|
||||
import org.springframework.data.mongodb.test.util.MongoVersionRule;
|
||||
import org.springframework.data.util.Version;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -48,7 +47,8 @@ import com.mongodb.client.model.ValidationLevel;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link CollectionOptions#validation(ValidationOptions)} using
|
||||
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator} and {@link DocumentValidator}.
|
||||
* {@link org.springframework.data.mongodb.core.validation.CriteriaValidator} and
|
||||
* {@link org.springframework.data.mongodb.core.validation.DocumentValidator}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
@@ -212,5 +212,4 @@ public class MongoTemplateValidationTests {
|
||||
private @Nullable Integer rangedInteger;
|
||||
private @Field("customName") Object customFieldName;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,47 +1,47 @@
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CriteriaValidator}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class CriteriaValidatorUnitTests {
|
||||
|
||||
@Test // DATAMONGO-1322
|
||||
public void testSimpleCriteria() {
|
||||
|
||||
Criteria criteria = Criteria.where("nonNullString").ne(null).type(2).and("rangedInteger").type(16).gte(0).lte(122);
|
||||
Document validator = CriteriaValidator.of(criteria).toDocument();
|
||||
|
||||
assertThat(validator.get("nonNullString")).isEqualTo(new Document("$ne", null).append("$type", 2));
|
||||
assertThat(validator.get("rangedInteger"))
|
||||
.isEqualTo(new Document("$type", 16).append("$gte", 0).append("$lte", 122));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1322
|
||||
public void testFailOnNull() {
|
||||
CriteriaValidator.of(null);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.validation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CriteriaValidator}.
|
||||
*
|
||||
* @author Andreas Zink
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class CriteriaValidatorUnitTests {
|
||||
|
||||
@Test // DATAMONGO-1322
|
||||
public void testSimpleCriteria() {
|
||||
|
||||
Criteria criteria = Criteria.where("nonNullString").ne(null).type(2).and("rangedInteger").type(16).gte(0).lte(122);
|
||||
Document validator = CriteriaValidator.of(criteria).toDocument();
|
||||
|
||||
assertThat(validator.get("nonNullString")).isEqualTo(new Document("$ne", null).append("$type", 2));
|
||||
assertThat(validator.get("rangedInteger"))
|
||||
.isEqualTo(new Document("$type", 16).append("$gte", 0).append("$lte", 122));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1322
|
||||
public void testFailOnNull() {
|
||||
CriteriaValidator.of(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ Spring Data MongoDB supports MongoDB's specific JSON schema implementation to de
|
||||
[[mongo.mongo-3.validation.query-expression]]
|
||||
==== Query Expression Validation
|
||||
|
||||
Next to the <<mongo.mongo-3.validation.json-schema>> as of version 3.2 MongoDB supports validating documents against a given query structure. The structure can be built using `Criteria` objects just the same way as they are used for defining queries.
|
||||
Next to the <<mongo.mongo-3.validation.json-schema>>, MongoDB supports as of version 3.2 validating documents against a given structure described by a query. The structure can be built using `Criteria` objects just the same way as they are used for defining queries.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
Reference in New Issue
Block a user