From db6987b3a86137d006bcc95791d0fa894a90dfd6 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 13 Jul 2023 00:54:29 +0200 Subject: [PATCH] Validation: first draft. WIP. Cannot build atm. --- spago.dhall | 1 + src/App/Validation.purs | 112 +++++++++++++++++++++++++++++++++++++ src/App/ZoneInterface.purs | 63 --------------------- 3 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 src/App/Validation.purs diff --git a/spago.dhall b/spago.dhall index 081b081..b262abd 100644 --- a/spago.dhall +++ b/spago.dhall @@ -30,6 +30,7 @@ , "transformers" , "tuples" , "uint" + , "validation" , "web-encoding" , "web-events" , "web-socket" diff --git a/src/App/Validation.purs b/src/App/Validation.purs new file mode 100644 index 0000000..8852c04 --- /dev/null +++ b/src/App/Validation.purs @@ -0,0 +1,112 @@ +module App.Validation where + +import Data.Tuple (Tuple(..)) +import App.ResourceRecord (ResourceRecord) + +data Attribute + = Name + | TTL + | RRType + | Id + | Name + | Target + | Priority + | Protocol + | Weight + | Port + | NotAnAttribute + +type Errors = Array (Tuple Attribute String) + +validateA :: forall l. SimpleRR (|l) -> V Errors ResourceRecord +validateA _ = invalid [Tuple NotAnAttribute "validation not implemented"] +validateAAAA :: forall l. SimpleRR (|l) -> V Errors ResourceRecord +validateAAAA _ = invalid [Tuple NotAnAttribute "validation not implemented"] +validateTXT :: forall l. SimpleRR (|l) -> V Errors ResourceRecord +validateTXT _ = invalid [Tuple NotAnAttribute "validation not implemented"] +validateMX :: forall l. MXRR (|l) -> V Errors ResourceRecord +validateMX _ = invalid [Tuple NotAnAttribute "validation not implemented"] +validateSRV :: forall l. SRVRR (|l) -> V Errors ResourceRecord +validateSRV _ = invalid [Tuple NotAnAttribute "validation not implemented"] + +validateSRR :: forall l. SimpleRR (|l) -> V Errors ResourceRecord +validateSRR form = case form.rrtype of + "A" -> validateA form + "AAAA" -> validateAAAA form + "TXT" -> validateTXT form + "CNAME" -> validateCNAME form + "NS" -> validateNS form + _ -> invalid [Tuple NotAnAttribute $ "invalid type: " <> form.rrtype] + +validateMXRR :: forall l. MXRR (|l) -> V Errors ResourceRecord +validateMXRR form = case form.rrtype of + "MX" -> validateMX form + _ -> invalid [Tuple NotAnAttribute $ "invalid type (expected: MX): " <> form.rrtype] + +validateSRVRR :: forall l. SRVRR (|l) -> V Errors ResourceRecord +validateSRVRR form = case form.rrtype of + "SRV" -> validateSRV form + _ -> invalid [Tuple NotAnAttribute $ "invalid type (expected: SRV): " <> form.rrtype] + +fromLocalSimpleRRRepresentationToResourceRecord :: forall l. SimpleRR (|l) -> ResourceRecord +fromLocalSimpleRRRepresentationToResourceRecord form + = { rrtype: form.rrtype + , rrid: form.rrid + , name: form.name + , ttl: fromMaybe 3600 $ fromString form.ttl + , target: form.target + , readonly: form.readonly + , priority: Nothing + , port: Nothing + , protocol: Nothing + , weight: Nothing + , mname: Nothing + , rname: Nothing + , serial: Nothing + , refresh: Nothing + , retry: Nothing + , expire: Nothing + , minttl: Nothing + } + +fromLocalMXRRRepresentationToResourceRecord :: forall l. MXRR (|l) -> ResourceRecord +fromLocalMXRRRepresentationToResourceRecord form + = { rrtype: form.rrtype + , rrid: form.rrid + , name: form.name + , ttl: fromMaybe 3600 $ fromString form.ttl + , target: form.target + , readonly: form.readonly + , priority: Just $ fromMaybe 10 $ fromString form.priority + , port: Nothing + , protocol: Nothing + , weight: Nothing + , mname: Nothing + , rname: Nothing + , serial: Nothing + , refresh: Nothing + , retry: Nothing + , expire: Nothing + , minttl: Nothing + } + +fromLocalSRVRRepresentationToResourceRecord :: forall l. SRVRR (|l) -> ResourceRecord +fromLocalSRVRRepresentationToResourceRecord form + = { rrtype: form.rrtype + , rrid: form.rrid + , name: form.name + , ttl: fromMaybe 3600 $ fromString form.ttl + , target: form.target + , readonly: form.readonly + , priority: Just $ fromMaybe 10 $ fromString form.priority + , port: Just $ fromMaybe 10 $ fromString form.port + , protocol: Just form.protocol + , weight: Just $ fromMaybe 10 $ fromString form.weight + , mname: Nothing + , rname: Nothing + , serial: Nothing + , refresh: Nothing + , retry: Nothing + , expire: Nothing + , minttl: Nothing + } diff --git a/src/App/ZoneInterface.purs b/src/App/ZoneInterface.purs index 48d95ac..248ddc0 100644 --- a/src/App/ZoneInterface.purs +++ b/src/App/ZoneInterface.purs @@ -922,69 +922,6 @@ fromResourceRecordToLocalRepresentationSOARR new_rr = do , minttl: show minttl -- :: RR (Maybe Int) Local (String) } -fromLocalSimpleRRRepresentationToResourceRecord :: forall l. SimpleRR (|l) -> ResourceRecord -fromLocalSimpleRRRepresentationToResourceRecord form - = { rrtype: form.rrtype - , rrid: form.rrid - , name: form.name - , ttl: fromMaybe 3600 $ fromString form.ttl - , target: form.target - , readonly: form.readonly - , priority: Nothing - , port: Nothing - , protocol: Nothing - , weight: Nothing - , mname: Nothing - , rname: Nothing - , serial: Nothing - , refresh: Nothing - , retry: Nothing - , expire: Nothing - , minttl: Nothing - } - -fromLocalMXRRRepresentationToResourceRecord :: forall l. MXRR (|l) -> ResourceRecord -fromLocalMXRRRepresentationToResourceRecord form - = { rrtype: form.rrtype - , rrid: form.rrid - , name: form.name - , ttl: fromMaybe 3600 $ fromString form.ttl - , target: form.target - , readonly: form.readonly - , priority: Just $ fromMaybe 10 $ fromString form.priority - , port: Nothing - , protocol: Nothing - , weight: Nothing - , mname: Nothing - , rname: Nothing - , serial: Nothing - , refresh: Nothing - , retry: Nothing - , expire: Nothing - , minttl: Nothing - } - -fromLocalSRVRRepresentationToResourceRecord :: forall l. SRVRR (|l) -> ResourceRecord -fromLocalSRVRRepresentationToResourceRecord form - = { rrtype: form.rrtype - , rrid: form.rrid - , name: form.name - , ttl: fromMaybe 3600 $ fromString form.ttl - , target: form.target - , readonly: form.readonly - , priority: Just $ fromMaybe 10 $ fromString form.priority - , port: Just $ fromMaybe 10 $ fromString form.port - , protocol: Just form.protocol - , weight: Just $ fromMaybe 10 $ fromString form.weight - , mname: Nothing - , rname: Nothing - , serial: Nothing - , refresh: Nothing - , retry: Nothing - , expire: Nothing - , minttl: Nothing - } - first :: forall a. (a -> Boolean) -> Array a -> Maybe a first condition = A.head <<< (A.filter condition)