mirror of
https://github.com/Redocly/redoc.git
synced 2025-06-05 21:43:06 +03:00
Deduce type value from validation keywords (fixes #71)
This commit is contained in:
parent
a5959bdf55
commit
ae0b0f5f14
|
@ -23,6 +23,7 @@ describe('Redoc components', () => {
|
||||||
builder = tcb;
|
builder = tcb;
|
||||||
specMgr = _spec;
|
specMgr = _spec;
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fixture = builder.createSync(TestAppComponent);
|
fixture = builder.createSync(TestAppComponent);
|
||||||
let debugEl = getChildDebugElement(fixture.debugElement, 'json-schema');
|
let debugEl = getChildDebugElement(fixture.debugElement, 'json-schema');
|
||||||
|
@ -37,14 +38,14 @@ describe('Redoc components', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set isTrivial for non-object/array types', () => {
|
it('should set isTrivial for non-object/array types', () => {
|
||||||
component.pointer = '';
|
component.pointer = '#';
|
||||||
(<any>specMgr)._schema = {type: 'string'};
|
(<any>specMgr)._schema = {type: 'string'};
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
component.schema.isTrivial.should.be.true();
|
component.schema.isTrivial.should.be.true();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use < * > notation for prop without type', () => {
|
it('should use < * > notation for prop without type', () => {
|
||||||
component.pointer = '';
|
component.pointer = '#';
|
||||||
(<any>specMgr)._schema = {type: 'object', properties: {
|
(<any>specMgr)._schema = {type: 'object', properties: {
|
||||||
test: {}
|
test: {}
|
||||||
}};
|
}};
|
||||||
|
|
|
@ -16,7 +16,7 @@ import { Zippy } from '../../shared/components/Zippy/zippy';
|
||||||
detect: true
|
detect: true
|
||||||
})
|
})
|
||||||
export class JsonSchema extends BaseComponent {
|
export class JsonSchema extends BaseComponent {
|
||||||
schema: any;
|
schema: any = {};
|
||||||
activeDescendant:any = {};
|
activeDescendant:any = {};
|
||||||
hasDescendants: boolean = false;
|
hasDescendants: boolean = false;
|
||||||
_hasSubSchemas: boolean = false;
|
_hasSubSchemas: boolean = false;
|
||||||
|
@ -69,6 +69,7 @@ export class JsonSchema extends BaseComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
if (!this.pointer) return;
|
||||||
if (this.nestOdd) {
|
if (this.nestOdd) {
|
||||||
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'nestodd', 'true');
|
this._renderer.setElementAttribute(this._elementRef.nativeElement, 'nestodd', 'true');
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
import { JsonPointer } from '../utils/JsonPointer';
|
import { JsonPointer } from '../utils/JsonPointer';
|
||||||
import { SpecManager } from '../utils/SpecManager';
|
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';
|
import slugify from 'slugify';
|
||||||
|
|
||||||
interface PropertyPreprocessOptions {
|
interface PropertyPreprocessOptions {
|
||||||
|
@ -27,6 +27,17 @@ export interface MenuCategory {
|
||||||
}
|
}
|
||||||
|
|
||||||
const injectors = {
|
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: {
|
general: {
|
||||||
check: () => true,
|
check: () => true,
|
||||||
inject: (injectTo, propertySchema, pointer) => {
|
inject: (injectTo, propertySchema, pointer) => {
|
||||||
|
@ -243,6 +254,17 @@ export class SchemaHelper {
|
||||||
(method.description && method.description.substring(0, 50)) || '<no description>';
|
(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> {
|
static buildMenuTree(schema):Array<MenuCategory> {
|
||||||
let tag2MethodMapping = {};
|
let tag2MethodMapping = {};
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,26 @@
|
||||||
'use strict';
|
'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'
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user