Skip to content

Commit

Permalink
feat: add simplified channel parameter (#816)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Urbańczyk <[email protected]>
  • Loading branch information
jonaslagoni and magicmatatjahu authored Jul 26, 2023
1 parent 2000140 commit c9f56b1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/models/v3/channel-parameter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { BaseModel } from '../base';
import { Schema } from './schema';

import { hasDescription, description, extensions } from './mixins';
import { Schema } from './schema';

import type { ChannelParameterInterface } from '../channel-parameter';
import type { SchemaInterface } from '../schema';
import type { ExtensionsInterface } from '../extensions';

import type { v3 } from '../../spec-types';

export class ChannelParameter extends BaseModel<v3.ParameterObject, { id: string }> implements ChannelParameterInterface {
Expand All @@ -15,12 +13,23 @@ export class ChannelParameter extends BaseModel<v3.ParameterObject, { id: string
}

hasSchema(): boolean {
return !!this._json.schema;
return this._json && (
this._json.description !== undefined ||
this._json.default !== undefined ||
this._json.enum !== undefined ||
this._json.examples !== undefined
);
}

schema(): SchemaInterface | undefined {
if (!this._json.schema) return undefined;
return this.createModel(Schema, this._json.schema, { pointer: `${this._meta.pointer}/schema` });
if (!this.hasSchema()) return undefined;
return this.createModel(Schema, {
type: 'string',
description: this._json.description,
enum: this._json.enum,
default: this._json.default,
examples: this._json.examples
}, { pointer: `${this._meta.pointer}` });
}

hasLocation(): boolean {
Expand Down
4 changes: 3 additions & 1 deletion src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ export type ParametersObject = Record<string, ParameterObject | ReferenceObject>

export interface ParameterObject extends SpecificationExtensions {
description?: string;
schema?: SchemaObject;
enum?: string[];
default?: string;
examples?: string[];
location?: string;
}

Expand Down
32 changes: 28 additions & 4 deletions test/models/v3/channel-parameter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,23 @@ describe('ChannelParameter model', function() {
});

describe('.hasSchema()', function() {
it('should return true when there is a value', function() {
const doc = serializeInput<v3.ParameterObject>({ schema: {} });
it('should return true if enum are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ enum: ['test'] });
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(true);
});
it('should return true if default are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ default: 'test' });
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(true);
});

it('should return true if examples are sat', function() {
const doc = serializeInput<v3.ParameterObject>({ examples: ['test'] });
const d = new ChannelParameter(doc);
expect(d.hasSchema()).toEqual(true);
});

it('should return false when there is no value', function() {
const doc = serializeInput<v3.ParameterObject>({});
const d = new ChannelParameter(doc);
Expand All @@ -57,10 +68,23 @@ describe('ChannelParameter model', function() {
});

describe('.schema()', function() {
it('should return the value', function() {
const doc = serializeInput<v3.ParameterObject>({ schema: {} });
it('should be able to access enum values', function() {
const doc = serializeInput<v3.ParameterObject>({ enum: ['test'] });
const d = new ChannelParameter(doc);
expect(d.schema()).toBeInstanceOf(Schema);
expect(d.schema()?.enum()).toEqual(['test']);
});
it('should be able to access examples values', function() {
const doc = serializeInput<v3.ParameterObject>({ examples: ['test'] });
const d = new ChannelParameter(doc);
expect(d.schema()).toBeInstanceOf(Schema);
expect(d.schema()?.examples()).toEqual(['test']);
});
it('should be able to access default value', function() {
const doc = serializeInput<v3.ParameterObject>({ default: 'test' });
const d = new ChannelParameter(doc);
expect(d.schema()).toBeInstanceOf(Schema);
expect(d.schema()?.default()).toEqual('test');
});

it('should return undefined when there is no value', function() {
Expand Down

0 comments on commit c9f56b1

Please sign in to comment.