Skip to content

Commit

Permalink
fix(Package): Export JsonSchema type
Browse files Browse the repository at this point in the history
Closes #240
  • Loading branch information
nokome committed Jan 23, 2021
1 parent 10e9b35 commit e328278
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ts/jsonSchema.ts → ts/JsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { JSONSchema7 } from 'json-schema'
*
* For more details see the guidelines for authoring schemas.
*/
export default interface JsonSchema extends JSONSchema7 {
export interface JsonSchema extends JSONSchema7 {
/**
* The id for the type or property schema to be used
* when generating JSON-LD.
Expand Down
8 changes: 4 additions & 4 deletions ts/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs-extra'
import path from 'path'
import JsonSchema from '../jsonSchema'
import { JsonSchema } from '../JsonSchema'

export const schema = (name: string): Promise<JsonSchema> =>
fs.readJSON(path.join(__dirname, '..', '..', 'public', name)) as Promise<
JsonSchema
>
fs.readJSON(
path.join(__dirname, '..', '..', 'public', name)
) as Promise<JsonSchema>

export const snapshot = (dirname: string, name: string): string =>
path.join(dirname, '__file_snapshots__', name)
14 changes: 7 additions & 7 deletions ts/bindings/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
filterUnionSchemas,
getSchemaProperties,
readSchemas,
Schema,
} from '../helpers'
import { JsonSchema } from '../JsonSchema'

const MAX_LINE_LENGTH = 75 // Desired max length - 4 to allow for indent

Expand Down Expand Up @@ -102,7 +102,7 @@ function formatDocstring(description: string): string {
/**
* Generate a `class`.
*/
export function classGenerator(schema: Schema): string {
export function classGenerator(schema: JsonSchema): string {
const { title, extends: parent, description } = schema
assert(title !== undefined)

Expand Down Expand Up @@ -166,7 +166,7 @@ export function classGenerator(schema: Schema): string {
/**
* Generate a `Union` type.
*/
export function unionGenerator(schema: Schema): string {
export function unionGenerator(schema: JsonSchema): string {
const { title = '', description } = schema
let code = ''
if (description !== undefined) {
Expand All @@ -179,7 +179,7 @@ export function unionGenerator(schema: Schema): string {
/**
* Convert a schema definition to a Python type
*/
function schemaToType(schema: Schema): string {
function schemaToType(schema: JsonSchema): string {
const { type, anyOf, allOf, $ref } = schema

if ($ref !== undefined) return `"${$ref.replace('.schema.json', '')}"`
Expand All @@ -201,7 +201,7 @@ function schemaToType(schema: Schema): string {
/**
* Convert a schema with the `anyOf` property to a Python `Union` type.
*/
function anyOfToType(anyOf: Schema[]): string {
function anyOfToType(anyOf: JsonSchema[]): string {
const types = anyOf
.map((schema) => schemaToType(schema))
.reduce(
Expand All @@ -222,15 +222,15 @@ function anyOfToType(anyOf: Schema[]): string {
* used on a property and the last schema is the final, expected, type of
* the property).
*/
function allOfToType(allOf: Schema[]): string {
function allOfToType(allOf: JsonSchema[]): string {
if (allOf.length === 1) return schemaToType(allOf[0])
else return schemaToType(allOf[allOf.length - 1])
}

/**
* Convert a schema with the `array` property to a Python `Array` type.
*/
function arrayToType(schema: Schema): string {
function arrayToType(schema: JsonSchema): string {
const items = Array.isArray(schema.items)
? anyOfToType(schema.items)
: schema.items !== undefined
Expand Down
14 changes: 7 additions & 7 deletions ts/bindings/r.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
filterUnionSchemas,
getSchemaProperties,
readSchemas,
Schema,
} from '../helpers'
import { JsonSchema } from '../JsonSchema'

/**
* Run `build()` when this file is run as a Node script
Expand Down Expand Up @@ -51,7 +51,7 @@ ${unionsCode}
/**
* Generate a constructor function for a normal type.
*/
export function classGenerator(schema: Schema): string {
export function classGenerator(schema: JsonSchema): string {
const { title = 'Untitled', extends: parent, description = title } = schema
const { all, inherited, own } = getSchemaProperties(schema)

Expand Down Expand Up @@ -100,7 +100,7 @@ export function classGenerator(schema: Schema): string {
/**
* Generate a `Union` type.
*/
export function unionGenerator(schema: Schema): string {
export function unionGenerator(schema: JsonSchema): string {
const { title = '', description = title } = schema
let code = docComment(description, ['@export'])
code += `${title} <- ${schemaToType(schema)}\n\n`
Expand All @@ -125,7 +125,7 @@ function docComment(description: string, tags: string[] = []): string {
/**
* Convert a schema definition to a R class
*/
function schemaToType(schema: Schema): string {
function schemaToType(schema: JsonSchema): string {
const { type, anyOf, allOf, $ref } = schema

if ($ref !== undefined) return `${$ref.replace('.schema.json', '')}`
Expand All @@ -147,7 +147,7 @@ function schemaToType(schema: Schema): string {
/**
* Convert a schema with the `anyOf` property to a `Union` type checker.
*/
function anyOfToType(anyOf: Schema[]): string {
function anyOfToType(anyOf: JsonSchema[]): string {
const types = anyOf
.map((schema) => schemaToType(schema))
.reduce(
Expand All @@ -162,15 +162,15 @@ function anyOfToType(anyOf: Schema[]): string {
/**
* Convert a schema with the `allOf` property to a type.
*/
function allOfToType(allOf: Schema[]): string {
function allOfToType(allOf: JsonSchema[]): string {
if (allOf.length === 1) return schemaToType(allOf[0])
else return schemaToType(allOf[allOf.length - 1])
}

/**
* Convert a schema with the `array` property to an `Array` type checker.
*/
function arrayToType(schema: Schema): string {
function arrayToType(schema: JsonSchema): string {
const items = Array.isArray(schema.items)
? anyOfToType(schema.items)
: schema.items !== undefined
Expand Down
16 changes: 8 additions & 8 deletions ts/bindings/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
filterUnionSchemas,
getSchemaProperties,
readSchemas,
Schema,
} from '../helpers'
import { JsonSchema } from '../JsonSchema'

/**
* Runs Prettier to beautify code contents based on the project settings
Expand Down Expand Up @@ -72,7 +72,7 @@ ${await generateTypeMaps()}
* Generate a `interface Types`, that maps all types
* and can be used to get a type from its name at compile time.
*/
export const typesInterface = (schemas: Schema[]): string => {
export const typesInterface = (schemas: JsonSchema[]): string => {
return `export interface Types {\n${schemas
.map(({ title }) =>
title !== undefined ? ` ${title}: ${titleToType(title)}` : ''
Expand Down Expand Up @@ -101,7 +101,7 @@ export const titleToType = (title: string): string => {
/**
* Generate a `interface` and a factory function for each type.
*/
export const interfaceGenerator = (schema: Schema): string => {
export const interfaceGenerator = (schema: JsonSchema): string => {
const {
title = 'Undefined',
extends: parent,
Expand Down Expand Up @@ -155,7 +155,7 @@ export const interfaceGenerator = (schema: Schema): string => {
/**
* Generate a `Union` type.
*/
export const unionGenerator = (schema: Schema): string => {
export const unionGenerator = (schema: JsonSchema): string => {
const { title = '', description } = schema
return (
docComment(description) +
Expand Down Expand Up @@ -194,7 +194,7 @@ const docComment = (description?: string, tags: string[] = []): string => {
/**
* Convert a JSON Schema definition to a Typescript type
*/
const schemaToType = (schema: Schema): string => {
const schemaToType = (schema: JsonSchema): string => {
const { type, anyOf, allOf, $ref } = schema

if ($ref !== undefined) return $refToType($ref)
Expand Down Expand Up @@ -225,7 +225,7 @@ const $refToType = ($ref: string): string => {
/**
* Convert a JSON Schema with the `anyOf` property to a Typescript `Union` type.
*/
const anyOfToType = (anyOf: Schema[]): string => {
const anyOfToType = (anyOf: JsonSchema[]): string => {
const types = anyOf
.map((schema) => schemaToType(schema))
.reduce(
Expand All @@ -246,7 +246,7 @@ const anyOfToType = (anyOf: Schema[]): string => {
* used on a property and the last schema is the final, expected, type of
* the property).
*/
const allOfToType = (allOf: Schema[]): string => {
const allOfToType = (allOf: JsonSchema[]): string => {
if (allOf.length === 1) return schemaToType(allOf[0])
else return schemaToType(allOf[allOf.length - 1])
}
Expand All @@ -257,7 +257,7 @@ const allOfToType = (allOf: Schema[]): string => {
* Uses the more explicity `Array<>` syntax over the shorter`[]` syntax
* because the latter necessitates the use of, sometime superfluous, parentheses.
*/
const arrayToType = (schema: Schema): string => {
const arrayToType = (schema: JsonSchema): string => {
const items = Array.isArray(schema.items)
? anyOfToType(schema.items)
: schema.items !== undefined
Expand Down
2 changes: 1 addition & 1 deletion ts/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import flatten from 'lodash.flatten'
import path from 'path'
import { readSchemas } from './helpers'
import log from './log'
import JsonSchema from './jsonSchema'
import { JsonSchema } from './JsonSchema'
import {
Article,
article,
Expand Down
3 changes: 1 addition & 2 deletions ts/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import fs from 'fs-extra'
import globby from 'globby'
import path from 'path'
import toposort from 'toposort'
import JsonSchema from './jsonSchema'
export { default as Schema } from './jsonSchema'
import { JsonSchema } from './JsonSchema'

/**
* Read the schemas from `public/*.schema.json` and dereference
Expand Down
2 changes: 1 addition & 1 deletion ts/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './jsonSchema'
export * from './JsonSchema'
export * from './types'
export * from './util/index.browser'
2 changes: 1 addition & 1 deletion ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './jsonSchema'
export * from './JsonSchema'
export * from './types'
export * from './util'
2 changes: 1 addition & 1 deletion ts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import yaml from 'js-yaml'
import cloneDeep from 'lodash.clonedeep'
import path from 'path'
import log from './log'
import JsonSchema from './jsonSchema'
import { JsonSchema } from './JsonSchema'
import { versionMajor } from './util/version'

const SCHEMA_SOURCE_DIR = path.join(__dirname, '..', 'schema')
Expand Down
2 changes: 1 addition & 1 deletion ts/util/jsonSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import fs from 'fs'
import path from 'path'
import JsonSchema from '../jsonSchema'
import { JsonSchema } from '../JsonSchema'

// Lazily loaded set of JSON Schemas
let SCHEMAS: Record<string, JsonSchema> = {}
Expand Down

0 comments on commit e328278

Please sign in to comment.