代码之家  ›  专栏  ›  技术社区  ›  Noitidart

输入定义可选键

  •  0
  • Noitidart  · 技术社区  · 6 年前

    我用 sails generate action task/update-task . 我现在试图创建一个输入参数,该参数应该是一个具有可选键的对象:

      inputs: {
        fields: {
          type: {
            body: 'string?',
            rruleSetStr: 'string?',
          },
          required: true,
          description: 'All keys are not required, but at least one is'
        },
    

    但是我一直有错误:

     The action `task/update-task` could not be registered.  It looks like a machine definition (actions2), but it could not be used to build an action.
    Details: ImplementationError: Sorry, could not interpret "task/update-task.js" because its underlying implementation has a problem:
    ------------------------------------------------------
    • Invalid input definition ("fields").  Unrecognized `type`.  (Must be 'string', 'number', 'boolean', 'json' or 'ref'.  Or set it to a type schema like `[{id:'number', name: {givenName: 'Lisa'}}]`.)
    ------------------------------------------------------
    
    If you are the maintainer of "task/update-task.js", then you can change its implementation to solve the problem above.  Otherwise, please file a bug report with the maintainer, or fork your own copy and fix that.
     [?] See https://sailsjs.com/support for help.
        at machineAsAction (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\machine-as-action\lib\machine-as-action.js:271:28)
        at helpRegisterAction (C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\app\private\controller\help-register-action.js:63:27)
        at C:\Users\Mercurius\Documents\GitHub\Homie-Web\node_modules\sails\lib\app\private\controller\load-action-modules.js:146:13
    

    是否有人知道文档中关于如何在此中生成可选密钥的位置?我试过了- http://node-machine.org/spec/machine#inputs -但没有运气。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Dejan KubaÅ¡a    6 年前

    类型必须是'string'、'number'、'boolean'、'json'或'ref',就像error say一样。 因此,您需要将type设置为'ref'(对象或数组),并且可以使用自定义函数进行验证。

    inputs: {
            fields: {
                type: 'ref',
                custom: function (data) {
                    // some logic
                    // example
                    if (typeof data.body !== "string") {
                        return false;
                        // or u can use trow Error('Body is not a string')
                    }
                    return true;
                },
                required: true,
                description: 'All keys are not required, but at least one is'
            }
    

    现在输入是type object和in custom function return false trow Error('Some problem') 中断验证。

    如果使用模式类型,只需删除 ? 从你的例子来看:

    inputs: {
            fields: {
                type: {
                  body: 'string',
                  rruleSetStr: 'string'
                },
                required: true,
                description: 'All keys are not required, but at least one is'
            }
    

    这是 Runtime (recursive) type-checking for JavaScript. ,请检查文档以了解编写规则。

    推荐文章