【问题标题】:No function clause matching in Regex.match?/2 on Elixir w/ Phoenix在带有 Phoenix 的 Elixir 上的 Regex.match?/2 中没有函数子句匹配
【发布时间】:2018-01-29 23:37:48
【问题描述】:

我在凤凰城有一条路线,需要使用 REGEX 检查 JSON 参数。 在我的例程中,我正在创建一个错误列表以在正文中报告,以防一个或多个正则表达式失败。 但是每当我运行代码时,我都会在“Regex.match?”上收到“FunctionClauseError”错误。我尝试过 String.match,但它们评估为相同的函数。

这是我的代码:

   def postServidor(conn, parameters) do
        reasons = []
        error = False
        if not Regex.match?(~r/^(19[0-9]{2}|2[0-9]{3})-(0[1-9]|1[012])-([123]0|[012][1-9]|31)$/, Map.get(parameters, "data_nascimento")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[data_nascimento] missing or failed to match API requirements. It should look like this: 1969-02-12"}]}
        end
        if not Regex.match?(~r/^([A-Z][a-z]+([ ]?[a-z]?['-]?[A-Z][a-z]+)*)$/, Map.get(parameters, "nome")) do            
            {error, reasons} = {True, reasons ++ [%{Reason => "[name] missing or failed to match API requirements. It should look like this: Firstname Middlename(optional) Lastname"}]}
        end
        if not Regex.match?(~r/^([A-Z][a-z]+([ ]?[a-z]?['-]?[A-Z][a-z]+)*)$/, Map.get(parameters, "nome_identificacao")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[nome_identificacao] missing or failed to match API requirements. It should look like this: Firstname Middlename(optional) Lastname"}]}
        end
        if not Regex.match?(~r/\b[MF]{1}\b/, Map.get(parameters, "sexo")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[sexo] missing or failed to match API requirements. It should look like this: M for male, F for female"}]}
        end
        if not Regex.match?( ~r/\b[0-9]+\b/, Map.get(parameters, "id_pessoa")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[id_pessoa] missing or failed to match API requirements. It should be numeric. "}]}
        end
        if not Regex.match?(~r/\b[0-9]+\b/, Map.get(parameters, "matricula_interna")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[matricula_interna] missing or failed to match API requirements. It should be numeric. "}]}
        end
        if not Regex.match?(~r/\b[0-9]+\b/, Map.get(parameters, "siape")) do
            {error, reasons} = {True, reasons ++ [%{Reason => "[siape] missing or failed to match API requirements. It should be numeric. "}]}
        end
        if error = True do
            json put_status(conn, 400),reasons
        else
            IO.puts("ok")
        end
    end

【问题讨论】:

    标签: json elixir phoenix-framework


    【解决方案1】:

    Regex.match?/2 需要一个字符串作为其第二个参数。您使用Map.get/3 可能会返回该字符串,但如果在地图中找不到该键,则默认为nil,这就是我对正在发生的事情的猜测。如果在您的地图中找不到该键,则它将nil 传递给Regex.match?/2,不会有与之匹配的函数子句。您可以修复您的地图以正确拥有密钥,或者您可以提供您自己的默认字符串用作Map.get/3 的第三个参数。例如,Map.get(parameters, "data_nascimento", "some default")

    【讨论】:

    • 我的一些 Map 值是整数,这是导致问题的原因。我还实施了您的“默认”故障保护。谢谢!
    • 很高兴它有帮助!
    猜你喜欢
    • 2019-06-15
    • 2019-09-24
    • 2017-11-30
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    相关资源
    最近更新 更多