Skip to content

Commit

Permalink
ES-880
Browse files Browse the repository at this point in the history
Signed-off-by: ase-101 <sunkadaeanusha@gmail.com>
  • Loading branch information
ase-101 committed Jul 9, 2024
1 parent 1947067 commit 2bf59ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.signup.plugin.mosipid.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class SchemaFieldValidator {

private String type;
private String validator;
private List<String> arguments;
private String langCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.signup.plugin.mosipid.dto;

import lombok.Data;

@Data
public class SimpleType {

private String value;
private String language;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.micrometer.core.annotation.Timed;
Expand Down Expand Up @@ -395,41 +394,59 @@ private String getUTCDateTime() {
.format(DateTimeFormatter.ofPattern(UTC_DATETIME_PATTERN));
}

private void checkRegexValidator(JsonNode validator, JsonNode valueNode) {
String value = valueNode.get("value").textValue();
if (validator.get("type").textValue().equals("regex") &&
!value.matches(validator.get("validator").textValue())) {
log.error("Regex of {} does not match value of {}", validator.get("validator").textValue(), value);
private void validateValue(String keyName, SchemaFieldValidator validator, String value) {
if(value == null || value.isEmpty())
throw new InvalidProfileException(ErrorConstants.INVALID_INPUT);

if( validator != null && "regex".equalsIgnoreCase(validator.getType()) && !value.matches(validator.getValidator()) ) {
log.error("Regex of {} does not match value of {}", validator.getValidator(), value);
throw new InvalidProfileException("invalid_".concat(keyName.toLowerCase()));
}
}

private void validateEntryFields(Iterator<Map.Entry<String, JsonNode>> input, JsonNode schemaFields) {
while (input.hasNext()) {
Map.Entry<String, JsonNode> entry = input.next();
log.debug("validate field {} --> {}", entry.getKey(), entry.getValue());
JsonNode validateField = schemaFields.get(entry.getKey());
JsonNode schemaField = schemaFields.get(entry.getKey());

if (validateField == null) {
if (schemaField == null) {
log.error("No field found in the schema with this field name : {}", entry.getKey());
throw new InvalidProfileException(ErrorConstants.INVALID_INPUT);
throw new InvalidProfileException(ErrorConstants.UNKNOWN_FIELD);
}

JsonNode validators = validateField.get("validators");
if (validators == null) continue;

JsonNode validator = validators.get(0);
if (entry.getValue().isTextual()) {
checkRegexValidator(validator, entry.getValue());
} else if (entry.getValue().isArray()) {
for (JsonNode valueNode: entry.getValue()) {
JsonNode language = valueNode.get("language");
JsonNode langCode = validator.get("langCode");
if ((language == null) || (langCode != null && language.textValue().equals(langCode.textValue()))) {
checkRegexValidator(validator, valueNode);
if(!schemaField.hasNonNull("validators"))
continue;

SchemaFieldValidator[] validators = objectMapper.convertValue(schemaField.get("validators"), SchemaFieldValidator[].class);
if(validators == null || validators.length == 0) continue;

String datatype = schemaField.get("type") == null ? schemaField.get("$ref").textValue() : schemaField.get("type").textValue();
switch (datatype) {
case "string" :
validateValue(entry.getKey(), validators[0], entry.getValue().textValue());
break;
case "#/definitions/simpleType":
SimpleType[] values = objectMapper.convertValue(entry.getValue(), SimpleType[].class);
Optional<SimpleType> mandatoryLangValue = Arrays.stream(values).filter( v -> mandatoryLanguages.contains(v.getLanguage())).findFirst();
if(mandatoryLangValue.isEmpty())
throw new InvalidProfileException(MANDATORY_LANGUAGE_MISSING);

for(SimpleType value : values) {
validateLanguage(value.getLanguage());
Optional<SchemaFieldValidator> result = Arrays.stream(validators)
.filter(v-> value.getLanguage().equals(v.getLangCode()) || v.getLangCode() == null).findFirst();
result.ifPresent(schemaFieldValidator -> validateValue(entry.getKey(), schemaFieldValidator, value.getValue()));
}
}
break;
default:
log.error("Unhandled datatype found : {}", datatype);
}
}
}

private void validateLanguage(String language) {
if(!mandatoryLanguages.contains(language) && (optionalLanguages != null && !optionalLanguages.contains(language)))
throw new InvalidProfileException("invalid_language");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class ErrorConstants {
public static final String UNKNOWN_FIELD = "unknown_field";
public static final String MISSING_FIELD = "missing_field";
public static final String DATATYPE_MISMATCH = "datatype_mismatch";
public static final String MANDATORY_LANGUAGE_MISSING = "mandatory_language_missing";

}

0 comments on commit 2bf59ae

Please sign in to comment.