From 47fc55aa6873c44cc77630af1ccc6bad253fa3e7 Mon Sep 17 00:00:00 2001 From: damian Date: Wed, 19 Feb 2020 19:41:55 +0100 Subject: [PATCH 1/5] Added helper file and prepared space for pesel validation --- src/alinka.db | 0 .../Dashboard/Form/ChildData/ChildData.js | 2 ++ .../Dashboard/Form/FieldWrapper/FieldWrapper.js | 13 +++++++++---- src/utils/DataHelper.js | 3 +++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 src/alinka.db create mode 100644 src/utils/DataHelper.js diff --git a/src/alinka.db b/src/alinka.db new file mode 100644 index 0000000..e69de29 diff --git a/src/components/Dashboard/Form/ChildData/ChildData.js b/src/components/Dashboard/Form/ChildData/ChildData.js index 1070b72..8f9594b 100644 --- a/src/components/Dashboard/Form/ChildData/ChildData.js +++ b/src/components/Dashboard/Form/ChildData/ChildData.js @@ -4,6 +4,7 @@ import FieldWrapper from "../FieldWrapper/FieldWrapper"; import styles from "./ChildData.scss"; const { ipcRenderer } = require("electron"); +const { isPeselValid } = require("./../../../../../src/utils/DataHelper"); const ChildData = () => { const [childDataState, setChildDataState] = useState({ @@ -43,6 +44,7 @@ const ChildData = () => { name={`child.pesel`} componentSize="medium" component="input" + validator={isPeselValid} /> (value ? undefined : "Required"); - +const composeValidators = (required, validator) => value => { + if (typeof validator === "undefined") return required(value); + return validator(value); +} const FieldWrapper = ({ name, componentSize, @@ -16,7 +19,8 @@ const FieldWrapper = ({ disabled, onFocus, options, - onChange + onChange, + validator }) => { const dataKeys = name.split("."); const mainKey = dataKeys[0]; @@ -30,7 +34,7 @@ const FieldWrapper = ({ name={name} component={component} type="text" - validate={required} + validate={composeValidators(required, validator)} disabled={disabled} /> ) : ( @@ -66,7 +70,8 @@ FieldWrapper.propTypes = { options: PropTypes.array, label: PropTypes.string, onFocus: PropTypes.func, - onChange: PropTypes.func + onChange: PropTypes.func, + validator: PropTypes.func }; FieldWrapper.defaultProps = { diff --git a/src/utils/DataHelper.js b/src/utils/DataHelper.js new file mode 100644 index 0000000..1b86817 --- /dev/null +++ b/src/utils/DataHelper.js @@ -0,0 +1,3 @@ +export const isPeselValid = pesel => { + return pesel; + }; From b990b4b4c56a2d1995202f67d97a829accf82aab Mon Sep 17 00:00:00 2001 From: damian Date: Wed, 26 Feb 2020 19:47:08 +0100 Subject: [PATCH 2/5] Added validator for pesel --- src/utils/DataHelper.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/utils/DataHelper.js b/src/utils/DataHelper.js index 1b86817..13983df 100644 --- a/src/utils/DataHelper.js +++ b/src/utils/DataHelper.js @@ -1,3 +1,16 @@ export const isPeselValid = pesel => { - return pesel; - }; + if (typeof pesel === "undefined" || !/^[0-9]{11}$/.test(pesel)) return "Type PESEL"; + + const weights = [1,3,7,9,1,3,7,9,1,3]; + let sum = 0; + + weights.forEach((weight, index) => { + sum += weight * pesel.substring(index, index + 1); + }); + + const checkSum = sum % 10 === 0 ? 0 : 10 - sum % 10; + + if (checkSum !== +pesel.substring(10, 11)) return "Invalid PESEL"; + + return undefined; +}; From d5ea880f1d7b32681661d7385e887e732947472c Mon Sep 17 00:00:00 2001 From: damian Date: Wed, 26 Feb 2020 19:52:03 +0100 Subject: [PATCH 3/5] Fixed lint errors --- .../Dashboard/Form/FieldWrapper/FieldWrapper.js | 2 +- src/utils/DataHelper.js | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js b/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js index 59a182f..4364e16 100644 --- a/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js +++ b/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js @@ -11,7 +11,7 @@ const required = value => (value ? undefined : "Required"); const composeValidators = (required, validator) => value => { if (typeof validator === "undefined") return required(value); return validator(value); -} +}; const FieldWrapper = ({ name, componentSize, diff --git a/src/utils/DataHelper.js b/src/utils/DataHelper.js index 13983df..bf2f6df 100644 --- a/src/utils/DataHelper.js +++ b/src/utils/DataHelper.js @@ -1,14 +1,15 @@ export const isPeselValid = pesel => { - if (typeof pesel === "undefined" || !/^[0-9]{11}$/.test(pesel)) return "Type PESEL"; - - const weights = [1,3,7,9,1,3,7,9,1,3]; + if (typeof pesel === "undefined" || !/^[0-9]{11}$/.test(pesel)) + return "Type PESEL"; + + const weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]; let sum = 0; weights.forEach((weight, index) => { sum += weight * pesel.substring(index, index + 1); }); - const checkSum = sum % 10 === 0 ? 0 : 10 - sum % 10; + const checkSum = sum % 10 === 0 ? 0 : 10 - (sum % 10); if (checkSum !== +pesel.substring(10, 11)) return "Invalid PESEL"; From 9ac4e0eb1df63db80ba5a868e8fda3757de2a77b Mon Sep 17 00:00:00 2001 From: damian Date: Wed, 11 Mar 2020 18:22:22 +0100 Subject: [PATCH 4/5] Moved validators to one file and renamed file with validators --- src/components/Dashboard/Form/ChildData/ChildData.js | 2 +- .../Dashboard/Form/FieldWrapper/FieldWrapper.js | 9 +++------ src/utils/{DataHelper.js => validators.js} | 6 +++++- 3 files changed, 9 insertions(+), 8 deletions(-) rename src/utils/{DataHelper.js => validators.js} (68%) diff --git a/src/components/Dashboard/Form/ChildData/ChildData.js b/src/components/Dashboard/Form/ChildData/ChildData.js index 8f9594b..9997482 100644 --- a/src/components/Dashboard/Form/ChildData/ChildData.js +++ b/src/components/Dashboard/Form/ChildData/ChildData.js @@ -4,7 +4,7 @@ import FieldWrapper from "../FieldWrapper/FieldWrapper"; import styles from "./ChildData.scss"; const { ipcRenderer } = require("electron"); -const { isPeselValid } = require("./../../../../../src/utils/DataHelper"); +const { isPeselValid } = require("./../../../../../src/utils/validators"); const ChildData = () => { const [childDataState, setChildDataState] = useState({ diff --git a/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js b/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js index 4364e16..1f33023 100644 --- a/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js +++ b/src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js @@ -7,11 +7,8 @@ import PropTypes from "prop-types"; import OptionList from "./SelectOptions"; -const required = value => (value ? undefined : "Required"); -const composeValidators = (required, validator) => value => { - if (typeof validator === "undefined") return required(value); - return validator(value); -}; +const { composeValidators } = require("./../../../../../src/utils/validators"); + const FieldWrapper = ({ name, componentSize, @@ -34,7 +31,7 @@ const FieldWrapper = ({ name={name} component={component} type="text" - validate={composeValidators(required, validator)} + validate={composeValidators(validator)} disabled={disabled} /> ) : ( diff --git a/src/utils/DataHelper.js b/src/utils/validators.js similarity index 68% rename from src/utils/DataHelper.js rename to src/utils/validators.js index bf2f6df..a716cfd 100644 --- a/src/utils/DataHelper.js +++ b/src/utils/validators.js @@ -1,3 +1,5 @@ +const required = value => (value ? undefined : "Required"); + export const isPeselValid = pesel => { if (typeof pesel === "undefined" || !/^[0-9]{11}$/.test(pesel)) return "Type PESEL"; @@ -12,6 +14,8 @@ export const isPeselValid = pesel => { const checkSum = sum % 10 === 0 ? 0 : 10 - (sum % 10); if (checkSum !== +pesel.substring(10, 11)) return "Invalid PESEL"; +}; - return undefined; +export const composeValidators = validator => value => { + return typeof validator === "undefined" ? required(value) : validator(value); }; From 461203c37c39ff5b322c7966da2d6eebd258f8ac Mon Sep 17 00:00:00 2001 From: damian Date: Sat, 14 Mar 2020 13:13:39 +0100 Subject: [PATCH 5/5] Added tests for pesel validator --- src/utils/validators.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/validators.test.js diff --git a/src/utils/validators.test.js b/src/utils/validators.test.js new file mode 100644 index 0000000..24f549a --- /dev/null +++ b/src/utils/validators.test.js @@ -0,0 +1,19 @@ +import { isPeselValid } from "./validators"; + +describe("Validators", () => { + it("returns valid pesel", () => { + expect(isPeselValid("64042999928")).toBeUndefined(); + }); + + it("return Type PESEL because of no value", () => { + expect(isPeselValid(undefined)).toBe("Type PESEL"); + }); + + it("return Type PESEL because of too short text", () => { + expect(isPeselValid("9703100302")).toBe("Type PESEL"); + }); + + it("return Invalid PESEL because of wrong numbers", () => { + expect(isPeselValid("97031003021")).toBe("Invalid PESEL"); + }); +});