Skip to content

Commit 2f27315

Browse files
fix: false schemas no longer crash the library (#151)
* False schemas are now handled Co-authored-by: Maciej Urbańczyk <urbanczyk.maciej.95@gmail.com>
1 parent 15e7039 commit 2f27315

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

src/simplification/Simplifier.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class Simplifier {
3838
return [cachedModel];
3939
}
4040
}
41+
//If it is a false validation schema return no common model
42+
if (schema === false) {
43+
return [];
44+
}
4145
const model = new CommonModel();
4246
model.originalSchema = Schema.toSchema(schema);
4347
model.type = simplifyTypes(schema);

src/simplification/SimplifyTypes.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ type Output = string[] | string | undefined;
1010
export default function simplifyTypes(schema: Schema | boolean, seenSchemas: Map<any, Output> = new Map()): Output {
1111
//If we find absence of data format ensure all types are returned
1212
if (typeof schema === 'boolean') {
13-
if (schema === true) {
14-
return ['object', 'string', 'number', 'array', 'boolean', 'null', 'integer'];
15-
}
16-
throw new Error('False value schemas are not supported');
13+
return ['object', 'string', 'number', 'array', 'boolean', 'null', 'integer'];
1714
}
1815
const types: Output = [];
1916
if (seenSchemas.has(schema)) return seenSchemas.get(schema);

test/simplification/simplify.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import {simplify} from '../../src/simplification/Simplifier';
99
* on a JSON Schema which actually makes sense but are used to test the principles.
1010
*/
1111
describe('Simplification', function() {
12+
13+
test('should return empty models if false schema', function () {
14+
const schema: any = false;
15+
const models = simplify(schema);
16+
expect(models).toHaveLength(0);
17+
});
1218
test('should return as is', function() {
1319
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './simplify/multipleObjects.json'), 'utf8');
1420
const expectedSchemaString = fs.readFileSync(path.resolve(__dirname, './simplify/expected/multipleObjects.json'), 'utf8');

test/simplification/types.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ describe('Simplification of types', function () {
4646
expect(types).toEqual(["object", "string", "number", "array", "boolean", "null",
4747
"integer"]);
4848
});
49-
test('should if false return all types', function () {
50-
const schema: any = false;
51-
expect(() => { simplifyTypes(schema) }).toThrow("False value schemas are not supported");
52-
});
5349
});
5450

5551
describe('from allOf schemas', function () {

0 commit comments

Comments
 (0)