Skip to content

Commit

Permalink
fix(Schema): Inherit propertyAliases
Browse files Browse the repository at this point in the history
Closes #126
  • Loading branch information
nokome committed Aug 5, 2019
1 parent 3d3d81c commit a29f215
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tests/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import betterAjvErrors from 'better-ajv-errors'
import fs from 'fs-extra'
import globby from 'globby'
import path from 'path'
import Schema from '../ts/bindings/schema.d'

const readSchema = async (type: string): Promise<Schema> => {
return await fs.readJSON(
path.join(__dirname, '..', 'built', type + '.schema.json')
)
}

/**
* Check that the `built/*.schema.json` files, generated from `schema/*.schema.yaml` files,
Expand All @@ -14,7 +21,7 @@ test('schemas are valid', async () => {
const validate = ajv.compile(metaSchema)

const files = await globby(
path.join(__dirname, '..', 'built', '*.json.schema')
path.join(__dirname, '..', 'built', '*.schema.json')
)
for (const file of files) {
const schema = await fs.readJSON(file)
Expand All @@ -28,3 +35,27 @@ test('schemas are valid', async () => {
}
}
})

test('inheritance', async () => {
const thing = await readSchema('Thing')
const person = await readSchema('Person')

// All `Thing` properties are in `Person` properties
expect(
Object.keys(thing.properties || {}).some(
name => !Object.keys(person.properties || {}).includes(name)
)
).toBe(false)

// All `Thing` required properties in `Person` required properties
expect(
(thing.required || []).some(name => !(person.required || []).includes(name))
).toBe(false)

// All `Thing` property aliases in `Person` property aliases
expect(
Object.keys(thing.propertyAliases || {}).some(
name => !Object.keys(person.propertyAliases || {}).includes(name)
)
).toBe(false)
})
8 changes: 8 additions & 0 deletions ts/bindings/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ const processSchema = (schemas: Map<string, Schema>, schema: Schema): void => {
try {
const parent = parentSchema(schemas, schema)
let parentProperties: { [key: string]: Schema } = {}
let parentPropertyAliases: { [key: string]: string } = {}
let parentRequired: string[] = []
if (parent !== null) {
// Ensure that the parent schema has been processed (to collect properties)
processSchema(schemas, parent)
if (parent.properties !== undefined) parentProperties = parent.properties
if (parent.propertyAliases !== undefined) parentPropertyAliases = parent.propertyAliases
if (parent.required !== undefined) parentRequired = parent.required
}

Expand Down Expand Up @@ -241,6 +243,12 @@ const processSchema = (schemas: Map<string, Schema>, schema: Schema): void => {
...(schema.required !== undefined ? schema.required : [])
]

// Extend `propertyAliases`
schema.propertyAliases = {
...parentPropertyAliases,
...(schema.propertyAliases !== undefined ? schema.propertyAliases : {})
}

// Initialize the `type` property
if (schema.properties.type !== undefined) {
schema.properties.type = {
Expand Down

0 comments on commit a29f215

Please sign in to comment.