Skip to content

Commit

Permalink
withValidator methods now prefix parent property path
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRandall committed Aug 19, 2019
1 parent 473297c commit 361844b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageVersion>0.10.0</PackageVersion>
<PackageVersion>0.10.1</PackageVersion>
<Title>Accidental Fish Validation</Title>
<Authors>James Randall</Authors>
<Description>Simple F# DSL style record validation framework </Description>
Expand Down
23 changes: 17 additions & 6 deletions src/AccidentalFish.FSharp.Validation/Validation.fs
Expand Up @@ -307,21 +307,32 @@ module Validation =
else
Ok

// Function / sub-validators
// Function
let withFunction (validatorFunc:('validatorTargetType->ValidationState)) =
let comparator _ (value:'validatorTargetType) =
validatorFunc value
comparator


// Sub-validators - very similar to functions but prefix the property name with the current property path
let private runChildValidator value propertyPath (validatorFunc:('validatorTargetType->ValidationState)) =
let buildPrefixedPropertyName error =
{ error with property = sprintf "%s.%s" propertyPath error.property }
match (validatorFunc value) with
| Ok -> Ok
| Errors e -> Errors(e |> Seq.map buildPrefixedPropertyName |> Seq.toList)

let withValidatorWhen (predicate:'validatorTargetType->bool) (validatorFunc:('validatorTargetType->ValidationState)) =
let comparator _ (value:'validatorTargetType) =
let comparator propertyPath (value:'validatorTargetType) =
match predicate(value) with
| true -> validatorFunc value
| true -> runChildValidator value propertyPath validatorFunc
| false -> Ok
comparator

// Just an alias for withFunction but it makes it read better in calling code
let withValidator = withFunction

let withValidator (validatorFunc:('validatorTargetType->ValidationState)) =
let comparator propertyPath (value:'validatorTargetType) =
runChildValidator value propertyPath validatorFunc
comparator

let createValidatorFor<'targetType>() =
ValidatorBuilder<'targetType>()

0 comments on commit 361844b

Please sign in to comment.