使用 F# 的多字段验证(在 azure 函数内)
multiple field validation using F# (inside an azure function)
我有一个基于 http post 模板的 azure 函数。我将 json 从 1 个道具扩展到 3 个。
let versionJ = json.["version"]
let customerIdJ = json.["customerId"]
let stationIdJ = json.["stationId"]
match isNull versionJ with
检查所有三个是否为 null 的最佳方法是什么?使用元组?
match isNull versionJ, isNull customerIdJ, isNull stationIdJ with
在这种情况下,我认为使用简单的 if 将是更清洁的解决方案,
如果您将 isNull
定义为:
let inline isNull value = (value = null)
然后就这样做:
if isNull versionJ && isNull customerIdJ && isNull stationIdJ then
// your code
具体取决于您要检查的内容。
如果你想看到至少有1个null,那么你可以这样做:
let allAreNotNull = [versionJ; customerIdJ; stationIdJ]
|> List.map (not << isNull)
|> List.fold (&&) true
如果你想检查它们是否都是空值,你可以执行以下操作:
let allAreNull = [versionJ; customerIdJ; stationIdJ]
|> List.map isNull
|> List.fold (&&) true
更新
也可以换成List.forall
:
[versionJ; customerIdJ; stationIdJ]
|> List.forall (not << isNull)
[versionJ; customerIdJ; stationIdJ]
|> List.forall isNull
另一种方法受到 Applicatives 的启发,如果所有元素 (<>) null
.
则应用 createRecord
let createRecord v c s = v, c, s
let inline ap v f =
match f, v with
| _ , null
| None , _ -> None
| Some f, v -> f v |> Some
let v =
Some createRecord
|> ap json.["version"]
|> ap json.["customerId"]
|> ap json.["stationId"]
我有一个基于 http post 模板的 azure 函数。我将 json 从 1 个道具扩展到 3 个。
let versionJ = json.["version"]
let customerIdJ = json.["customerId"]
let stationIdJ = json.["stationId"]
match isNull versionJ with
检查所有三个是否为 null 的最佳方法是什么?使用元组?
match isNull versionJ, isNull customerIdJ, isNull stationIdJ with
在这种情况下,我认为使用简单的 if 将是更清洁的解决方案,
如果您将 isNull
定义为:
let inline isNull value = (value = null)
然后就这样做:
if isNull versionJ && isNull customerIdJ && isNull stationIdJ then
// your code
具体取决于您要检查的内容。 如果你想看到至少有1个null,那么你可以这样做:
let allAreNotNull = [versionJ; customerIdJ; stationIdJ]
|> List.map (not << isNull)
|> List.fold (&&) true
如果你想检查它们是否都是空值,你可以执行以下操作:
let allAreNull = [versionJ; customerIdJ; stationIdJ]
|> List.map isNull
|> List.fold (&&) true
更新
也可以换成List.forall
:
[versionJ; customerIdJ; stationIdJ]
|> List.forall (not << isNull)
[versionJ; customerIdJ; stationIdJ]
|> List.forall isNull
另一种方法受到 Applicatives 的启发,如果所有元素 (<>) null
.
createRecord
let createRecord v c s = v, c, s
let inline ap v f =
match f, v with
| _ , null
| None , _ -> None
| Some f, v -> f v |> Some
let v =
Some createRecord
|> ap json.["version"]
|> ap json.["customerId"]
|> ap json.["stationId"]