How to use openapi-schema-validation - 4 common examples

To help you get started, we’ve selected a few openapi-schema-validation examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anttiviljami / serverless-openapi-joi-boilerplate / scripts / build-swaggerui.js View on Github external
const openapi = new OpenAPIBuilder({
    info,
    servers: [{ url: ServiceEndpoint }],
    routes,
  });

  // create static files directory if not exists
  const outputPath = SwaggerUIBucketName ?
    path.join(__dirname, '..', '.serverless-static') : path.join(__dirname, '..', 'static');
  fs.existsSync(outputPath) || fs.mkdirSync(outputPath);

  // generate swagger.json
  const apispec = openapi.getDefinition(routes, ServiceEndpoint);

  // validate openapi and warn in case there are issues
  const { valid, errors } = validate(apispec, 3);
  if (valid) {
    console.info('\x1b[32m[success]\x1b[0m Document is valid OpenAPI v3!');
  } else {
    console.warn(`\x1b[33m[warning]\x1b[0m Document is not valid OpenAPI!\n ${errors.length} validation errors:\n` +
      JSON.stringify(errors, null, 2));
  }

  // write to swagger.json file under static/
  const swaggerFile = path.join(outputPath, 'swagger.json');
  fs.writeFileSync(swaggerFile, JSON.stringify(apispec));
  console.info(`[info] Wrote OpenAPI spec to ${path.basename(outputPath)}/${path.basename(swaggerFile)}`);

  // copy swagger ui dist files
  const swaggerDist = getAbsoluteFSPath();
  const swaggerFiles = fs.readdirSync(swaggerDist)
    .filter(file => !file.endsWith('.map'));
github anttiviljami / serverless-openapi-joi-boilerplate / __tests__ / openapi.ts View on Github external
toBeValidOpenAPI(received: any, version: number = 3) {
    const { valid, errors } = validate(received, version);
    return valid ? {
      pass: true,
      message: () => `Document is valid openapi v${version}`,
    } : {
      pass: false,
      message: () => `Document is not valid openapi v${version}, ${errors.length} validation errors:\n` +
        JSON.stringify(errors, null, 2),
    };
  },
});
github anttiviljami / openapi-backend / src / backend.ts View on Github external
public validateDefinition() {
    const { valid, errors } = validateOpenAPI(this.document, 3);
    if (!valid) {
      const prettyErrors = JSON.stringify(errors, null, 2);
      throw new Error(`Document is not valid OpenAPI. ${errors.length} validation errors:\n${prettyErrors}`);
    }
    return this.document;
  }
github anttiviljami / openapi-client-axios / src / client.ts View on Github external
public validateDefinition = () => {
    const { valid, errors } = validateOpenAPI(this.document, 3);
    if (!valid) {
      const prettyErrors = JSON.stringify(errors, null, 2);
      throw new Error(`Document is not valid OpenAPI. ${errors.length} validation errors:\n${prettyErrors}`);
    }
    return this.document;
  };

openapi-schema-validation

Validate openapi documents.

MIT
Latest version published 6 years ago

Package Health Score

69 / 100
Full package analysis

Popular openapi-schema-validation functions