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

chore: validate schemas to ensure no top-level null #122

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion scripts/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import fs from 'fs'
import path from 'path'
import { mkdirp } from 'mkdirp'
import { execSync } from 'child_process'

import { parseConfig } from './lib/parse-config.js'
import { generateProtoTypes } from './lib/generate-proto-types.js'
import { PROJECT_ROOT } from './lib/utils.js'
import { generateConfig } from './lib/generate-config.js'
import { readJSONSchema } from './lib/read-json-schema.js'
import { validateJsonSchema } from './lib/validate-json-schema.js'
import { generateValidations } from './lib/generate-validations.js'
import { generateJSONSchemaTS } from './lib/generate-jsonschema-ts.js'
import { generateEncodeDecode } from './lib/generate-encode-decode.js'
Expand Down Expand Up @@ -37,6 +37,11 @@ fs.writeFileSync(path.join(GENERATED_DIRNAME, 'config.ts'), configFile)

const jsonSchemas = readJSONSchema(config)

// Check the schemas meet our specific criteria - currently no top-level null
for (const schema of Object.values(jsonSchemas.merged)) {
validateJsonSchema(schema)
}

const validationCode = generateValidations(config, jsonSchemas)
fs.writeFileSync(path.join(GENERATED_DIRNAME, 'validations.ts'), validationCode)

Expand Down
41 changes: 41 additions & 0 deletions scripts/lib/validate-json-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @ts-check
/**
* Validate the JSON schema matches certain app-secific criteria we have. Currently:
*
* - top-level properties cannot be null or nullable
*
* @param {import('json-schema').JSONSchema7} schema
*/
export function validateJsonSchema(schema) {
if (typeof schema.properties !== 'object') {
throw new Error('Schema must be an object')
}
for (const value of Object.values(schema.properties)) {
validateValueNotNullable(value)
}
}

/**
* @param {import('json-schema').JSONSchema7Definition} value
*/
function validateValueNotNullable(value) {
if (typeof value === 'boolean') {
throw new Error('top-level properties cannot be booleans')
}
if (
value.type === 'null' ||
(Array.isArray(value.type) && value.type.includes('null')) ||
value.const === null ||
(Array.isArray(value.enum) && value.enum.includes(null))
) {
throw new Error('top-level properties cannot be of type null')
}
for (const xof of /** @type {const} */ (['oneOf', 'allOf', 'anyOf'])) {
const subValues = value[xof]
if (Array.isArray(subValues)) {
for (const subValue of subValues) {
validateValueNotNullable(subValue)
}
}
}
}
64 changes: 64 additions & 0 deletions test/validate-json-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// @ts-check
import test from 'tape'
import { validateJsonSchema } from '../scripts/lib/validate-json-schema.js'

/** @type {import('json-schema').JSONSchema7Definition[]} */
const fixtures = [
{
type: 'null',
},
{
type: ['string', 'null'],
},
{
const: null,
},
{
enum: [1, 2, null],
},
{
oneOf: [
{
type: 'string',
},
{
type: 'null',
},
],
},
{
anyOf: [
{
type: 'string',
},
{
type: 'null',
},
],
},
{
allOf: [
{
type: 'string',
},
{
type: 'null',
},
],
},
true,
false,
]

test('throws for schemas with null at top level', (t) => {
for (const def of fixtures) {
t.throws(() =>
validateJsonSchema({
type: 'object',
properties: { foo: def },
})
)
}
t.throws(() => validateJsonSchema({ type: 'string' }))
t.end()
})
Loading