Skip to content

Commit

Permalink
feat: support built-in JavaScript/Node schema types
Browse files Browse the repository at this point in the history
Add support for built-in JavaScript/Node schema types in Model
properties
  • Loading branch information
Hage Yaapa authored and hacksparrow committed Sep 24, 2018
1 parent b7c60c5 commit d65a17f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/openapi-v3/src/generate-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function resolveSchema(
} else if (fn === Boolean) {
resolvedSchema = {type: 'boolean'};
} else if (fn === Date) {
resolvedSchema = {type: 'string', format: 'date'};
resolvedSchema = {type: 'string', format: 'date-time'};
} else if (fn === Object) {
resolvedSchema = {type: 'object'};
} else if (fn === Array) {
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-v3/test/unit/generate-schema.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('generate-schema unit tests', () => {
});

it('resolves type Date', () => {
expect(resolveSchema(Date)).to.eql({type: 'string', format: 'date'});
expect(resolveSchema(Date)).to.eql({type: 'string', format: 'date-time'});
});

it('resolves type Object', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function isComplexType(ctor: Function) {
Object,
Function,
Array,
Date,
] as Function[]).includes(ctor);
}

Expand Down Expand Up @@ -125,6 +126,11 @@ export function metaToJsonProperty(meta: PropertyDefinition): JSONSchema {

if (isComplexType(propertyType)) {
Object.assign(propDef, {$ref: `#/definitions/${propertyType.name}`});
} else if (propertyType === Date) {
Object.assign(propDef, {
type: 'string',
format: 'date-time',
});
} else {
Object.assign(propDef, {
type: <JSONSchemaTypeName>propertyType.name.toLowerCase(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ describe('build-schema', () => {
expectValidJsonSchema(jsonSchema);
});

it('properly converts date property', () => {
@model()
class TestModel {
@property()
date: Date;
}

const jsonSchema = modelToJsonSchema(TestModel);
expect(jsonSchema.properties).to.deepEqual({
date: {
type: 'string',
format: 'date-time',
},
});
expectValidJsonSchema(jsonSchema);
});

it('properly converts object properties', () => {
@model()
class TestModel {
Expand Down
64 changes: 59 additions & 5 deletions packages/repository-json-schema/test/unit/build-schema.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe('build-schema', () => {
expect(isComplexType(Function)).to.eql(false);
});

it('returns false for Date', () => {
expect(isComplexType(Date)).to.eql(false);
});

it('returns false for Array', () => {
expect(isComplexType(Array)).to.eql(false);
});
Expand All @@ -88,24 +92,74 @@ describe('build-schema', () => {
);
});

it('converts types in strings', () => {
expect(metaToJsonProperty({type: 'number'})).to.eql({
type: 'number',
it('converts Boolean', () => {
expect(metaToJsonProperty({type: Boolean})).to.eql({
type: 'boolean',
});
});

it('converts primitives', () => {
it('converts String', () => {
expect(metaToJsonProperty({type: String})).to.eql({
type: 'string',
});
});

it('converts Number', () => {
expect(metaToJsonProperty({type: Number})).to.eql({
type: 'number',
});
});

it('converts arrays', () => {
it('converts Date', () => {
expect(metaToJsonProperty({type: Date})).to.eql({
type: 'string',
format: 'date-time',
});
});

it('converts Object', () => {
expect(metaToJsonProperty({type: Object})).to.eql({
type: 'object',
});
});

it('converts Array', () => {
expect(metaToJsonProperty({type: Array})).to.eql({
type: 'array',
});
});

it('converts "boolean" in strings', () => {
expect(metaToJsonProperty({type: 'boolean'})).to.eql({
type: 'boolean',
});
});

it('converts "string" in strings', () => {
expect(metaToJsonProperty({type: 'string'})).to.eql({
type: 'string',
});
});

it('converts "date" in strings', () => {
expect(metaToJsonProperty({type: 'date'})).to.eql({
type: 'string',
format: 'date-time',
});
});

it('converts "object" in strings', () => {
expect(metaToJsonProperty({type: 'object'})).to.eql({
type: 'object',
});
});

it('converts "array" in strings', () => {
expect(metaToJsonProperty({type: 'array'})).to.eql({
type: 'array',
});
});

it('converts complex types', () => {
class CustomType {}
expect(metaToJsonProperty({type: CustomType})).to.eql({
Expand Down

0 comments on commit d65a17f

Please sign in to comment.