Skip to content

Commit

Permalink
Merge pull request #140 from damianwojtkowski/118-pesel-validation
Browse files Browse the repository at this point in the history
118 pesel validation
  • Loading branch information
Rafalkufel authored Apr 24, 2020
2 parents dd79578 + 461203c commit c857466
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/Dashboard/Form/ChildData/ChildData.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FieldWrapper from "../FieldWrapper/FieldWrapper";
import styles from "./ChildData.scss";

const { ipcRenderer } = require("electron");
const { isPeselValid } = require("./../../../../../src/utils/validators");

const ChildData = () => {
const [childDataState, setChildDataState] = useState({
Expand Down Expand Up @@ -43,6 +44,7 @@ const ChildData = () => {
name={`child.pesel`}
componentSize="medium"
component="input"
validator={isPeselValid}
/>
<FieldWrapper
name={`child.city`}
Expand Down
10 changes: 6 additions & 4 deletions src/components/Dashboard/Form/FieldWrapper/FieldWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import PropTypes from "prop-types";

import OptionList from "./SelectOptions";

const required = value => (value ? undefined : "Required");
const { composeValidators } = require("./../../../../../src/utils/validators");

const FieldWrapper = ({
name,
Expand All @@ -16,7 +16,8 @@ const FieldWrapper = ({
disabled,
onFocus,
options,
onChange
onChange,
validator
}) => {
const dataKeys = name.split(".");
const mainKey = dataKeys[0];
Expand All @@ -30,7 +31,7 @@ const FieldWrapper = ({
name={name}
component={component}
type="text"
validate={required}
validate={composeValidators(validator)}
disabled={disabled}
/>
) : (
Expand Down Expand Up @@ -66,7 +67,8 @@ FieldWrapper.propTypes = {
options: PropTypes.array,
label: PropTypes.string,
onFocus: PropTypes.func,
onChange: PropTypes.func
onChange: PropTypes.func,
validator: PropTypes.func
};

FieldWrapper.defaultProps = {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const required = value => (value ? undefined : "Required");

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];
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";
};

export const composeValidators = validator => value => {
return typeof validator === "undefined" ? required(value) : validator(value);
};
19 changes: 19 additions & 0 deletions src/utils/validators.test.js
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit c857466

Please sign in to comment.