Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type guards for SchemaObject and ReferenceObject #46

Merged
merged 1 commit into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/model/OpenApi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "mocha";
import { expect } from "chai";
import { isSchemaObject, isReferenceObject, SchemaObject, ReferenceObject } from "./OpenApi";

describe('type-guards unit tests', () => {
describe('isSchemaObject()', () => {
it('returns true for a schema object', () => {
const schemaObject = new TestSchemaObject();
expect(isSchemaObject(schemaObject)).to.equal(true);
});

it('returns false for a reference object', () => {
const referenceObject = new TestReferenceObject();
expect(isSchemaObject(referenceObject)).to.equal(false);
});
});

describe('isReferenceObject()', () => {
it('returns true for a reference object', () => {
const referenceObject = new TestReferenceObject();
expect(isReferenceObject(referenceObject)).to.equal(true);
});

it('returns false for a schema object', () => {
const schemaObject = new TestSchemaObject();
expect(isReferenceObject(schemaObject)).to.equal(false);
});
});
});

class TestSchemaObject implements SchemaObject {
// empty schema
}

class TestReferenceObject implements ReferenceObject {
$ref = 'test';
}
25 changes: 25 additions & 0 deletions src/model/OpenApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,21 @@ export interface TagObject extends ISpecificationExtension {
export interface ExamplesObject {
[name: string]: ExampleObject | ReferenceObject;
}

export interface ReferenceObject {
$ref: string;
}

/**
* A type guard to check if the given value is a `ReferenceObject`.
* See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
*
* @param obj The value to check.
*/
export function isReferenceObject(obj: object): obj is ReferenceObject {
return obj.hasOwnProperty("$ref");
}

export interface SchemaObject extends ISpecificationExtension {
nullable?: boolean;
discriminator?: DiscriminatorObject;
Expand Down Expand Up @@ -289,6 +301,19 @@ export interface SchemaObject extends ISpecificationExtension {
enum?: any[];
}

/**
* A type guard to check if the given object is a `SchemaObject`.
* Useful to distinguish from `ReferenceObject` values that can be used
* in most places where `SchemaObject` is allowed.
*
* See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
*
* @param schema The value to check.
*/
export function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject {
return !schema.hasOwnProperty('$ref');
}

export interface SchemasObject {
[schema: string]: SchemaObject;
}
Expand Down