Skip to content

Commit

Permalink
feat(openapi-ns): add predicates
Browse files Browse the repository at this point in the history
Refs #60
  • Loading branch information
char0n committed Aug 28, 2020
1 parent 9a25dd2 commit 46a2161
Show file tree
Hide file tree
Showing 19 changed files with 848 additions and 14 deletions.
4 changes: 3 additions & 1 deletion apidom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apidom/packages/apidom-ns-asyncapi2-0/src/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const isComponentsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeInfo = isElementType('components');
const primitiveEqObject = primitiveEq('object');
const hasGetterSchemes = hasGetter('schemes');
const hasGetterSchemes = hasGetter('schemas');

return either(
is(ComponentsElement),
Expand Down
4 changes: 2 additions & 2 deletions apidom/packages/apidom-ns-asyncapi2-0/test/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ describe('predicates', function () {
primitive() {
return 'object';
},
get schemes() {
return 'schemes';
get schemas() {
return 'schemas';
},
};

Expand Down
7 changes: 7 additions & 0 deletions apidom/packages/apidom-ns-openapi3-1/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
recursive: true,
spec: 'test/**/*.ts',
require: ['test/mocha-bootstrap.js'],
};
5 changes: 4 additions & 1 deletion apidom/packages/apidom-ns-openapi3-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix",
"clean": "rimraf ./es ./lib ./dist ./types",
"test": "cross-env BABEL_ENV=commonjs mocha",
"typescript:check-types": "tsc --noEmit",
"typescript:declaration": "tsc -p declaration.tsconfig.json",
"security-audit": "run-s -sc security-audit:prod security-audit:dev",
Expand All @@ -24,6 +25,8 @@
"author": "Vladimir Gorej",
"license": "Apache-2.0",
"dependencies": {
"minim": "=0.23.8"
"minim": "=0.23.8",
"ramda": "=0.27.0",
"apidom": "file:../apidom"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, ObjectElement } from 'minim';

class Components extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'components';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, ObjectElement, StringElement } from 'minim';

class Contact extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'contact';
}
Expand Down
2 changes: 1 addition & 1 deletion apidom/packages/apidom-ns-openapi3-1/src/elements/Info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ContactElement from './Contact';
import LicenseElement from './License';

class Info extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'info';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, ObjectElement, StringElement } from 'minim';

class License extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'license';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ServerElement from './Server';
import ComponentsElement from './Components';

class OpenApi3_1 extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'openApi3-1';
this.classes.push('api');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, StringElement } from 'minim';

class Openapi extends StringElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'openapi';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, ObjectElement } from 'minim';

class Schema extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'schema';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Attributes, Meta, ObjectElement, StringElement } from 'minim';
import ServerVariable from './ServerVariable';

class Server extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'server';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Attributes, Meta, ObjectElement, ArrayElement, StringElement } from 'minim';

class ServerVariable extends ObjectElement {
constructor(content: Array<unknown>, meta: Meta, attributes: Attributes) {
constructor(content?: Array<unknown>, meta?: Meta, attributes?: Attributes) {
super(content, meta, attributes);
this.element = 'serverVariable';
}
Expand Down
36 changes: 36 additions & 0 deletions apidom/packages/apidom-ns-openapi3-1/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export { default } from './namespace';

export {
isRefElement,
isLinkElement,
isMemberElement,
isObjectElement,
isArrayElement,
isBooleanElement,
isNullElement,
isElement,
isNumberElement,
isStringElement,
} from 'apidom';

export {
isOpenApiApi3_1Element,
isContactElement,
isLicenseElement,
isInfoElement,
isComponentsElement,
isSchemaElement,
isOpenapiElement,
isServerElement,
isServerVariableElement,
} from './predicates';

export { default as ComponentsElement } from './elements/Components';
export { default as ContactElement } from './elements/Contact';
export { default as InfoElement } from './elements/Info';
export { default as LicenseElement } from './elements/License';
export { default as OpenapiElement } from './elements/Openapi';
export { default as OpenApi3_1Element } from './elements/OpenApi3-1';
export { default as SchemaElement } from './elements/Schema';
export { default as ServerElement } from './elements/Server';
export { default as ServerVariableElement } from './elements/ServerVariable';
194 changes: 194 additions & 0 deletions apidom/packages/apidom-ns-openapi3-1/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { allPass, either, is } from 'ramda';
import { createPredicate } from 'apidom';

import ComponentsElement from './elements/Components';
import ContactElement from './elements/Contact';
import InfoElement from './elements/Info';
import LicenseElement from './elements/License';
import OpenapiElement from './elements/Openapi';
import OpenApi3_1Element from './elements/OpenApi3-1';
import SchemaElement from './elements/Schema';
import ServerElement from './elements/Server';
import ServerVariableElement from './elements/ServerVariable';

export const isOpenApiApi3_1Element = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter, hasClass }) => {
const isElementTypeOpenApi3_1 = isElementType('openApi3-1');
const primitiveEqObject = primitiveEq('object');
const hasClassApi = hasClass('api');
const hasGetterOpenapi = hasGetter('openapi');
const hasGetterInfo = hasGetter('info');
const hasGetterServers = hasGetter('servers');
const hasGetterComponents = hasGetter('components');

return either(
is(OpenApi3_1Element),
allPass([
hasBasicElementProps,
isElementTypeOpenApi3_1,
primitiveEqObject,
hasClassApi,
hasGetterOpenapi,
hasGetterInfo,
hasGetterServers,
hasGetterComponents,
]),
);
},
);

export const isOpenapiElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeOpenapi = isElementType('openapi');
const primitiveEqString = primitiveEq('string');
const hasGetterLength = hasGetter('length');

return either(
is(OpenapiElement),
allPass([hasBasicElementProps, isElementTypeOpenapi, primitiveEqString, hasGetterLength]),
);
},
);

export const isInfoElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeInfo = isElementType('info');
const primitiveEqObject = primitiveEq('object');
const hasGetterTitle = hasGetter('title');
const hasGetterDescription = hasGetter('description');
const hasGetterSummary = hasGetter('summary');
const hasGetterTermsOfService = hasGetter('termsOfService');
const hasGetterVersion = hasGetter('version');
const hasGetterLicense = hasGetter('license');
const hasGetterContact = hasGetter('contact');

return either(
is(InfoElement),
allPass([
hasBasicElementProps,
isElementTypeInfo,
primitiveEqObject,
hasGetterTitle,
hasGetterDescription,
hasGetterSummary,
hasGetterTermsOfService,
hasGetterVersion,
hasGetterLicense,
hasGetterContact,
]),
);
},
);

export const isLicenseElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeLicense = isElementType('license');
const primitiveEqObject = primitiveEq('object');
const hasGetterName = hasGetter('name');
const hasGetterIdentifier = hasGetter('identifier');
const hasGetterUrl = hasGetter('url');

return either(
is(LicenseElement),
allPass([
hasBasicElementProps,
isElementTypeLicense,
primitiveEqObject,
hasGetterName,
hasGetterIdentifier,
hasGetterUrl,
]),
);
},
);

export const isContactElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeContact = isElementType('contact');
const primitiveEqObject = primitiveEq('object');
const hasGetterName = hasGetter('name');
const hasGetterUrl = hasGetter('url');
const hasGetterEmail = hasGetter('email');

return either(
is(ContactElement),
allPass([
hasBasicElementProps,
isElementTypeContact,
primitiveEqObject,
hasGetterName,
hasGetterUrl,
hasGetterEmail,
]),
);
},
);

export const isComponentsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeInfo = isElementType('components');
const primitiveEqObject = primitiveEq('object');
const hasGetterSchemes = hasGetter('schemas');

return either(
is(ComponentsElement),
allPass([hasBasicElementProps, isElementTypeInfo, primitiveEqObject, hasGetterSchemes]),
);
},
);

export const isSchemaElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
const isElementTypeInfo = isElementType('schema');
const primitiveEqObject = primitiveEq('object');

return either(
is(SchemaElement),
allPass([hasBasicElementProps, isElementTypeInfo, primitiveEqObject]),
);
},
);

export const isServerElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeInfo = isElementType('server');
const primitiveEqObject = primitiveEq('object');
const hasGetterUrl = hasGetter('url');
const hasGetterDescription = hasGetter('description');
const hasGetterVariables = hasGetter('variables');

return either(
is(ServerElement),
allPass([
hasBasicElementProps,
isElementTypeInfo,
primitiveEqObject,
hasGetterUrl,
hasGetterDescription,
hasGetterVariables,
]),
);
},
);

export const isServerVariableElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq, hasGetter }) => {
const isElementTypeInfo = isElementType('serverVariable');
const primitiveEqObject = primitiveEq('object');
const hasGetterDefault = hasGetter('default');
const hasGetterDescription = hasGetter('description');
const hasGetterEnum = hasGetter('enum');

return either(
is(ServerVariableElement),
allPass([
hasBasicElementProps,
isElementTypeInfo,
primitiveEqObject,
hasGetterDefault,
hasGetterDescription,
hasGetterEnum,
]),
);
},
);
Loading

0 comments on commit 46a2161

Please sign in to comment.