@windingtree/wt-write-api
Version:
API to write data to the Winding Tree platform
53 lines (49 loc) • 1.93 kB
JavaScript
const openapi2schema = require('openapi2schema');
const fs = require('fs');
const _ = require('lodash');
/**
* Usage: `npm run regenerate-schemas`
* Use this after updating swagger.yaml to update json object schemas used for validation.
*
* Build will fail if changes are found at build time.
* $ [ -z "$(git diff --name-status --diff-filter=M src/services/validators/)" ] || exit 1
*/
const SCHEMA_DEFS = [
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'hotel', 'properties', 'description'],
filePath: 'src/services/validators/description-schema.json',
},
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'hotel', 'properties', 'availability'],
filePath: 'src/services/validators/availability-schema.json',
},
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'hotel', 'properties', 'ratePlans'],
filePath: 'src/services/validators/rateplans-schema.json',
},
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'legalEntity', 'oneOf', 0, 'properties', 'name'],
filePath: 'src/services/validators/orgname-schema.json',
},
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'legalEntity', 'oneOf', 0, 'properties', 'address'],
filePath: 'src/services/validators/address-schema.json',
},
{
schemaPath: ['/hotels', 'post', 'body', 'oneOf', 1, 'properties', 'legalEntity', 'oneOf', 0, 'properties', 'contact'],
filePath: 'src/services/validators/contact-schema.json',
},
];
// load base schema
openapi2schema('docs/swagger.yaml', { async: true }, (err, result) => {
if (err) {
// openapi2schema fails
console.error(err);
process.exit(1);
} else {
for (let definition of SCHEMA_DEFS) {
const schema = _.get(result, definition.schemaPath);
fs.writeFileSync(definition.filePath, JSON.stringify(schema, null, ' '));
}
}
});