NIHVIVO-138 Distinguish format validation from nonempty validation: specification of format validation now does not entail a requirement that the field be nonempty.
This commit is contained in:
parent
794e5d9f64
commit
324a6ea3f9
1 changed files with 94 additions and 84 deletions
|
@ -30,7 +30,7 @@ public class BasicValidation {
|
||||||
EditConfiguration editConfig;
|
EditConfiguration editConfig;
|
||||||
|
|
||||||
public BasicValidation(EditConfiguration editConfig, EditSubmission editSub){
|
public BasicValidation(EditConfiguration editConfig, EditSubmission editSub){
|
||||||
this.editConfig = editConfig;
|
this.editConfig = editConfig;
|
||||||
Map<String,List<String>> validatorsForFields = new HashMap<String,List<String>>();
|
Map<String,List<String>> validatorsForFields = new HashMap<String,List<String>>();
|
||||||
for(String fieldName: editConfig.getFields().keySet()){
|
for(String fieldName: editConfig.getFields().keySet()){
|
||||||
Field field = editConfig.getField(fieldName);
|
Field field = editConfig.getField(fieldName);
|
||||||
|
@ -57,9 +57,6 @@ public class BasicValidation {
|
||||||
String validateMsg = validate(validationType,value);
|
String validateMsg = validate(validationType,value);
|
||||||
if( validateMsg != null) {
|
if( validateMsg != null) {
|
||||||
errors.put(name,validateMsg);
|
errors.put(name,validateMsg);
|
||||||
if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,69 +69,78 @@ public class BasicValidation {
|
||||||
HashMap<String,String> errors = new HashMap<String,String>();
|
HashMap<String,String> errors = new HashMap<String,String>();
|
||||||
|
|
||||||
for( String name : editConfig.getLiteralsOnForm() ){
|
for( String name : editConfig.getLiteralsOnForm() ){
|
||||||
Literal literal = varNamesToValues.get(name);
|
Literal literal = varNamesToValues.get(name);
|
||||||
List<String>validations = varsToValidations.get(name);
|
List<String>validations = varsToValidations.get(name);
|
||||||
if( validations != null ){
|
if( validations != null ){
|
||||||
for( String validationType : validations){
|
// NB this is case-sensitive
|
||||||
String value = null;
|
boolean isRequiredField = validations.contains("nonempty");
|
||||||
try{
|
for( String validationType : validations){
|
||||||
if( literal != null )
|
String value = null;
|
||||||
value = literal.getString();
|
try{
|
||||||
}catch(Throwable th){
|
if( literal != null )
|
||||||
log.debug("could not convert literal to string" , th);
|
value = literal.getString();
|
||||||
}
|
}catch(Throwable th){
|
||||||
String validateMsg = validate(validationType, value );
|
log.debug("could not convert literal to string" , th);
|
||||||
|
}
|
||||||
|
// Empty field: if required, include only the empty field
|
||||||
|
// error message, not a format validation message. If non-required,
|
||||||
|
// don't do format validation, since that is both unnecessary and may
|
||||||
|
// incorrectly generate errors.
|
||||||
|
if (isEmpty(value)) {
|
||||||
|
if (isRequiredField) {
|
||||||
|
errors.put(name, REQUIRED_FIELD_EMPTY_MSG);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String validateMsg = validate(validationType, value);
|
||||||
if( validateMsg != null) {
|
if( validateMsg != null) {
|
||||||
errors.put(name,validateMsg);
|
errors.put(name,validateMsg);
|
||||||
// If it's a required field that is empty, do not continue with
|
|
||||||
// other validations for this field.
|
|
||||||
if (validateMsg == REQUIRED_FIELD_EMPTY_MSG) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String,String>validateFiles(Map<String, List<FileItem>> fileItemMap) {
|
public Map<String,String>validateFiles(Map<String, List<FileItem>> fileItemMap) {
|
||||||
|
|
||||||
HashMap<String,String> errors = new HashMap<String,String>();
|
HashMap<String,String> errors = new HashMap<String,String>();
|
||||||
for(String name: editConfig.getFilesOnForm() ){
|
for(String name: editConfig.getFilesOnForm() ){
|
||||||
List<String> validators = varsToValidations.get(name);
|
List<String> validators = varsToValidations.get(name);
|
||||||
for( String validationType : validators){
|
for( String validationType : validators){
|
||||||
String validateMsg = validate(validationType, fileItemMap.get(name));
|
String validateMsg = validate(validationType, fileItemMap.get(name));
|
||||||
if( validateMsg != null ) {
|
if( validateMsg != null ) {
|
||||||
errors.put(name, validateMsg);
|
errors.put(name, validateMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String validate(String validationType, List<FileItem> fileItems) {
|
private String validate(String validationType, List<FileItem> fileItems) {
|
||||||
if( "nonempty".equalsIgnoreCase(validationType)){
|
if( "nonempty".equalsIgnoreCase(validationType)){
|
||||||
if( fileItems == null || fileItems.size() == 0 ){
|
if( fileItems == null || fileItems.size() == 0 ){
|
||||||
return "a file must be entered for this field";
|
return "a file must be entered for this field.";
|
||||||
}else{
|
}else{
|
||||||
FileItem fileItem = fileItems.get(0);
|
FileItem fileItem = fileItems.get(0);
|
||||||
if( fileItem == null || fileItem.getName() == null || fileItem.getName().length() < 1 || fileItem.getSize() < 0){
|
if( fileItem == null || fileItem.getName() == null || fileItem.getName().length() < 1 || fileItem.getSize() < 0){
|
||||||
return "a file must be entered for this field";
|
return "a file must be entered for this field.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* null indicates success. A returned string is the validation
|
/* null indicates success. A returned string is the validation
|
||||||
error message.
|
error message.
|
||||||
*/
|
*/
|
||||||
public String validate(String validationType, String value){
|
public String validate(String validationType, String value){
|
||||||
// Required field validation
|
// Required field validation.
|
||||||
|
// For literals, testing empty required values in validateLiterals.
|
||||||
|
// This case may be needed for validation of other field types.
|
||||||
if( "nonempty".equalsIgnoreCase(validationType)){
|
if( "nonempty".equalsIgnoreCase(validationType)){
|
||||||
if( value == null || value.trim().length() == 0 )
|
if( isEmpty(value) )
|
||||||
return REQUIRED_FIELD_EMPTY_MSG;
|
return REQUIRED_FIELD_EMPTY_MSG;
|
||||||
}
|
}
|
||||||
// Format validation
|
// Format validation
|
||||||
|
@ -142,15 +148,15 @@ public class BasicValidation {
|
||||||
if( isDate( value))
|
if( isDate( value))
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
else
|
else
|
||||||
return "must be in valid date format mm/dd/yyyy";
|
return "must be in valid date format mm/dd/yyyy.";
|
||||||
}else if( validationType.indexOf("datatype:") == 0 ) {
|
}else if( validationType.indexOf("datatype:") == 0 ) {
|
||||||
String datatypeURI = validationType.substring(9);
|
String datatypeURI = validationType.substring(9);
|
||||||
String errorMsg = validateAgainstDatatype( value, datatypeURI );
|
String errorMsg = validateAgainstDatatype( value, datatypeURI );
|
||||||
if ( errorMsg == null ) {
|
if ( errorMsg == null ) {
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
return errorMsg;
|
return errorMsg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null; //
|
return null; //
|
||||||
}
|
}
|
||||||
|
@ -162,24 +168,24 @@ public class BasicValidation {
|
||||||
private static DatatypeDaoJena ddao = null;
|
private static DatatypeDaoJena ddao = null;
|
||||||
|
|
||||||
public static synchronized String validateAgainstDatatype( String value, String datatypeURI ) {
|
public static synchronized String validateAgainstDatatype( String value, String datatypeURI ) {
|
||||||
if ( ( datatypeURI != null ) && ( datatypeURI.length()>0 ) ) {
|
if ( ( datatypeURI != null ) && ( datatypeURI.length()>0 ) ) {
|
||||||
RDFDatatype datatype = TypeMapper.getInstance().getSafeTypeByName(datatypeURI);
|
RDFDatatype datatype = TypeMapper.getInstance().getSafeTypeByName(datatypeURI);
|
||||||
if ( datatype == null ) {
|
if ( datatype == null ) {
|
||||||
throw new RuntimeException( datatypeURI + " is not a recognized datatype");
|
throw new RuntimeException( datatypeURI + " is not a recognized datatype");
|
||||||
}
|
}
|
||||||
if ( datatype.isValid(value) ) {
|
if ( datatype.isValid(value) ) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
// TODO: better way of getting more friendly names for common datatypes
|
// TODO: better way of getting more friendly names for common datatypes
|
||||||
if (ddao == null) {
|
if (ddao == null) {
|
||||||
ddao = new DatatypeDaoJena(new WebappDaoFactoryJena(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM)));
|
ddao = new DatatypeDaoJena(new WebappDaoFactoryJena(ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM)));
|
||||||
}
|
}
|
||||||
Datatype dtype = ddao.getDatatypeByURI(datatypeURI);
|
Datatype dtype = ddao.getDatatypeByURI(datatypeURI);
|
||||||
String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI;
|
String dtypeMsg = (dtype != null) ? dtype.getName() : datatypeURI;
|
||||||
return " Please correct this value. (Must be a valid " + dtypeMsg + ")";
|
return " Please correct this value: must be a valid " + dtypeMsg + ".";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkValidations(){
|
private void checkValidations(){
|
||||||
|
@ -187,10 +193,10 @@ public class BasicValidation {
|
||||||
for( String key : varsToValidations.keySet()){
|
for( String key : varsToValidations.keySet()){
|
||||||
for( String validator : varsToValidations.get(key)){
|
for( String validator : varsToValidations.get(key)){
|
||||||
if( ! basicValidations.contains( validator)) {
|
if( ! basicValidations.contains( validator)) {
|
||||||
if ( ! ( ( validator != null) &&
|
if ( ! ( ( validator != null) &&
|
||||||
( validator.indexOf( "datatype:" ) == 0 ) ) ) {
|
( validator.indexOf( "datatype:" ) == 0 ) ) ) {
|
||||||
unknown.add(validator);
|
unknown.add(validator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,10 +206,14 @@ public class BasicValidation {
|
||||||
throw new Error( "Unknown basic validators: " + unknown.toArray());
|
throw new Error( "Unknown basic validators: " + unknown.toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isEmpty(String value) {
|
||||||
|
return (value == null || value.trim().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** we use null to indicate success */
|
/** we use null to indicate success */
|
||||||
public final static String SUCCESS = null;
|
public final static String SUCCESS = null;
|
||||||
public final static String REQUIRED_FIELD_EMPTY_MSG = "This field must not be empty";
|
public final static String REQUIRED_FIELD_EMPTY_MSG = "This field must not be empty.";
|
||||||
|
|
||||||
/** regex for strings like "12/31/2004" */
|
/** regex for strings like "12/31/2004" */
|
||||||
private final String dateRegex = "((1[012])|([1-9]))/((3[10])|([12][0-9])|([1-9]))/[\\d]{4}";
|
private final String dateRegex = "((1[012])|([1-9]))/((3[10])|([12][0-9])|([1-9]))/[\\d]{4}";
|
||||||
|
|
Loading…
Add table
Reference in a new issue