Skip to content

Commit

Permalink
feat(annotations): changed meaning of 'title' and 'description'
Browse files Browse the repository at this point in the history
The textual descriptions is now entirely in the 'description' field
The title field is a type name descriptor

fix #13
  • Loading branch information
grantila committed Oct 20, 2021
1 parent e040baa commit 0009532
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 53 deletions.
42 changes: 36 additions & 6 deletions lib/__snapshots__/batch-convert.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ Object {
"additionalProperties": false,
"properties": Object {
"bar": Object {
"title": "Foo.bar",
"type": "string",
},
"baz": Object {
"anyOf": Array [
Object {
"title": "Foo.baz",
"type": "number",
},
Object {
"$ref": "#/definitions/Foo",
"title": "Foo.baz",
},
],
"description": "@see baz documentation at http://documentation.yada",
"title": "A baz is a number of another Foo",
"description": "A baz is a number of another Foo
@see baz documentation at http://documentation.yada",
"title": "Foo.baz",
},
},
"required": Array [
"bar",
"baz",
],
"title": "Foo",
"type": "object",
},
"Foo2": Object {
Expand All @@ -36,12 +42,14 @@ Object {
"items": Object {
"type": "boolean",
},
"title": "Foo2.bak",
"type": "array",
},
},
"required": Array [
"bak",
],
"title": "Foo2",
"type": "object",
},
},
Expand All @@ -56,12 +64,14 @@ Object {
"additionalProperties": false,
"properties": Object {
"name": Object {
"title": "SubDirType.name",
"type": "string",
},
},
"required": Array [
"name",
],
"title": "SubDirType",
"type": "object",
},
},
Expand All @@ -76,25 +86,31 @@ Object {
"additionalProperties": false,
"properties": Object {
"bar": Object {
"title": "Foo.bar",
"type": "string",
},
"baz": Object {
"anyOf": Array [
Object {
"title": "Foo.baz",
"type": "number",
},
Object {
"$ref": "#/definitions/Foo",
"title": "Foo.baz",
},
],
"description": "@see baz documentation at http://documentation.yada",
"title": "A baz is a number of another Foo",
"description": "A baz is a number of another Foo
@see baz documentation at http://documentation.yada",
"title": "Foo.baz",
},
},
"required": Array [
"bar",
"baz",
],
"title": "Foo",
"type": "object",
},
"Foo2": Object {
Expand All @@ -104,12 +120,14 @@ Object {
"items": Object {
"type": "boolean",
},
"title": "Foo2.bak",
"type": "array",
},
},
"required": Array [
"bak",
],
"title": "Foo2",
"type": "object",
},
},
Expand All @@ -124,12 +142,14 @@ Object {
"additionalProperties": false,
"properties": Object {
"name": Object {
"title": "SubDirType.name",
"type": "string",
},
},
"required": Array [
"name",
],
"title": "SubDirType",
"type": "object",
},
},
Expand All @@ -156,12 +176,14 @@ Object {
"additionalProperties": false,
"properties": Object {
"name": Object {
"title": "SubDirType.name",
"type": "string",
},
},
"required": Array [
"name",
],
"title": "SubDirType",
"type": "object",
},
},
Expand All @@ -176,25 +198,31 @@ Object {
"additionalProperties": false,
"properties": Object {
"bar": Object {
"title": "Foo.bar",
"type": "string",
},
"baz": Object {
"anyOf": Array [
Object {
"title": "Foo.baz",
"type": "number",
},
Object {
"$ref": "#/definitions/Foo",
"title": "Foo.baz",
},
],
"description": "@see baz documentation at http://documentation.yada",
"title": "A baz is a number of another Foo",
"description": "A baz is a number of another Foo
@see baz documentation at http://documentation.yada",
"title": "Foo.baz",
},
},
"required": Array [
"bar",
"baz",
],
"title": "Foo",
"type": "object",
},
"Foo2": Object {
Expand All @@ -204,12 +232,14 @@ Object {
"items": Object {
"type": "boolean",
},
"title": "Foo2.bak",
"type": "array",
},
},
"required": Array [
"bak",
],
"title": "Foo2",
"type": "object",
},
},
Expand Down
16 changes: 10 additions & 6 deletions lib/__snapshots__/converter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ import { suretype, v, compile, annotate } from 'suretype';
/** The validation schema for a Foo */
export const schemaFoo = suretype({
name: \\"Foo\\",
title: \\"The Foo type\\"
title: \\"Foo\\",
description: \\"The Foo type\\"
}, v.object({
int: annotate({
title: \\"This is the integer thing\\",
description: \\"@default 55\\"
title: \\"Foo.int\\",
description: \\"This is the integer thing\\\\n\\\\n@default 55\\"
}, v.number().integer()),
str: v.string().required(),
str: annotate({
title: \\"Foo.str\\"
}, v.string().required()),
stra: annotate({
title: \\"Excellent array of strings\\"
title: \\"Foo.stra\\",
description: \\"Excellent array of strings\\"
}, v.array(v.string()).required())
}));
Expand Down Expand Up @@ -83,7 +87,7 @@ paths: {}
components:
schemas:
Foo:
title: This is Foo
description: This is Foo
properties:
gt5:
exclusiveMinimum: 5
Expand Down
9 changes: 7 additions & 2 deletions lib/convert-graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,31 @@ describe( "convert-graphql", ( ) =>
types: [
{
name: 'Foo',
title: 'Foo type',
title: 'Foo',
description: 'Foo type',
type: 'object',
loc: expect.anything( ),
properties: {
bar: {
node: {
type: 'string',
title: 'Foo.bar',
loc: expect.anything( ),
},
required: true,
},
num: {
node: {
type: 'number',
title: 'Foo.num',
loc: expect.anything( ),
},
required: false,
},
int: {
node: {
type: 'integer',
title: 'Foo.int',
loc: expect.anything( ),
},
required: false,
Expand Down Expand Up @@ -81,7 +85,8 @@ describe( "convert-graphql", ( ) =>
types: [
{
name: 'Foo',
title: 'Foo type',
title: 'Foo',
description: 'Foo type',
type: 'object',
properties: {
bar: {
Expand Down
2 changes: 1 addition & 1 deletion lib/fixtures/validator.st.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { suretype, v } from 'suretype'

export const myval = suretype(
{ name: 'Foo', title: 'This is Foo' },
{ name: 'Foo', description: 'This is Foo' },
v.object( {
gt5: v.number( ).gt( 5 ),
gte5: v.number( ).gte( 5 ),
Expand Down
2 changes: 1 addition & 1 deletion lib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe( "index", ( ) =>
expect( JSON.parse( data ) ).toStrictEqual( {
$comment: expect.anything( ),
definitions: {
Foo: { type: [ 'number', 'string' ] },
Foo: { type: [ 'number', 'string' ], title: 'Foo' },
},
} );
} );
Expand Down
68 changes: 40 additions & 28 deletions lib/tests/__snapshots__/ts-to-openapi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ts-to-openapi ensure descriptions are forwarded 1`] = `
"openapi: 3.0.0
info:
title: My API
version: v1
x-comment: >-
Generated by core-types-json-schema
(https://github.com/grantila/core-types-json-schema) on behalf of typeconv
(https://github.com/grantila/typeconv)
paths: {}
components:
schemas:
Point:
properties:
x:
title: Point.x
description: The distance from the left in mm
type: number
'y':
title: Point.y
description: The distance from the top in mm
type: number
required:
- x
- 'y'
additionalProperties: false
title: Point
type: object
"
`;

exports[`ts-to-openapi typescript to openapi 1`] = `
"openapi: 3.0.0
info:
Expand All @@ -15,63 +46,44 @@ components:
Foo:
properties:
a:
title: Foo.a
type: string
b:
title: Foo.b
nullable: true
c:
title: Foo.c
type: number
d:
title: Foo.d
type: boolean
e:
$ref: '#/components/schemas/Thing'
title: Foo.e
required:
- a
- b
- c
- d
- e
additionalProperties: false
title: Foo
type: object
Thing:
properties:
x:
title: Thing.x
enum:
- 6
type: number
'y':
title: Thing.y
type: string
required:
- x
- 'y'
additionalProperties: false
type: object
"
`;

exports[`ts-to-openapi typescript to openapi 2`] = `
"openapi: 3.0.0
info:
title: My API
version: v1
x-comment: >-
Generated by core-types-json-schema
(https://github.com/grantila/core-types-json-schema) on behalf of typeconv
(https://github.com/grantila/typeconv)
paths: {}
components:
schemas:
Point:
properties:
x:
title: The distance from the left in mm
type: number
'y':
title: The distance from the top in mm
type: number
required:
- x
- 'y'
additionalProperties: false
title: Thing
type: object
"
`;
Loading

0 comments on commit 0009532

Please sign in to comment.