2017-12-03 19 views
1

TypeScriptでjoi@types/joiを使用しています。 Joiにはextendというメソッドがあり、元のjoiライブラリを変更せずに新しいインスタンスを返すことでjoiを拡張することができます。私はそれを使って拡張インスタンスを作成しました。この拡張されたインスタンスの定義を作成するTypeScriptを使用してnpmからモジュールを拡張するには?

は、私は以下のコードを用いて説明hereようModule Augmentationを試みた:

declare module 'joi' { 
    // Add a new Schema type which has noChildren() method. 
    interface CustomSchema extends ObjectSchema { 
    noChildren(): this; 
    } 
} 

をしかし、予想通り、これはそれを増強することにより、元の定義を変更します。私が望むのは、元のものから変更せずにすべてを継承する拡張インスタンスの定義を作成することです。

Joiを拡張し、以下のように作成されます。

import * as Joi from 'joi'; 
const JoiExtended = Joi.extend({...some implementation...}) 
// How to export? 
// export * from 'Joi' ---> In this case, original non-extended Joi is exported 
// export default JoiExtended ---> Imported `Joi` reports: Cannot find namespace 'Joi' 
  1. どのように拡張された定義を作成することができますか?
  2. 拡張子をエクスポートする方法Joi

P.私はTypeScriptを勉強していますが、この質問の答えを探しましたが、多分、私はタイプコピーの用語と間違った用語を検索するのに慣れていないので、答えを見つけることができませんでした。

答えて

1

Joi.extendは、Rootタイプを使用できるjoiモジュールの新しいインスタンスを返します。

Joi.Rootを拡張するインターフェイスと、拡張するベースのjoiタイプを拡張するインターフェイスを作成する必要があります。カスタムjoiインスタンスは、他のオブジェクトと同じようにエクスポートできます。

extend()API Documentationの例のround()dividable()の規則を使用する例を以下に示します。

import * as Joi from 'joi'; 

interface ExtendedNumberSchema extends Joi.NumberSchema { 
    round(): this; 
    dividable(num: number): this; 
} 

interface ExtendedJoi extends Joi.Root { 
    number(): ExtendedNumberSchema; 
} 

const customJoi: ExtendedJoi = Joi.extend((joi) => ({ 
    base: joi.number(), 
    name: 'number', 
    language: { 
     round: 'needs to be a rounded number', // Used below as 'number.round' 
     dividable: 'needs to be dividable by {{q}}' 
    }, 
    pre(value, state, options) { 

     if (options.convert && this._flags.round) { 
      return Math.round(value); // Change the value 
     } 

     return value; // Keep the value as it was 
    }, 
    rules: [ 
     { 
      name: 'round', 
      setup(params) { 

       this._flags.round = true; // Set a flag for later use 
      }, 
      validate(params, value, state, options) { 

       if (value % 1 !== 0) { 
        // Generate an error, state and options need to be passed 
        return this.createError('number.round', {v: value}, state, options); 
       } 

       return value; // Everything is OK 
      } 
     }, 
     { 
      name: 'dividable', 
      params: { 
       q: joi.alternatives([joi.number().required(), joi.func().ref()]) 
      }, 
      validate(params, value, state, options) { 

       if (value % params.q !== 0) { 
        // Generate an error, state and options need to be passed, q is used in the language 
        return this.createError('number.dividable', {v: value, q: params.q}, state, options); 
       } 

       return value; // Everything is OK 
      } 
     } 
    ] 
})); 


const schema = { 
    a: customJoi.number().round().dividable(3) 
}; 

const result = customJoi.validate({a: 4.1}, schema); // will fail because 4 is no divisible by 3 

console.log(result); 

export = customJoi; 
関連する問題