2016-07-22 4 views
0

この質問が明白であると思われる場合は、私は失礼ですが、私はExpress、Node、Swaggerを初めて使用しています。ExpressでSwagger APIリクエストの自動検証?

私はAPI用のSwagger仕様を書いています。

Swagger.jsonファイルとともに要求されたパラメータが存在すること、それらのパラメータの列挙値が正しいことなどを確認するために、文書化されたSwagger APIに要求を渡すためのツールはありますか?

に似て何か:

validator.validate ("./swagger.json", req, function (req, res, err) { 
    if (err) { 
     res.status('400').send(err.message()); 
    } 
    else { 
     // Call my controller 
     swaggerController(req, res); 
    } 
}); 

私はそこにあると信じて、見つけることは困難ですか、私は正しいものを探していませんよ。

答えて

0

次に、swagger json-schemaに対する着信応答を検証するためのミドルウェアの例を示します。ただ、コンセプトの証明が、それは正しい方向にあなたを指すことがあります。

swagger-json-schema-middleware-example

0

はい、あなたがこれを行うことができます。 正確にこれを行うジェネレータプロジェクトがあります。入手するhere

APIを使用すると、Api.yamlで提供されているSwagger APIの説明に対して、各APIリクエストが検証されます。たとえば、/examplesPOST要求のbodyを検証するために、次の操作を行うことができ

編集Api.yaml

... 
definitions: 
    # define the example body i.e. require the property name 
    ExampleBody: 
    type: object 
    title: example 
    required: 
     - name 
    properties: 
     name: 
     type: string 
     description: The example name 
paths: 
    # define your /examples POST endpoint 
    # reference the ExamplesBody definition from above 
    /examples: 
    post: 
     tags: 
     - Examples 
     description: Create a new example 
     parameters: 
     - name: example 
      in: body 
      description: number of items to skip 
      required: true 
      schema: 
      $ref: "#/definitions/ExampleBody" 
     responses: 
     200: 
      description: Returns all examples 
... 

次に、Node.jsのコードでのルートハンドラを作成しますPOSTから/例

app.post('/examples', function (req, res) { /* your handler logic here */ /* no need to validate the request payload. it will be done automatically */ });

注:のみあなたのハンドラのロジックを含みます。身体の確認は自動的に処理されます。

関連する問題