Search in sources :

Example 1 with Validator

use of org.graylog2.plugin.database.validators.Validator in project graylog2-server by Graylog2.

the class InputImpl method getValidations.

@Override
public Map<String, Validator> getValidations() {
    final ImmutableMap.Builder<String, Validator> validations = ImmutableMap.builder();
    //validations.put(MessageInput.FIELD_INPUT_ID, new FilledStringValidator());
    validations.put(MessageInput.FIELD_TITLE, new FilledStringValidator());
    validations.put(MessageInput.FIELD_TYPE, new FilledStringValidator());
    validations.put(MessageInput.FIELD_CONFIGURATION, new MapValidator());
    validations.put(MessageInput.FIELD_CREATOR_USER_ID, new FilledStringValidator());
    validations.put(MessageInput.FIELD_CREATED_AT, new DateValidator());
    validations.put(MessageInput.FIELD_CONTENT_PACK, new OptionalStringValidator());
    return validations.build();
}
Also used : DateValidator(org.graylog2.database.validators.DateValidator) MapValidator(org.graylog2.database.validators.MapValidator) FilledStringValidator(org.graylog2.database.validators.FilledStringValidator) OptionalStringValidator(org.graylog2.database.validators.OptionalStringValidator) ImmutableMap(com.google.common.collect.ImmutableMap) OptionalStringValidator(org.graylog2.database.validators.OptionalStringValidator) Validator(org.graylog2.plugin.database.validators.Validator) MapValidator(org.graylog2.database.validators.MapValidator) DateValidator(org.graylog2.database.validators.DateValidator) FilledStringValidator(org.graylog2.database.validators.FilledStringValidator)

Example 2 with Validator

use of org.graylog2.plugin.database.validators.Validator in project graylog2-server by Graylog2.

the class StreamRuleImpl method getValidations.

@Override
public Map<String, Validator> getValidations() {
    final ImmutableMap.Builder<String, Validator> validators = ImmutableMap.builder();
    validators.put(FIELD_TYPE, new IntegerValidator());
    validators.put(FIELD_STREAM_ID, new ObjectIdValidator());
    validators.put(FIELD_CONTENT_PACK, new OptionalStringValidator());
    if (!EnumSet.of(StreamRuleType.ALWAYS_MATCH).contains(this.getType())) {
        validators.put(FIELD_FIELD, new FilledStringValidator());
    }
    if (!EnumSet.of(StreamRuleType.PRESENCE, StreamRuleType.ALWAYS_MATCH).contains(this.getType())) {
        validators.put(FIELD_VALUE, new FilledStringValidator());
    }
    return validators.build();
}
Also used : IntegerValidator(org.graylog2.database.validators.IntegerValidator) ObjectIdValidator(org.graylog2.database.validators.ObjectIdValidator) FilledStringValidator(org.graylog2.database.validators.FilledStringValidator) OptionalStringValidator(org.graylog2.database.validators.OptionalStringValidator) ImmutableMap(com.google.common.collect.ImmutableMap) OptionalStringValidator(org.graylog2.database.validators.OptionalStringValidator) Validator(org.graylog2.plugin.database.validators.Validator) IntegerValidator(org.graylog2.database.validators.IntegerValidator) ObjectIdValidator(org.graylog2.database.validators.ObjectIdValidator) FilledStringValidator(org.graylog2.database.validators.FilledStringValidator)

Example 3 with Validator

use of org.graylog2.plugin.database.validators.Validator in project graylog2-server by Graylog2.

the class PersistedServiceImpl method validate.

@Override
public Map<String, List<ValidationResult>> validate(Map<String, Validator> validators, Map<String, Object> fields) {
    if (validators == null || validators.isEmpty()) {
        return Collections.emptyMap();
    }
    final Map<String, List<ValidationResult>> validationErrors = new HashMap<>();
    for (Map.Entry<String, Validator> validation : validators.entrySet()) {
        Validator v = validation.getValue();
        String field = validation.getKey();
        try {
            ValidationResult validationResult = v.validate(fields.get(field));
            if (validationResult instanceof ValidationResult.ValidationFailed) {
                LOG.debug("Validation failure: [{}] on field [{}]", v.getClass().getCanonicalName(), field);
                validationErrors.computeIfAbsent(field, k -> new ArrayList<>());
                validationErrors.get(field).add(validationResult);
            }
        } catch (Exception e) {
            final String error = "Error while trying to validate <" + field + ">, got exception: " + e;
            LOG.debug(error);
            validationErrors.computeIfAbsent(field, k -> new ArrayList<>());
            validationErrors.get(field).add(new ValidationResult.ValidationFailed(error));
        }
    }
    return validationErrors;
}
Also used : Logger(org.slf4j.Logger) Validator(org.graylog2.plugin.database.validators.Validator) Persisted(org.graylog2.plugin.database.Persisted) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) DateTime(org.joda.time.DateTime) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) PersistedService(org.graylog2.plugin.database.PersistedService) Maps(com.google.common.collect.Maps) ArrayList(java.util.ArrayList) List(java.util.List) Lists(com.google.common.collect.Lists) DBObject(com.mongodb.DBObject) NodeId(org.graylog2.plugin.system.NodeId) DBCursor(com.mongodb.DBCursor) ValidationException(org.graylog2.plugin.database.ValidationException) Map(java.util.Map) EmbeddedPersistable(org.graylog2.plugin.database.EmbeddedPersistable) ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) ObjectId(org.bson.types.ObjectId) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ValidationResult(org.graylog2.plugin.database.validators.ValidationResult) ValidationException(org.graylog2.plugin.database.ValidationException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Validator(org.graylog2.plugin.database.validators.Validator)

Example 4 with Validator

use of org.graylog2.plugin.database.validators.Validator in project graylog2-server by Graylog2.

the class DateValidatorTest method testValidate.

@Test
public void testValidate() throws Exception {
    Validator v = new DateValidator();
    assertFalse(v.validate(null).passed());
    assertFalse(v.validate(9001).passed());
    assertFalse(v.validate("").passed());
    assertFalse(v.validate(new java.util.Date()).passed());
    // Only joda datetime.
    assertTrue(v.validate(new org.joda.time.DateTime(DateTimeZone.UTC)).passed());
    // Only accepts UTC.
    assertFalse(v.validate(new org.joda.time.DateTime(DateTimeZone.forID("+09:00"))).passed());
}
Also used : Validator(org.graylog2.plugin.database.validators.Validator) Test(org.junit.Test)

Example 5 with Validator

use of org.graylog2.plugin.database.validators.Validator in project graylog2-server by Graylog2.

the class FilledStringValidatorTest method testValidate.

@Test
public void testValidate() throws Exception {
    Validator v = new FilledStringValidator();
    assertFalse(v.validate(null).passed());
    assertFalse(v.validate(534).passed());
    assertFalse(v.validate("").passed());
    assertFalse(v.validate(new String()).passed());
    assertTrue(v.validate("so valid").passed());
}
Also used : Validator(org.graylog2.plugin.database.validators.Validator) Test(org.junit.Test)

Aggregations

Validator (org.graylog2.plugin.database.validators.Validator)8 FilledStringValidator (org.graylog2.database.validators.FilledStringValidator)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 OptionalStringValidator (org.graylog2.database.validators.OptionalStringValidator)3 Test (org.junit.Test)3 DateValidator (org.graylog2.database.validators.DateValidator)2 MapValidator (org.graylog2.database.validators.MapValidator)2 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 DBCursor (com.mongodb.DBCursor)1 DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ObjectId (org.bson.types.ObjectId)1