Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
fix: use default validator and translate its error message (fixes #494)…
Browse files Browse the repository at this point in the history
… (#554)
  • Loading branch information
MichaelKohler authored Nov 18, 2021
1 parent 7d58d53 commit 5a1b95d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
6 changes: 2 additions & 4 deletions server/lib/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const defaultValidator = require('./languages/default');
const bas = require('./languages/bas');
const en = require('./languages/en');
const eo = require('./languages/eo');
Expand All @@ -24,9 +25,6 @@ const VALIDATORS = {
or,
};

const DEFAULT_VALIDATOR_LANGUAGE = 'en';
const DEFAULT_VALIDATOR = VALIDATORS[DEFAULT_VALIDATOR_LANGUAGE];

module.exports = {
validateSentences,
};
Expand Down Expand Up @@ -87,5 +85,5 @@ function validateSentence(validator, sentence) {
}

function getValidatorFor(language) {
return VALIDATORS[language] || DEFAULT_VALIDATOR;
return VALIDATORS[language] || defaultValidator;
}
34 changes: 34 additions & 0 deletions server/lib/validation/languages/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const tokenizeWords = require('talisman/tokenizers/words');

const TRANSLATION_KEY_PREFIX = 'TRANSLATION_KEY:';

// Minimum of words that qualify as a sentence.
const MIN_WORDS = 1;

// Maximum of words allowed per sentence to keep recordings in a manageable duration.
const MAX_WORDS = 14;

const INVALIDATIONS = [{
fn: (sentence) => {
const words = tokenizeWords(sentence);
return words.length < MIN_WORDS || words.length > MAX_WORDS;
},
error: `${TRANSLATION_KEY_PREFIX}sc-validation-number-of-words`,
}, {
regex: /[0-9]+/,
error: `${TRANSLATION_KEY_PREFIX}sc-validation-no-numbers`,
}, {
regex: /[<>+*#@^[\]()/]/,
error: `${TRANSLATION_KEY_PREFIX}sc-validation-no-symbols`,
}, {
// Any words consisting of uppercase letters or uppercase letters with a period
// in-between are considered abbreviations or acronyms.
// This currently also matches fooBAR but we most probably don't want that either
// as users wouldn't know how to pronounce the uppercase letters.
regex: /[A-Z]{2,}|[A-Z]+\.*[A-Z]+/,
error: `${TRANSLATION_KEY_PREFIX}sc-validation-no-abbreviations`,
}];

module.exports = {
INVALIDATIONS,
};
6 changes: 6 additions & 0 deletions web/locales/en/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,9 @@ sc-settings-reset-skipped = Reset skipped sentences
sc-settings-skipped-decription = You previously skipped sentences while reviewing. Resetting skipped sentences will show all skipped sentences again. This is independent of the language.
sc-settings-show-all-button = Show all skipped sentences again
sc-settings-failed = Could not change settings. Please try again.
# VALIDATION
sc-validation-number-of-words = Sentence must contain between 1 and 14 (inclusive) words
sc-validation-no-numbers = Sentence should not contain numbers
sc-validation-no-symbols = Sentence should not contain symbols
sc-validation-no-abbreviations = Sentence should not contain abbreviations
25 changes: 17 additions & 8 deletions web/src/components/submit-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SubmitButton from './submit-button';
import { Prompt } from './prompt';

const SPLIT_ON = '\n';
const TRANSLATION_KEY_PREFIX = 'TRANSLATION_KEY:';

function parseSentences(sentenceText: string): string[] {
const sentences = sentenceText
Expand Down Expand Up @@ -220,14 +221,22 @@ export default function SubmitForm({
<p></p>
</Localized>

{Object.keys(sentenceSubmissionFailures).map((filterKey) => (
<React.Fragment key={'fragment-' + filterKey}>
<h3 key="{filterKey}">{filterKey}</h3>
{sentenceSubmissionFailures[filterKey].map((filteredSentence) => (
<Sentence key={filteredSentence}>{filteredSentence}</Sentence>
))}
</React.Fragment>
))}
{Object.keys(sentenceSubmissionFailures).map((filterKey) => {
const title = filterKey.startsWith(TRANSLATION_KEY_PREFIX) ? (
<Localized id={filterKey.replace(TRANSLATION_KEY_PREFIX, '')}></Localized>
) : (
filterKey
);

return (
<React.Fragment key={'fragment-' + filterKey}>
<h3 key="{filterKey}">{title}</h3>
{sentenceSubmissionFailures[filterKey].map((filteredSentence) => (
<Sentence key={filteredSentence}>{filteredSentence}</Sentence>
))}
</React.Fragment>
);
})}
</section>
)}
</React.Fragment>
Expand Down

0 comments on commit 5a1b95d

Please sign in to comment.