Deduce type value from validation keywords (fixes #71)

This commit is contained in:
Roman Hotsiy 2016-07-26 14:07:25 +03:00
parent a5959bdf55
commit ae0b0f5f14
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 67 additions and 5 deletions

View File

@ -23,6 +23,7 @@ describe('Redoc components', () => {
builder = tcb;
specMgr = _spec;
}));
beforeEach(() => {
fixture = builder.createSync(TestAppComponent);
let debugEl = getChildDebugElement(fixture.debugElement, 'json-schema');
@ -37,14 +38,14 @@ describe('Redoc components', () => {
});
it('should set isTrivial for non-object/array types', () => {
component.pointer = '';
component.pointer = '#';
(<any>specMgr)._schema = {type: 'string'};
fixture.detectChanges();
component.schema.isTrivial.should.be.true();
});
it('should use < * > notation for prop without type', () => {
component.pointer = '';
component.pointer = '#';
(<any>specMgr)._schema = {type: 'object', properties: {
test: {}
}};

View File

@ -16,7 +16,7 @@ import { Zippy } from '../../shared/components/Zippy/zippy';
detect: true
})
export class JsonSchema extends BaseComponent {
schema: any;
schema: any = {};
activeDescendant:any = {};
hasDescendants: boolean = false;
_hasSubSchemas: boolean = false;
@ -69,6 +69,7 @@ export class JsonSchema extends BaseComponent {
}
init() {
if (!this.pointer) return;
if (this.nestOdd) {
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'nestodd', 'true');
}

View File

@ -85,4 +85,19 @@ describe('Spec Helper', () => {
}
});
});
describe('injectors', () => {
it('should autodetect type if not-specified', () => {
spyOn(console, 'warn').and.stub();
let schema = {
type: undefined,
properties: {}
};
SchemaHelper.runInjectors(schema, schema, '#/');
schema.type.should.be.equal('object');
expect(console.warn).toHaveBeenCalled();
(<jasmine.Spy>console.warn).and.callThrough();
});
});
});

View File

@ -1,7 +1,7 @@
'use strict';
import { JsonPointer } from '../utils/JsonPointer';
import { SpecManager } from '../utils/SpecManager';
import {methods as swaggerMethods} from '../utils/swagger-defs';
import {methods as swaggerMethods, keywordTypes} from '../utils/swagger-defs';
import slugify from 'slugify';
interface PropertyPreprocessOptions {
@ -27,6 +27,17 @@ export interface MenuCategory {
}
const injectors = {
notype: {
check: (propertySchema) => !propertySchema.type,
inject: (injectTo, propertySchema, pointer) => {
injectTo.type = SchemaHelper.detectType(propertySchema);
propertySchema.type = injectTo.type;
let message = `No "type" specified at ${pointer}. `;
message += injectTo.type ? `Automatically detected: "${injectTo.type}"` :
`Can't detect automatically`;
console.warn(message);
}
},
general: {
check: () => true,
inject: (injectTo, propertySchema, pointer) => {
@ -243,6 +254,17 @@ export class SchemaHelper {
(method.description && method.description.substring(0, 50)) || '<no description>';
}
static detectType(schema) {
let keywords = Object.keys(keywordTypes);
for (var i=0; i < keywords.length; i++) {
let keyword = keywords[i];
let type = keywordTypes[keyword];
if (schema[keyword]) {
return type;
}
}
}
static buildMenuTree(schema):Array<MenuCategory> {
let tag2MethodMapping = {};

View File

@ -1,3 +1,26 @@
'use strict';
export var methods = new Set(['get', 'put', 'post', 'delete', 'options', 'head', 'patch']);
export const methods = new Set(['get', 'put', 'post', 'delete', 'options', 'head', 'patch']);
export const keywordTypes = {
multipleOf: 'number',
maximum: 'number',
exclusiveMaximum: 'number',
minimum: 'number',
exclusiveMinimum: 'number',
maxLength: 'string',
minLength: 'string',
pattern: 'string',
items: 'array',
maxItems: 'array',
minItems: 'array',
uniqueItems: 'array',
maxProperties: 'object',
minProperties: 'object',
required: 'object',
additionalProperties: 'object',
properties: 'object'
};