From 7905f48684ea5b16a8b6217ac9526a5cb81cf29d Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Wed, 4 Sep 2024 14:32:49 +0000 Subject: [PATCH 1/3] test: properly test more `validate` errors Previously, we intended to test the validation of bad docs but weren't doing so correctly. This fixes that. --- test/index.test.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 0ea13aa..81689a4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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(() => { @@ -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` ) @@ -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 From 51bca962ba3ca8e2e921b2381cf9b56ed85d97f8 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Wed, 4 Sep 2024 14:41:53 +0000 Subject: [PATCH 2/3] test: test validating a schema name that could "trick" JavaScript I was worried that bad schema names like `hasOwnProperty` or `constructor` could cause trouble. They don't (as far as I can tell) but I still think it's worth adding a fixture for it. --- test/fixtures/bad-docs.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/fixtures/bad-docs.js b/test/fixtures/bad-docs.js index 4e37aaf..a106ca8 100644 --- a/test/fixtures/bad-docs.js +++ b/test/fixtures/bad-docs.js @@ -24,6 +24,23 @@ export const badDocs = [ deleted: false, }, }, + { + text: 'test schema name that could "trick" JavaScript', + /** @type Omit & { 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 */ From ea13801d3e7ebb46680f534e8f20cbbaa83993db Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Wed, 4 Sep 2024 15:28:10 +0000 Subject: [PATCH 3/3] Fix merge issue --- test/index.test.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 6eb20e3..b28f4f6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -25,10 +25,6 @@ import { cachedValues } from './fixtures/cached.js' const schemaNames = /** @type {SchemaName[]} */ (Object.keys(dataTypeIds)) -/** @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(() => { @@ -321,14 +317,6 @@ function isSchemaName(value) { return schemaNames.includes(/** @type {any} */ (value)) } -/** - * @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