Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edb6918

Browse files
rewrite const as single element enum
1 parent 17e4ffc commit edb6918

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function stripIllegalKeywords(schema) {
2929
function convertSchema(schema, path, parent, parentPath) {
3030
schema = stripIllegalKeywords(schema);
3131
schema = convertTypes(schema);
32+
schema = rewriteConst(schema);
3233
schema = convertDependencies(schema);
3334

3435
if (typeof schema['patternProperties'] === 'object') {
@@ -137,7 +138,17 @@ function convertTypes(schema) {
137138
function convertPatternProperties(schema) {
138139
schema['x-patternProperties'] = schema['patternProperties'];
139140
delete schema['patternProperties'];
141+
if (typeof schema.additionalProperties === 'undefined') schema.additionalProperties = true;
142+
return schema;
143+
}
144+
145+
function rewriteConst(schema) {
146+
if (schema.const) {
147+
schema.enum = [ schema.const ];
148+
delete schema.const;
149+
}
140150
return schema;
141151
}
142152

143153
module.exports = convert;
154+

‎test/const.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const convert = require('../');
4+
const should = require('should');
5+
6+
it('const', () => {
7+
const schema = {
8+
$schema: 'http://json-schema.org/draft-04/schema#',
9+
type: 'string',
10+
const: 'hello'
11+
};
12+
13+
const result = convert(schema);
14+
15+
const expected = {
16+
type: 'string',
17+
enum: [ 'hello' ]
18+
};
19+
20+
should(result).deepEqual(expected, 'converted');
21+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /