question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation error despite valid schema.

See original GitHub issue

Hi,

I’d really like to give open-api for express a chance. - The concept is very cool!

My code is this:

const express = require('express');
const openapi = require('express-openapi');
const v1UserService = require('./api/v1/services/userService');


const app = express();
openapi.initialize({
  app,
  // NOTE: If using yaml you can provide a path relative to process.cwd() e.g.
  // apiDoc: './api-v1/api-doc.yml',
  apiDoc: './api/v1/doc/testproject.v1.yaml',
  dependencies: {
    userService: v1UserService
  },
  paths: './api/v1/paths'
});

app.listen(3000);

However, when I try to run my .yaml I run into the following error:

express-openapi: Validating schema before populating paths
express-openapi: validation errors [
  {
    "keyword": "type",
    "dataPath": "",
    "schemaPath": "#/type",
    "params": {
      "type": "object"
    },
    "message": "should be object"
  }
]
E:\projects\test-openapi\node_modules\openapi-framework\dist\index.js:100
                throw new Error(this.loggingPrefix + "args.apiDoc was invalid.  See the output.");
                ^

Error: express-openapi: args.apiDoc was invalid.  See the output.

I can confirm, that my .yaml file is valid:

openapi: 3.0.0
info:
  title: testproject
  version: '1.0'
  contact:
    name: me
    email: me@me.me
    url: ''
  license:
    name: closed source
  description: This is the API service for testproject.
servers:
  - url: 'http://localhost:1337'
    description: dev
paths:
  /users:
    get:
      summary: bulk create users
      tags: []
      responses:
        '200':
          description: OK
          headers: {}
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
              examples: {}
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      operationId: getUser
      description: Get a User based on the query provided in the body.
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                id:
                  type: string
                username:
                  type: string
                active:
                  type: string
                firstName:
                  type: string
                lastName:
                  type: string
                email:
                  type: string
            examples:
              Query for username:
                value:
                  user: frank
              Query for status:
                value: {}
    parameters: []
    put:
      summary: ''
      operationId: updateUsers
      responses:
        '200':
          description: OK
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/User'
      description: Updates an array of users. The provided objects will be replaced entirely.
    patch:
      summary: ''
      operationId: archiveUsers
      responses:
        '200':
          description: OK
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      description: |-
        This operation allows to manage the archived state of the object.
        Use this operation instead of delete.
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                type: object
                properties:
                  _id:
                    type: string
                  archived:
                    type: boolean
    post:
      summary: ''
      operationId: createUsers
      responses:
        '201':
          description: Created
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/User'
      description: Bulk create Users.
  /users/login:
    post:
      summary: ''
      operationId: login
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  token:
                    type: string
                  user:
                    $ref: '#/components/schemas/User'
      description: ''
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  type: string
                password:
                  type: string
              required:
                - username
                - password
      security: []
  /users/logout:
    post:
      summary: ''
      operationId: logout
      responses:
        '200':
          description: OK
      description: ''
  /register:
    post:
      summary: ''
      operationId: registerUser
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                type: object
                properties:
                  token:
                    type: string
                  user:
                    $ref: '#/components/schemas/User'
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      security: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
  /flights:
    get:
      summary: bulk read Flights (paged)
      tags: []
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  pages:
                    $ref: '#/components/schemas/Pages'
                  flights:
                    type: array
                    items:
                      $ref: '#/components/schemas/Flight'
          headers: {}
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      operationId: getFlights
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                dep:
                  type: array
                  items:
                    type: string
                arr:
                  type: array
                  items:
                    type: string
                flt:
                  type: string
                fromStd:
                  type: string
                toStd:
                  type: string
                fromSta:
                  type: string
                toSta:
                  type: string
      parameters:
        - schema:
            type: integer
            default: 0
          in: query
          name: page
          description: 'The page of results to be returned (indexed: 0).'
        - schema:
            type: integer
            default: 20
          in: query
          name: pageSize
          description: The maximum number of results per page.
      description: ''
    post:
      summary: bulk create Flights
      operationId: createFlights
      responses:
        '201':
          description: Created
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/Flight'
    put:
      summary: bulk replace Flights
      operationId: replaceFlights
      responses:
        '200':
          description: OK
        '400':
          description: Bad Request
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
      description: Updates an array of flights. The provided objects will be replaced entirely.
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/Flight'
    patch:
      summary: bulk set archived state of Flights
      operationId: archiveFlight
      responses:
        '200':
          description: OK
      requestBody:
        content:
          application/json:
            schema:
              type: array
              items:
                type: object
                properties:
                  _id:
                    type: string
                  archived:
                    type: boolean
      description: |-
        This operation allows to manage the archived state of the object.
        Use this operation instead of delete.
components:
  schemas:
    User:
      title: User
      type: object
      x-examples:
        example-1:
          id: string
          user: string
          password: string
          active: string
          firstName: string
          lastName: string
          email: string
      properties:
        _id:
          type: string
          readOnly: true
        username:
          type: string
        password:
          type: string
        active:
          type: boolean
        firstName:
          type: string
        lastName:
          type: string
        email:
          type: string
        archived:
          type: boolean
      required:
        - username
        - password
    Flight:
      title: Flight
      type: object
      properties:
        _id:
          type: string
          readOnly: true
        flt:
          type: string
        callsign:
          type: string
        dep:
          type: string
        arr:
          type: string
        std:
          type: string
        sta:
          type: string
        etd:
          type: string
        eta:
          type: string
        atd:
          type: string
        ata:
          type: string
        archived:
          type: boolean
      required:
        - flt
        - callsign
        - dep
        - arr
        - std
        - sta
    Pages:
      title: Pages
      type: object
      properties:
        pageNumber:
          type: integer
          description: The index of the returned page.
        pageSize:
          type: integer
          description: The number of elements per page.
        totalPages:
          type: integer
          description: The total number of pages.
        totalElements:
          type: integer
          description: The total number of elements available.
  securitySchemes:
    Authorization:
      type: http
      scheme: bearer
  responses: {}
  parameters: {}
security:
  - Authorization: []

I’m wondering if there’s a way to get a more verbose output to understand why the initialization fails.

Kind regards

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jsdevelcommented, Dec 6, 2021

@pate313373 please feel free to submit a PR!

1reaction
tricooscommented, Dec 3, 2021

I found the reason for my error message: the file simply did not exist at the path defined in apiDoc. Could the devs please add a single if to give a better error message like The path ... specified in apiDoc does not exist ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] schemathesis fails with validation error despite schema ...
Describe the bug schemathesis fails with validation error despite schema is openapi 3.0.0 valid. To Reproduce $ docker run --network="host" ...
Read more >
How to Fix Schema Validation Errors
The most common schema validation error is "either "offers", "review", or "aggregate rating" should be specified." Here's how to fix it.
Read more >
Json schema validation shows no errors - Stack Overflow
I'm using Ajv version 6.10.2 to validate a simple Json schema that is separated in two files, but the thing is that I...
Read more >
Detailed validation error information - Json.NET
This sample validates a JObject using the IsValid(JToken, JSchema, IList<ValidationError> ) extension method and returns a collection of ValidationErrors.
Read more >
Mongoose v6.8.2: Validation
If an error occurs, your Model#save callback receives it; Validation is customizable. const schema = new Schema({ name: { type: String, required: true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found