Skip to content

Commit

Permalink
revert validation error formatting to the original method
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong committed Oct 17, 2023
1 parent 6955f7b commit e2e72cd
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions src/pages/data_submission/RegistrationValidation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import {dateValidator, emailValidator, urlValidator} from '../../components/forms/formValidation';
import {get, isNil, set} from 'lodash';

/**
* Generate a ValidateFunction from a dataset registration schema
Expand Down Expand Up @@ -40,24 +41,56 @@ export const validateForm = (schema, formData) => {
function errorsToValidation(errors) {
let validation = {};
if (errors !== null) {
errors.forEach(err => {
let field = extractFieldFromError(err);
if (field !== null) {
Object.assign(validation, {[field]: {failed: [err.keyword], valid: false}});
errors.forEach(error => {
let path;
let errorType;
if (error.keyword === 'required') {
path = error.instancePath + '/' + error.params.missingProperty;
errorType = 'required';
} else if (error.keyword === 'format') {
// format errors are, e.g., date/email/uri errors
path = error.instancePath;
errorType = error.params.format; // e.g., 'date'
} else if (error.keyword === 'type') {
path = error.instancePath;
errorType = error.params.type; // e.g., 'integer'
} else {
path = error.instancePath;
errorType = 'unknown';
}
const splitPath = splitJsonPointer(path);
// update the validation object for the current field with this new error
const existingValidation = get(validation, splitPath);
const newValidation = updateValidation(existingValidation, errorType);
set(validation, splitPath, newValidation);
});
}
return validation;
}

function extractFieldFromError(err) {
let field = null;
if (err.keyword === 'required') {
field = err.params.missingProperty;
} else if (err.keyword === 'format') {
// This isn't right for nested instance paths ...
// /consentGroups/0/url --> url, etc.
field = err.instancePath.split('/').pop();
const updateValidation = (existingValidation, validationError) => {
if (isNil(existingValidation)) {
return {
valid: false,
failed: [validationError]
};
}
return field;
}
// if the field is required and empty, we shouldn't also error on, e.g., that it isn't a date format
if (existingValidation.failed.includes('required') || validationError === 'required') {
return {
valid: false,
failed: ['required']
};
}
return {
valid: false,
failed: existingValidation.failed + [validationError],
};
};

const splitJsonPointer = (jsonPointer) => {
if (jsonPointer[0] === '/') {
jsonPointer = jsonPointer.substring(1, jsonPointer.length);
}
return jsonPointer.split('/');
};

0 comments on commit e2e72cd

Please sign in to comment.