Skip to content

Commit

Permalink
fix: render nullable fields (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Feb 28, 2022
1 parent a685f74 commit 71b0ded
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
8 changes: 8 additions & 0 deletions helpers/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ export class SchemaHelpers {
}

static jsonFieldToSchema(value) {
if (value === undefined || value === null) {
return {
type: 'string',
const: value === undefined ? '' : 'NULL',
[this.extRawValue]: true,
[this.extRenderType]: false,
};
}
if (typeof value !== 'object') {
const str =
typeof value.toString === 'function' ? value.toString() : value;
Expand Down
159 changes: 159 additions & 0 deletions test/helpers/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,165 @@ describe('SchemaHelpers', () => {
});
});

describe('.jsonToSchema', () => {
test('should transform string to schema', () => {
const json = 'foobar';
const schema = new Schema({
type: 'string',
const: 'foobar',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should transform number to schema', () => {
const json = 2137;
const schema = new Schema({
type: 'string',
const: '2137',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should transform boolean to schema', () => {
const json = true;
const schema = new Schema({
type: 'string',
const: 'true',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should transform array to schema', () => {
const json = ['bar', 2137, true];
const schema = new Schema({
type: 'array',
items: [
{
type: 'string',
const: 'bar',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
{
type: 'string',
const: '2137',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
{
type: 'string',
const: 'true',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
],
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should transform object to schema', () => {
const json = {
bar: 'foo',
};
const schema = new Schema({
type: 'object',
properties: {
bar: {
type: 'string',
const: 'foo',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
},
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should transform complex data to schema', () => {
const json = {
foo: ['bar', 2137, true],
bar: 'foo',
};
const schema = new Schema({
type: 'object',
properties: {
foo: {
type: 'array',
items: [
{
type: 'string',
const: 'bar',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
{
type: 'string',
const: '2137',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
{
type: 'string',
const: 'true',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
],
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
},
bar: {
type: 'string',
const: 'foo',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
},
},
'x-schema-private-render-additional-info': false,
'x-schema-private-render-type': false,
});
const result = SchemaHelpers.jsonToSchema(json);
expect(result).toEqual(schema);
});

test('should return empty string when data is null', () => {
const result = SchemaHelpers.jsonToSchema(null);
const schema = new Schema({
type: 'string',
const: 'NULL',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
});
expect(result).toEqual(schema);
});

test('should return empty string when data is undefined', () => {
const result = SchemaHelpers.jsonToSchema(undefined);
const schema = new Schema({
type: 'string',
const: '',
'x-schema-private-raw-value': true,
'x-schema-private-render-type': false,
});
expect(result).toEqual(schema);
});
});

describe('.getCustomExtensions', () => {
test('should return extensions', () => {
const schema = new Schema({
Expand Down

0 comments on commit 71b0ded

Please sign in to comment.