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();
}
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();
}
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;
}
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());
}
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());
}
Aggregations