用自定义类型扩展Joi以填充字符串

时间:2019-02-04 17:59:24

标签: javascript hapijs joi

我想通过使用populatedString为基于.extend(..)的类型创建joi.string()的自定义Joi类型:

  • 修剪字符串
  • 如果undefined,则将值更改为trimmed string === '',以便经过验证的输出完全不包含密钥
  • 将覆盖.required(),使其作用于修剪后的字符串,并使用我自己的语言创建错误。在我的类型上设置.required()时,这意味着它需要一个不仅包含空格或为空的字符串

到目前为止我的尝试很接近:

    const StandardJoi = require("joi");

    const Joi = StandardJoi.extend(joi => ({
      base: joi.string(),
      name: "populatedString",
      language: {
        required: "needs to be a a string containing non whitespace characters"
      },
      pre(value, state, options) {
        value = value.trim();
        return value === "" ? undefined : value;
      },
      rules: [
        {
          name: "required",
          validate(params, value, state, options) {
            if (value === undefined) {
              return this.createError(
                "populatedString.required",
                { v: value },
                state,
                options
              );
            }

            return value;
          }
        }
      ]
    }));

它起作用的例子

    Joi.populatedString().validate(" x "); // $.value === 'x'
    Joi.populatedString().validate("  "); // $.value === undefined

    // $.error.details
    //
    // [ { message: '"value" needs to be a a string containing non whitespace characters',​​​​​
    // ​​​​​    path: [],​​​​​
    // ​​​​​    type: 'populatedString.required',​​​​​
    // ​​​​​    context: { v: undefined, key: undefined, label: 'value' } } ]​​​​​
    Joi.populatedString()
      .required()
      .validate("  ");

    // $.value
    //
    // { inObj1: 'o' }
    Joi.object()
      .keys({
        inObj1: Joi.populatedString()
      })
      .validate({ inObj1: " o " });

但是它并不会(应该)失败

    // ​​​​​{ error: null, value: {}, then: [λ: then], catch: [λ: catch] }​​​​​
    Joi.object()
      .keys({
        inObj2: Joi.populatedString(),
        inObj3: Joi.populatedString().required()
      })
      .validate({ inObj2: "  " });

即使inObj3.required()且未提供,它也不会失败。

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题:

MTLBlitCommandEncoder

解决方法是添加func draw(in view: MTKView) { guard let drawable = view.currentDrawable else {return} guard let commandBuffer = commandQueue.makeCommandBuffer() else {fatalError()} guard let blitEncoder = commandBuffer.makeBlitCommandEncoder() else{fatalError()} guard let texture = self.texture else {fatalError()} let targetW = min(texture.width, drawable.texture.width) //720, 1125 in my case let targetH = min(texture.height, drawable.texture.height) //1280, 2436 blitEncoder.copy(from: texture, sourceSlice: 0, sourceLevel: 0, sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0), sourceSize: MTLSizeMake(targetW, targetH, texture.depth), to: drawable.texture, destinationSlice: 0, destinationLevel: 0, destinationOrigin: MTLOrigin(x: 0, y: 0, z: 0)) blitEncoder.endEncoding() commandBuffer.present(drawable) commandBuffer.commit() commandBuffer.waitUntilCompleted() } ,并在调用 const BaseJoi = require("joi"); const Joi = BaseJoi.extend(joi => ({ base: joi.string(), name: "populatedString", language: { required: "needs to be a a string containing non whitespace characters" }, pre(value, state, options) { value = value.trim(); return value === "" ? undefined : value; }, rules: [ { name: "required", setup(params) { return this.options({ presence: "required" }); }, validate(params, value, state, options) { if (value === undefined) { return this.createError( "populatedString.required", { v: value }, state, options ); } return value; } } ] })); 时让其设置选项setup