Skip to content

Commit

Permalink
refactor: update SchemaObject
Browse files Browse the repository at this point in the history
BREAKING CHANGE: SchemaObject.format was moved to PrimitiveSchemaObject.format
  • Loading branch information
raveclassic committed Nov 11, 2019
1 parent 21a6b8c commit 0b4cf16
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 104 deletions.
4 changes: 0 additions & 4 deletions src/language/typescript/3.0/serializers/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
getSerializedUnionType,
intercalateSerializedTypes,
SERIALIZED_BOOLEAN_TYPE,
SERIALIZED_NULL_TYPE,
SERIALIZED_NUMERIC_TYPE,
SERIALIZED_STRING_TYPE,
SERIALIZED_UNKNOWN_TYPE,
Expand Down Expand Up @@ -71,9 +70,6 @@ const serializeSchemaObjectWithRecursion = (from: Ref, shouldTrackRecursion: boo
}

switch (schemaObject.type) {
case 'null': {
return right(SERIALIZED_NULL_TYPE);
}
case 'string': {
return right(SERIALIZED_STRING_TYPE);
}
Expand Down
101 changes: 17 additions & 84 deletions src/schema/2.0/schema-object.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
Codec,
dictionary,
JSONPrimitive,
JSONPrimitiveCodec,
stringArrayOption,
stringOption,
} from '../../utils/io-ts';
import { Codec, dictionary, JSONPrimitive, JSONPrimitiveCodec, stringArrayOption } from '../../utils/io-ts';
import { optionFromNullable } from 'io-ts-types/lib/optionFromNullable';
import { ReferenceObject, ReferenceObjectCodec } from './reference-object';
import { intersection, literal, recursion, string, type, union } from 'io-ts';
Expand All @@ -16,11 +9,9 @@ import { nonEmptyArray } from 'io-ts-types/lib/nonEmptyArray';

export interface BaseSchemaObject {
readonly description: Option<string>;
readonly format: Option<string>;
}
export const BaseSchemaObjectCodec: Codec<BaseSchemaObject> = type({
description: optionFromNullable(string),
format: optionFromNullable(string),
});

export interface EnumSchemaObject extends BaseSchemaObject {
Expand All @@ -37,76 +28,26 @@ export const EnumSchemaObjectCodec: Codec<EnumSchemaObject> = intersection(
'EnumSchemaObject',
);

export interface NullSchemaObject extends BaseSchemaObject {
readonly type: 'null';
}

export const NullSchemaObjectCodec: Codec<NullSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: literal('null'),
}),
],
'NullSchemaObject',
);

export interface StringSchemaObject extends BaseSchemaObject {
readonly type: 'string';
}

export const StringSchemaObjectCodec: Codec<StringSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: literal('string'),
}),
],
'StringSchemaObject',
);

export interface NumberSchemaObject extends BaseSchemaObject {
readonly type: 'number';
}

export const NumberSchemaObjectCodec: Codec<NumberSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: literal('number'),
format: stringOption,
}),
],
'NumberSchemaObject',
);

export interface IntegerSchemaObject extends BaseSchemaObject {
readonly type: 'integer';
}

export const IntegerSchemaObjectCodec: Codec<IntegerSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: literal('integer'),
format: stringOption,
}),
],
'IntegerSchemaObject',
);

export interface BooleanSchemaObject extends BaseSchemaObject {
readonly type: 'boolean';
export interface PrimitiveSchemaObject extends BaseSchemaObject {
readonly format: Option<string>;
readonly type: 'null' | 'string' | 'number' | 'integer' | 'boolean';
}

export const BooleanSchemaObjectCodec: Codec<BooleanSchemaObject> = intersection(
export const PrimitiveSchemaObjectCodec: Codec<PrimitiveSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: literal('boolean'),
format: optionFromNullable(string),
type: union([
literal('null'),
literal('string'),
literal('number'),
literal('integer'),
literal('boolean'),
]),
}),
],
'BooleanSchemaObject',
'PrimitiveSchemaObject',
);

export interface AllOfSchemaObject extends BaseSchemaObject {
Expand Down Expand Up @@ -159,26 +100,18 @@ const ObjectSchemaObjectCodec: Codec<ObjectSchemaObject> = recursion('ObjectSche
export type SchemaObject =
| ReferenceObject
| EnumSchemaObject
| NullSchemaObject
| PrimitiveSchemaObject
| AllOfSchemaObject
| ObjectSchemaObject
| StringSchemaObject
| NumberSchemaObject
| IntegerSchemaObject
| BooleanSchemaObject
| ArraySchemaObject;

export const SchemaObjectCodec: Codec<SchemaObject> = recursion('SchemaObject', () =>
union([
ReferenceObjectCodec,
EnumSchemaObjectCodec,
NullSchemaObjectCodec,
PrimitiveSchemaObjectCodec,
AllOfSchemaObject,
ArraySchemaObjectCodec,
ObjectSchemaObjectCodec,
StringSchemaObjectCodec,
NumberSchemaObjectCodec,
IntegerSchemaObjectCodec,
BooleanSchemaObjectCodec,
ArraySchemaObjectCodec,
]),
);
17 changes: 9 additions & 8 deletions src/schema/3.0/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ export const EnumSchemaObjectCodec: Codec<EnumSchemaObject> = intersection(
'EnumSchemaObject',
);

/**
* Primitive type SchemaObject
* `null` is not supported as a primitive type
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types
*/
export interface PrimitiveSchemaObject extends BaseSchemaObject {
readonly type: 'boolean' | 'string' | 'number' | 'integer' | 'null';
readonly format: Option<string>;
readonly type: 'boolean' | 'string' | 'number' | 'integer';
}

const PrimitiveSchemaObjectCodec: Codec<PrimitiveSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
type: union([
literal('boolean'),
literal('string'),
literal('number'),
literal('integer'),
literal('null'),
]),
format: optionFromNullable(string),
type: union([literal('boolean'), literal('string'), literal('number'), literal('integer')]),
}),
],
'PrimitiveSchemaObject',
Expand Down
29 changes: 21 additions & 8 deletions src/schema/asyncapi-2.0.0/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,46 @@ export const OneOfSchemaObjectCodec: Codec<OneOfSchemaObject> = recursion('OneOf
]),
);

export interface NullSchemaObject extends BaseSchemaObject {
export interface BasePrimitiveSchemaObject extends BaseSchemaObject {
readonly format: Option<string>;
}
export const BasePrimitiveSchemaObjectCodec: Codec<BasePrimitiveSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
type({
format: optionFromNullable(string),
}),
],
'BasePrimitiveSchemaObject',
);

export interface NullSchemaObject extends BasePrimitiveSchemaObject {
readonly type: 'null';
}
const NullSchemaObjectCodec: Codec<NullSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
BasePrimitiveSchemaObjectCodec,
type({
type: literal('null'),
}),
],
'NullSchemaObject',
);

export interface BooleanSchemaObject extends BaseSchemaObject {
export interface BooleanSchemaObject extends BasePrimitiveSchemaObject {
readonly type: 'boolean';
}
const BooleanSchemaObjectCodec: Codec<BooleanSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
BasePrimitiveSchemaObjectCodec,
type({
type: literal('boolean'),
}),
],
'BooleanSchemaObject',
);

export interface BaseNumericSchemaObject extends BaseSchemaObject {
export interface BaseNumericSchemaObject extends BasePrimitiveSchemaObject {
readonly multipleOf: Option<Positive>;
readonly maximum: Option<number>;
readonly exclusiveMaximum: Option<number>;
Expand All @@ -115,7 +128,7 @@ export interface BaseNumericSchemaObject extends BaseSchemaObject {
}
const BaseNumericSchemaObjectCodec: Codec<BaseNumericSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
BasePrimitiveSchemaObjectCodec,
type({
multipleOf: optionFromNullable(positive),
maximum: optionFromNullable(number),
Expand Down Expand Up @@ -153,15 +166,15 @@ const IntegerSchemaObjectCodec: Codec<IntegerSchemaObject> = intersection(
'IntegerSchemaObject',
);

export interface StringSchemaObject extends BaseSchemaObject {
export interface StringSchemaObject extends BasePrimitiveSchemaObject {
readonly type: 'string';
readonly maxLength: Option<Natural>;
readonly minLength: Option<Natural>;
readonly pattern: Option<string>;
}
const StringSchemaObjectCodec: Codec<StringSchemaObject> = intersection(
[
BaseSchemaObjectCodec,
BasePrimitiveSchemaObjectCodec,
type({
type: literal('string'),
maxLength: optionFromNullable(natural),
Expand Down

0 comments on commit 0b4cf16

Please sign in to comment.