-
Notifications
You must be signed in to change notification settings - Fork 1
Custom format validation #5
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,10 @@ | ||||||
module Form.Validation exposing (validate) | ||||||
|
||||||
import Form.Error as Error exposing (ErrorValue(..)) | ||||||
import Dict | ||||||
import Form.Error as Error exposing (ErrorValue(..), TextFormat(..)) | ||||||
import Form.Normalization exposing (normalizeValue) | ||||||
import Form.Regex | ||||||
import Form.State exposing (Settings) | ||||||
import Json.Decode as Decode exposing (Value) | ||||||
import Json.Encode as Encode | ||||||
import Json.Schema.Definitions | ||||||
|
@@ -19,17 +21,17 @@ import Set | |||||
import Validation exposing (Validation, error) | ||||||
|
||||||
|
||||||
validate : Schema -> Value -> Validation Value | ||||||
validate schema rawValue = | ||||||
validate : Settings -> Schema -> Value -> Validation Value | ||||||
validate settings schema rawValue = | ||||||
let | ||||||
value = | ||||||
normalizeValue rawValue | ||||||
in | ||||||
Validation.voidRight value <| validateSchema schema value | ||||||
Validation.voidRight value <| validateSchema settings schema value | ||||||
|
||||||
|
||||||
validateSchema : Schema -> Value -> Validation Value | ||||||
validateSchema schema rawValue = | ||||||
validateSchema : Settings -> Schema -> Value -> Validation Value | ||||||
validateSchema settings schema rawValue = | ||||||
let | ||||||
value = | ||||||
normalizeValue rawValue | ||||||
|
@@ -44,29 +46,29 @@ validateSchema schema rawValue = | |||||
Validation.fail (error <| Unimplemented "Boolean schemas are not implemented.") | ||||||
|
||||||
ObjectSchema objectSchema -> | ||||||
validateSubSchema objectSchema value | ||||||
validateSubSchema settings objectSchema value | ||||||
|
||||||
|
||||||
validateSubSchema : SubSchema -> Value -> Validation Value | ||||||
validateSubSchema schema = | ||||||
validateSubSchema : Settings -> SubSchema -> Value -> Validation Value | ||||||
validateSubSchema settings schema = | ||||||
let | ||||||
typeValidations : Value -> Validation Value | ||||||
typeValidations = | ||||||
case schema.type_ of | ||||||
SingleType type_ -> | ||||||
validateSingleType schema type_ | ||||||
validateSingleType settings schema type_ | ||||||
|
||||||
AnyType -> | ||||||
Validation.succeed | ||||||
|
||||||
NullableType type_ -> | ||||||
Validation.oneOf | ||||||
[ \v -> Result.map (always Encode.null) <| validateNull v | ||||||
, validateSingleType schema type_ | ||||||
, validateSingleType settings schema type_ | ||||||
] | ||||||
|
||||||
UnionType types -> | ||||||
Validation.oneOf <| List.map (\type_ -> validateSingleType schema type_) types | ||||||
Validation.oneOf <| List.map (\type_ -> validateSingleType settings schema type_) types | ||||||
in | ||||||
Validation.validateAll | ||||||
[ Validation.whenJust schema.const validateConst | ||||||
|
@@ -75,8 +77,8 @@ validateSubSchema schema = | |||||
] | ||||||
|
||||||
|
||||||
validateSingleType : SubSchema -> SingleType -> Value -> Validation Value | ||||||
validateSingleType schema type_ value = | ||||||
validateSingleType : Settings -> SubSchema -> SingleType -> Value -> Validation Value | ||||||
validateSingleType settings schema type_ value = | ||||||
case type_ of | ||||||
ObjectType -> | ||||||
let | ||||||
|
@@ -99,7 +101,7 @@ validateSingleType schema type_ value = | |||||
Ok Encode.null | ||||||
|
||||||
( Just val, _ ) -> | ||||||
validateSchema propSchema val | ||||||
validateSchema settings propSchema val | ||||||
in | ||||||
Validation.validateAll (List.map (\( key, propSchema ) _ -> validateKey key propSchema) propList) value | ||||||
|
||||||
|
@@ -113,7 +115,7 @@ validateSingleType schema type_ value = | |||||
Result.map Encode.bool <| validateBool value | ||||||
|
||||||
StringType -> | ||||||
Result.map Encode.string <| validateString schema value | ||||||
Result.map Encode.string <| validateString settings schema value | ||||||
|
||||||
NullType -> | ||||||
Result.map (always Encode.null) <| validateNull value | ||||||
|
@@ -122,8 +124,8 @@ validateSingleType schema type_ value = | |||||
Err <| error (Error.Unimplemented "array") | ||||||
|
||||||
|
||||||
validateString : SubSchema -> Value -> Validation String | ||||||
validateString schema v = | ||||||
validateString : Settings -> SubSchema -> Value -> Validation String | ||||||
validateString settings schema v = | ||||||
case Decode.decodeValue Decode.string v of | ||||||
Err _ -> | ||||||
Err <| error Error.InvalidString | ||||||
|
@@ -133,31 +135,37 @@ validateString schema v = | |||||
[ Validation.whenJust schema.minLength validateMinLength | ||||||
, Validation.whenJust schema.maxLength validateMaxLength | ||||||
, Validation.whenJust schema.pattern validatePattern -- TODO: check specs if this is correct | ||||||
, Validation.whenJust schema.format validateFormat -- TODO: check specs if this is correct | ||||||
, Validation.whenJust schema.format (validateFormat settings) -- TODO: check specs if this is correct | ||||||
] | ||||||
s | ||||||
|
||||||
|
||||||
validateFormat : String -> String -> Validation String | ||||||
validateFormat format v = | ||||||
validateFormat : Settings -> String -> String -> Validation String | ||||||
validateFormat settings format value = | ||||||
case format of | ||||||
"date-time" -> | ||||||
validateRegex Form.Regex.dateTime Error.DateTime v | ||||||
validateRegex Form.Regex.dateTime Error.DateTime value | ||||||
|
||||||
"date" -> | ||||||
validateRegex Form.Regex.date Error.Date v | ||||||
validateRegex Form.Regex.date Error.Date value | ||||||
|
||||||
"time" -> | ||||||
validateRegex Form.Regex.time Error.Time v | ||||||
validateRegex Form.Regex.time Error.Time value | ||||||
|
||||||
"email" -> | ||||||
validateRegex Form.Regex.email Error.Email v | ||||||
validateRegex Form.Regex.email Error.Email value | ||||||
|
||||||
"phone" -> | ||||||
validateRegex Form.Regex.phone Error.Phone v | ||||||
validateRegex Form.Regex.phone Error.Phone value | ||||||
|
||||||
_ -> | ||||||
Validation.succeed v | ||||||
customFormat -> | ||||||
let | ||||||
customValidation = | ||||||
Dict.get customFormat settings.customFormats | ||||||
|> Maybe.map (\validation -> validation value) | ||||||
|> Maybe.withDefault (Result.Ok value) | ||||||
in | ||||||
Result.mapError (\err -> error (Error.InvalidCustomFormat err)) customValidation | ||||||
|
Result.mapError (\err -> error (Error.InvalidCustomFormat err)) customValidation | |
Result.mapError (\_ -> error (Error.InvalidCustomFormat customFormat)) customValidation |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,6 +332,9 @@ errorString error = | |
InvalidFormat _ -> | ||
"not the correct format" | ||
|
||
InvalidCustomFormat _ -> | ||
"not the correct format" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @potocpav Shall we by the way give more information here regarding which format it is (which we underscore now) or leave is same as InvalidFormat? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should. The format string is internal, and should not be present in the UI. If users want to see more info, they need to implement their own view. |
||
|
||
InvalidInt -> | ||
"not a valid integer" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we discussed, returning an error may be an overkill. Perhaps return
Result () String
?