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

118 pesel validation #140

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/alinka.db
Empty file.
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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a few unittests for checking if validators work are as expected.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just create separated file e.g. validators.test.js and call a function with parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test file with pesel validator test cases have been added.

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);
};