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

fix: validate shouldn't throw #244

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 1 addition & 4 deletions scripts/lib/generate-jsonschema-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import { default as _ } from '@json-schema-tools/dereferencer'

// Dereferencer's exports are all wrong for ESM imports
/** @type {any} */
const JsonSchemaDereferencer =
// @ts-ignore
_.default
const JsonSchemaDereferencer = /** @type {any} */ (_).default

/** @typedef {import('../../src/types.js').SchemaName} SchemaName */
/** @typedef {import('json-schema').JSONSchema7} JSONSchema */
Expand Down
17 changes: 12 additions & 5 deletions scripts/lib/generate-jsonschema-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ export async function generateJSONSchemaTS(config, jsonSchemas) {
/** @type {Record<string, string>} */
const typescriptDefs = {}
for (const [schemaName, jsonSchema] of Object.entries(jsonSchemas.values)) {
// @ts-ignore
const ts = await compile(jsonSchema, capitalize(schemaName), {
additionalProperties: false,
unknownAny: false,
})
const ts = await compile(
/**
* This argument is a v7 JSON Schema but the function expects a v4 schema.
* It should be fine, so we cast it to `any`.
* @type {any}
*/ (jsonSchema),
capitalize(schemaName),
{
additionalProperties: false,
unknownAny: false,
}
)
typescriptDefs[schemaName] = ts
}

Expand Down
1 change: 0 additions & 1 deletion src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
convertTranslation,
convertTrack,
} from './lib/decode-conversions.js'
// @ts-ignore
import * as cenc from 'compact-encoding'
import { DATA_TYPE_ID_BYTES, SCHEMA_VERSION_BYTES } from './constants.js'
import {
Expand Down
1 change: 0 additions & 1 deletion src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
type ValidSchemaDef,
} from './types.js'
import { currentSchemaVersions, dataTypeIds } from './config.js'
// @ts-ignore
import * as cenc from 'compact-encoding'
import { DATA_TYPE_ID_BYTES } from './constants.js'
import { Encode } from './proto/index.js'
Expand Down
1 change: 1 addition & 0 deletions src/typedefs/compact-encoding.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'compact-encoding'
11 changes: 10 additions & 1 deletion src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MapeoValue, FilterBySchemaName, SchemaName } from './types.js'
import * as validations from './validations.js'
import { getOwn } from './lib/utils.js'
import {
type ValidateFunction as AjvValidateFunction,
type DefinedError,
Expand All @@ -19,7 +20,15 @@ const validate: ValidateFunction = <
schemaName: TSchemaName,
obj: unknown
): obj is FilterBySchemaName<MapeoValue, TSchemaName> => {
const validateSchema = validations[schemaName] as AjvValidateFunction
validate.errors = null

const validateSchema = getOwn(validations, schemaName) as
| undefined
| AjvValidateFunction
if (!validateSchema) {
return false
}

const result = validateSchema(obj)
validate.errors = validateSchema.errors as DefinedError[]
return result
Expand Down
3 changes: 1 addition & 2 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ export const goodDocsCompleted = [
updatedAt: cachedValues.updatedAt,
links: [],
name: 'my device name',
// @ts-expect-error
deviceType: 'motorbike',
deviceType: /** @type {any} */ ('motorbike'),
deleted: true,
},
expected: {
Expand Down
26 changes: 12 additions & 14 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ import {
test('Bad docs throw when encoding', () => {
for (const { text, doc } of badDocs) {
assert.throws(() => {
// @ts-expect-error
encode(doc)
encode(/** @type {any} */ (doc))
}, text)
}
})

test(`Bad docs won't validate`, () => {
for (const { text, doc } of badDocs) {
assert.throws(() => {
// @ts-expect-error
validate(doc)
}, text)
assert(!validate(/** @type {any} */ (doc.schemaName), doc), text)
}
})

test('validate bad docs', () => {
for (const schemaName of Object.keys(currentSchemaVersions)) {
assert(
// @ts-ignore
!validate(schemaName, {}),
!validate(
/** @type {keyof (typeof currentSchemaVersions)} */ (schemaName),
{}
),
`${schemaName} with missing properties should not validate`
)
assert(
Expand All @@ -62,7 +60,6 @@ test('validate good docs', () => {
// skip docs with UNRECOGNIZED values - these are used for testing encoding/decoding and will not validate (the decoded versions should validate)
if (Object.values(expected).includes('UNRECOGNIZED')) continue
assert(
// @ts-ignore
validate(doc.schemaName, valueOf(doc)),
`${doc.schemaName} with all required properties should validate`
)
Expand Down Expand Up @@ -98,11 +95,12 @@ test(`testing encoding of doc with additional optional values,
test(`testing encoding of doc with additional extra values,
then decoding and comparing the two objects - extra values shouldn't be present`, async () => {
for (const { doc, expected } of goodDocsCompleted) {
const buf = encode({
...doc,
// @ts-expect-error
extraFieldNotInSchema: 'whatever',
})
const buf = encode(
/** @type {any} */ ({
...doc,
extraFieldNotInSchema: 'whatever',
})
)
const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId)))
assert.deepEqual(
decodedDoc,
Expand Down
Loading