Skip to content

Commit

Permalink
[open-formulieren/open-forms#3383] Refactored phone number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Sep 26, 2023
1 parent 0f61b0f commit 0eeac4e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
62 changes: 30 additions & 32 deletions src/formio/validators/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,35 @@ const errorMessageMap = {
'phonenumber-nl': 'Invalid Dutch phonenumber',
};

const pluginAPIValidator = plugin => {
let defaultMsg = errorMessageMap[plugin];
// catches undefined too
if (defaultMsg == null) {
defaultMsg = 'Invalid';
}
const pluginsAPIValidator = {
key: `validate.backendApi`,
message(component) {
return component.t(component.errorMessage('Invalid'), {
field: component.errorLabel,
data: component.data,
});
},
check(component, setting, value) {
if (!value) return true;

return {
key: `validate.${plugin}`,
message(component) {
return component.t(component.errorMessage(defaultMsg), {
field: component.errorLabel,
data: component.data,
});
},
check(component, setting, value) {
if (!value) return true;
const plugins = component.component.validate.plugins;
const {baseUrl} = component.currentForm?.options || component.options;

const {baseUrl} = component.currentForm?.options || component.options;
const promises = plugins.map(plugin => {
const url = `${baseUrl}validation/plugins/${plugin}`;
return post(url, {value})
.then(response => {
const valid = response.data.isValid;
return valid ? true : response.data.messages.join('<br>');
})
.catch(() => false);
},
};
return post(url, {value}).then(response => {
const valid = response.data.isValid;
return valid ? true : response.data.messages.join('<br>');
});
});
return Promise.all(promises).then(results => {
const anyValid = results.some(result => result === true);

if (anyValid) return true;

return results.join('<br>');
});
},
};

/**
Expand All @@ -46,13 +47,10 @@ const pluginAPIValidator = plugin => {
*/
const enableValidationPlugins = component => {
if (Array.isArray(component.component.validate.plugins)) {
for (let plugin of component.component.validate.plugins) {
const validator = pluginAPIValidator(plugin);
if (validator == null) continue;
component.component.validateOn = 'blur';
component.validator.validators[plugin] = validator;
component.validators.push(plugin);
}
debugger;
component.component.validateOn = 'blur';
component.validator.validators.backendApi = pluginsAPIValidator;
component.validators.push('backendApi');
}
};

Expand Down
13 changes: 13 additions & 0 deletions src/jstests/formio/validators/mocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {rest} from 'msw';

import {BASE_URL} from 'api-mocks';

const VALIDATION_ENDPOINT = `${BASE_URL}validation/plugins/phonenumber-international`;

export const mockValidationGet = rest.get(VALIDATION_ENDPOINT, (req, res, ctx) => {
const response = {
isValid: true,
messages: [],
};
return res(ctx.status(200), ctx.json(response));
});
14 changes: 14 additions & 0 deletions src/jstests/formio/validators/pluginApiValidator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {BASE_URL} from 'api-mocks';
import mswServer from 'api-mocks/msw-server';

import {
mockValidationGet
} from './mocks';
import pluginsAPIValidator from '..validators/pluginsAPIValidator';

describe('The OpenForms validation', () => {
it('tests...', async () => {
mswServer.use(mockValidationGet);
const pluginvalidator = pluginsAPIValidator;


0 comments on commit 0eeac4e

Please sign in to comment.