Skip to content

Commit

Permalink
Safely access field schema
Browse files Browse the repository at this point in the history
  • Loading branch information
fakenickels committed Feb 1, 2018
1 parent 75226d4 commit e25d966
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions re/ReForm.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
/* Validation types */
let safeHd = lst => List.length(lst) == 0 ? None : Some(List.hd(lst));

let (>>=) = (value, map) =>
switch value {
| None => None
| Some(value) => map(value)
};

module Validation = {
module I18n = {
type dictionary = {
Expand Down Expand Up @@ -74,13 +82,14 @@ module Create = (Config: Config) => {
let validateField:
(Config.fields, values, value, schema, Validation.I18n.dictionary) =>
option(string) =
(field, values, value, schema, i18n) => {
let fieldSchema =
schema
|> List.filter(((fieldName, _)) => fieldName === field)
|> List.hd;
Validation.getValidationError(fieldSchema, ~values, ~value, ~i18n);
};
(field, values, value, schema, i18n) =>
schema
|> List.filter(((fieldName, _)) => fieldName === field)
|> safeHd
>>= (
fieldSchema =>
Validation.getValidationError(fieldSchema, ~values, ~value, ~i18n)
);
let handleChange: ((Config.fields, value), values) => values =
((field, value), values) => {
let (_, _, setter) = getFieldLens(field);
Expand Down Expand Up @@ -207,10 +216,8 @@ module Create = (Config: Config) => {
self.state.errors
|> List.filter(((fieldName, _)) => fieldName === field)
|> List.map(((_, error)) => error)
|> (
finalList =>
List.length(finalList) == 0 ? None : List.hd(finalList)
);
|> safeHd
>>= (i => i);
children({
form: self.state,
handleChange,
Expand Down

0 comments on commit e25d966

Please sign in to comment.