Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIO-7847: Highlight components that have API keys consisting only fro… #5522

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions src/WebformBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1289,13 +1289,21 @@ export default class WebformBuilder extends Component {
highlightInvalidComponents() {
const repeatablePaths = this.findRepeatablePaths();
let hasInvalidComponents = false;
// Matches anything expect letters and '_' at the beginning of the key and anything except of letters, numbers,
// '-', '.' and '_' in the rest of the key
const badCharacters = /^[^A-Za-z_]+|[^A-Za-z0-9\-._]+/g;
alexandraRamanenka marked this conversation as resolved.
Show resolved Hide resolved

this.webform.everyComponent((comp) => {
const path = comp.path;
if (repeatablePaths.includes(path)) {
comp.setCustomValidity(this.t('apiKey', { key: comp.key }));
hasInvalidComponents = true;
}

if (comp.key.replace(badCharacters, '') === '') {
comp.setCustomValidity(this.t('apiKeyNotValid', { key: comp.key }));
hasInvalidComponents = true;
}
});

this.emit('builderFormValidityChange', hasInvalidComponents);
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default {
reCaptchaTokenValidationError: 'ReCAPTCHA: Token validation error',
reCaptchaTokenNotSpecifiedError: 'ReCAPTCHA: Token is not specified in submission',
apiKey: 'API Key is not unique: {{key}}',
apiKeyNotValid: 'API Key is not valid: {{key}}',
typeRemaining: '{{ remaining }} {{ type }} remaining.',
typeCount: '{{ count }} {{ type }}',
requiredDayField: '{{ field }} is required',
Expand Down
30 changes: 30 additions & 0 deletions test/forms/formWithNumericKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default {
type: 'form',
display: 'form',
components: [
{
label: 'Text Field',
applyMaskOn: 'change',
tableView: true,
key: 'test',
type: 'textfield',
input: true,
},
{
label: 'Text Field',
applyMaskOn: 'change',
tableView: true,
key: '1234',
type: 'textfield',
input: true,
},
{
type: 'button',
label: 'Submit',
key: 'submit',
disableOnInvalid: true,
input: true,
tableView: false,
},
],
};
13 changes: 12 additions & 1 deletion test/unit/WebformBuilder.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import testApiKeysUniquifying from '../forms/testApiKeysUniquifying';
import formBasedOnWizard from '../forms/formBasedOnWizard';
import formWithFormController from '../forms/formWithFormController';
import simpleWebform from '../forms/simpleWebform';
import formWithNumericKeys from '../forms/formWithNumericKeys';

global.requestAnimationFrame = (cb) => cb();
global.cancelAnimationFrame = () => {};
Expand Down Expand Up @@ -378,7 +379,17 @@ describe('WebformBuilder tests', function() {
}, 200);
}, 200)
}).catch(done);
})
});

it('Should show API error when components have invalid API keys', (done) => {
const builder = Harness.getBuilder();
builder.webform.setForm(formWithNumericKeys).then(() => {
builder.highlightInvalidComponents();
const component = builder.webform.components[1];
assert.equal(component.errors.length, 1);
done();
}).catch(done);
});
});

describe('Select Component selectData property', () => {
Expand Down
Loading