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

test: test validating a schema name that could "trick" JavaScript #250

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions test/fixtures/bad-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ export const badDocs = [
deleted: false,
},
},
{
text: 'test schema name that could "trick" JavaScript',
/** @type Omit<import('../../dist/index.js').Observation, 'schemaName'> & { schemaName: 'hasOwnProperty' }} */
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
originalVersionId: cachedValues.versionId,
schemaName: 'hasOwnProperty',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
attachments: [],
tags: {},
metadata: {},
deleted: false,
},
},
{
text: 'missing expected originalVersionId',
/** @type Omit<import('../../dist/index.js').Observation, 'originalVersionId'> */
Expand Down
34 changes: 28 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
badDocs,
} from './fixtures/index.js'

/** @import { SchemaName } from '../dist/types.js' */

const schemaNames = /** @type {SchemaName[]} */ (Object.keys(dataTypeIds))

test('Bad docs throw when encoding', () => {
for (const { text, doc } of badDocs) {
assert.throws(() => {
Expand All @@ -29,19 +33,29 @@ test('Bad docs throw when encoding', () => {
}
})

test(`Bad docs won't validate`, () => {
test('Bad docs throw when validating if bad schema name', () => {
for (const { text, doc } of badDocs) {
const { schemaName } = doc
if (isSchemaName(schemaName)) continue

assert.throws(() => {
// @ts-expect-error
validate(doc)
validate(/** @type {any} */ (schemaName), doc)
}, text)
}
})

test('validate bad docs', () => {
for (const schemaName of Object.keys(currentSchemaVersions)) {
test(`Bad docs won't validate`, () => {
for (const { text, doc } of badDocs) {
const { schemaName } = doc
if (!isSchemaName(schemaName)) continue

assert(!validate(schemaName, doc), text)
}
})

test('validate empty docs', () => {
for (const schemaName of schemaNames) {
assert(
// @ts-ignore
!validate(schemaName, {}),
`${schemaName} with missing properties should not validate`
)
Expand Down Expand Up @@ -225,6 +239,14 @@ test(`test encoding of wrongly formatted header`, async () => {
})
})

/**
* @param {unknown} value
* @returns {value is SchemaName}
*/
function isSchemaName(value) {
return schemaNames.includes(/** @type {any} */ (value))
}

/**
* Remove undefined properties (deeply) from an object, by round-tripping to
* JSON. Also handles Buffers via JSON.parse reviver
Expand Down
Loading