Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug union type including object type. #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 2 additions & 18 deletions src/FlowSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,8 @@ import type {

type FlowType = 'Object' | 'Array' | 'string' | 'number' | 'boolean' | 'null' | 'any' | 'void';

const hasProps = (schema: Schema, props: Array<string>): boolean =>
_.reduce(props, (result: boolean, prop: string) => result || _.has(schema, prop), false);

const isObject = (schema: Schema): boolean => hasProps(schema, [
'properties',
'additionalProperties',
'patternProperties',
'maxProperties',
'minProperties',
]);

const isArray = (schema: Schema): boolean => hasProps(schema, [
'items',
'additionalItems',
'maxItems',
'minItems',
'uniqueItems',
]);
const isObject = (schema: Schema): boolean => schema.type === 'object';
const isArray = (schema: Schema): boolean => schema.type === 'array';

export class FlowSchema {
$id: string;
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/FlowSchema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ test('should convert exact object', t => {
t.deepEqual(
convertSchema({
id: 'Exact',
type: 'object',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the specification of JSON Schema, the type seems not to be required.
However, if the type is not given, it is not expected behavior to convert to object.

properties: {
number: {
type: 'number',
Expand Down Expand Up @@ -197,6 +198,24 @@ test('should convert multi type', (t) => {
);
});

test('should convert multi type contains object', (t) => {
t.deepEqual(
convertSchema({
type: ['object', 'null'],
properties: {
foo: {
type: 'string',
},
},
}),
flow()
.union([
flow('Object').props({ foo: flow('string') }),
flow('null'),
]),
);
});

test('should convert simple types', (t) => {
t.deepEqual(
convertSchema({
Expand Down