输入定义可选键

时间:2019-03-12 03:07:17

标签: sails.js

我用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-但没有运气。

1 个答案:

答案 0 :(得分:1)

类型必须为'string','number','boolean','json'或'ref',例如错误提示。 因此,您需要将类型设置为“ 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'
        }

现在输入为类型对象,并在自定义函数return falsetrow Error('Some problem')中进行中断验证。

如果您使用架构类型,只需从示例中删除?

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

这是Azure Active Directory On-Behalf-Of,请检查文档以编写规则。