-
Notifications
You must be signed in to change notification settings - Fork 5
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
Plot search form validations #444
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// @flow | ||
import {parseISO} from 'date-fns'; | ||
import {get, set} from 'lodash/object'; | ||
|
||
import {CONTROL_SHARE_FIELD_IDENTIFIER} from '$src/application/constants'; | ||
|
||
const PERSONAL_IDENTIFIER_CHECK_CHAR_LIST = '0123456789ABCDEFHJKLMNPRSTUVWXY'; | ||
// from the rightmost digit to the leftmost | ||
const COMPANY_IDENTIFIER_CHECKSUM_MULTIPLIERS = [2, 4, 8, 5, 10, 9, 7]; | ||
|
||
export const personalIdentifierValidator = (value: any, error?: string): ?string => { | ||
if (value === '') { | ||
return; | ||
} | ||
|
||
if (typeof value !== 'string') { | ||
return error || 'Virheellinen henkilötunnus'; | ||
} | ||
|
||
const result = /^(\d{6})([-+ABCDEFUVWXY])(\d{3})([0-9ABCDEFHJKLMNPRSTUVWXY])$/.exec(value.toUpperCase()); | ||
|
||
if (!result) { | ||
return error || 'Virheellinen henkilötunnus'; | ||
} | ||
|
||
const datePart = result[1]; | ||
const separator = result[2]; | ||
const runningNumber = result[3]; | ||
const checkChar = result[4]; | ||
|
||
let century = '19'; | ||
switch (separator) { | ||
case '+': | ||
century = '18'; | ||
break; | ||
case 'A': | ||
case 'B': | ||
case 'C': | ||
case 'D': | ||
case 'E': | ||
case 'F': | ||
century = '20'; | ||
break; | ||
default: // U-Y, - | ||
break; | ||
} | ||
|
||
try { | ||
const year = `${century}${datePart.slice(4, 6)}`; | ||
const month = datePart.slice(2, 4); | ||
const day = datePart.slice(0, 2); | ||
|
||
const date = parseISO(`${year}-${month}-${day}T12:00:00`); | ||
|
||
if (date.getDate() !== parseInt(day) | ||
|| date.getMonth() !== parseInt(month) - 1 | ||
|| date.getFullYear() !== parseInt(year) | ||
) { | ||
return error || 'Virheellinen henkilötunnus'; | ||
} | ||
} catch (e) { | ||
return error || 'Virheellinen henkilötunnus'; | ||
} | ||
|
||
const calculatedCheckChar = PERSONAL_IDENTIFIER_CHECK_CHAR_LIST[parseInt(datePart + runningNumber) % 31]; | ||
|
||
if (checkChar !== calculatedCheckChar) { | ||
return error || 'Tarkistusmerkki ei täsmää'; | ||
} | ||
}; | ||
|
||
export const companyIdentifierValidator = (value: any, error?: string): ?string => { | ||
if (value === '') { | ||
return; | ||
} | ||
|
||
if (typeof value !== 'string') { | ||
return error || 'Virheellinen Y-tunnus'; | ||
} | ||
|
||
const result = /^(\d{6,7})-(\d)$/.exec(value); | ||
|
||
if (!result) { | ||
return error || 'Virheellinen Y-tunnus'; | ||
} | ||
|
||
const identifier = parseInt(result[1]); | ||
const checkNumber = parseInt(result[2]); | ||
|
||
let sum = 0; | ||
let calculatedCheckNumber; | ||
for (let i = 0; i < 7; ++i) { | ||
const digit = Math.floor(identifier / (Math.pow(10, i)) % 10); | ||
sum += digit * COMPANY_IDENTIFIER_CHECKSUM_MULTIPLIERS[i]; | ||
} | ||
|
||
calculatedCheckNumber = sum % 11; | ||
if (calculatedCheckNumber === 1) { | ||
// Company identifiers that sum up to a remainder of 1 are not handed out at all, | ||
// because non-zero values are subtracted from 11 to get the final number and | ||
// in these cases that number would be 10 | ||
return error || 'Virheellinen Y-tunnus'; | ||
} else if (calculatedCheckNumber > 1) { | ||
calculatedCheckNumber = 11 - calculatedCheckNumber; | ||
} | ||
|
||
if (calculatedCheckNumber !== checkNumber) { | ||
return error || 'Tarkistusmerkki ei täsmää'; | ||
} | ||
}; | ||
|
||
export const emailValidator = (value: any, error?: string): ?string => { | ||
if (!value) { | ||
return; | ||
} | ||
|
||
// A relatively simple validation that catches the most egregious examples of invalid emails. | ||
// (Also intentionally denies some technically valid but in this context exceedingly rare addresses, | ||
// like ones with quoted strings containing spaces or a right-side value without a dot.) | ||
if (!(/^\S+@\S+\.\S{2,}$/.exec(value))) { | ||
return error || 'Virheellinen sähköpostiosoite'; | ||
} | ||
}; | ||
|
||
export const validateApplicationForm: (string) => (Object) => Object = (pathPrefix: string) => (values: Object) => { | ||
let sum = 0; | ||
const errors = {}; | ||
const controlSharePaths = []; | ||
|
||
const root = get(values, pathPrefix); | ||
|
||
if (!root?.sections) { | ||
return {}; | ||
} | ||
|
||
const searchSingleSection = (section, path) => { | ||
if (section.fields) { | ||
Object.keys(section.fields).map((fieldIdentifier) => { | ||
if (fieldIdentifier === CONTROL_SHARE_FIELD_IDENTIFIER) { | ||
const result = /^(\d+)\s*\/\s*(\d+)$/.exec(section.fields[fieldIdentifier].value); | ||
if (!result) { | ||
set(errors, `${path}.fields.${fieldIdentifier}.value`, 'Virheellinen hallintaosuus'); | ||
} else { | ||
sum += parseInt(result[1]) / parseInt(result[2]); | ||
controlSharePaths.push(`${path}.fields.${fieldIdentifier}.value`); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
if (section.sections) { | ||
Object.keys(section.sections).map((identifier) => | ||
searchSection(section.sections[identifier], `${path}.sections.${identifier}`)); | ||
} | ||
}; | ||
|
||
const searchSection = (section, path) => { | ||
if (section instanceof Array) { | ||
section.forEach((singleSection, i) => searchSingleSection(singleSection, `${path}[${i}]`)); | ||
} else { | ||
searchSingleSection(section, path); | ||
} | ||
}; | ||
|
||
Object.keys(root.sections).map((identifier) => | ||
searchSection(root.sections[identifier], `${pathPrefix}.sections.${identifier}`)); | ||
|
||
|
||
if (Math.abs(sum - 1) > 1e-9) { | ||
controlSharePaths.forEach((path) => { | ||
set(errors, path, 'Hallintaosuuksien yhteismäärän on oltava 100%'); | ||
}); | ||
} | ||
|
||
return errors; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realised, that backend does not have this touppercase -operation and therefore this could pass the ui validation but fail at the be-side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me it does seem it accepts both uppercase and lowercase characters... though incorrectly (A-z includes these characters besides the intended letters: [\]^_`). That set by itself is a bit too wide as letters like Q never appear as the check character, though.It also doesn't seem to accept the recently added additional separator characters.Right, the actual validation in answer.py does the actual math properly, and indeed doesn't accept lowercase characters. The separator problem remains in the regex, however.