Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
fix: update dependencies
Browse files Browse the repository at this point in the history
Fixes #700
Fixes #699
Fixes #698
Fixes #697
Fixes #696
Fixes #695
Fixes #690
  • Loading branch information
coderbyheart committed Jan 15, 2024
1 parent 9b6eb4d commit f2485c2
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 290 deletions.
6 changes: 3 additions & 3 deletions agnssDeviceRequestsHandler/agnssDeviceRequestsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Static } from '@sinclair/typebox'
import { agnssRequestSchema } from '../agnss/types.js'
import { fromEnv } from '../lib/fromEnv.js'
import { log, logError } from '../lib/log.js'
import { validateWithJSONSchema } from '../lib/validateWithJSONSchema.js'
import { validate } from '../lib/validate.js'

type AGNSS = (
| {
Expand All @@ -29,7 +29,7 @@ type AGNSSContext = Omit<InvocationContext, 'triggerMetadata'> & {
}
}

const validateAgnssRequest = validateWithJSONSchema(agnssRequestSchema)
const validateAgnssRequest = validate(agnssRequestSchema)

const config = () =>
fromEnv({
Expand Down Expand Up @@ -102,7 +102,7 @@ const agnssDeviceRequestsHandler = async (
return
}
deviceRequests.push({
request: valid,
request: valid.value,
deviceId,
})
})
Expand Down
8 changes: 4 additions & 4 deletions agnssResolveRequestFromNrfCloud/agnss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { verify } from '@nordicsemiconductor/nrfcloud-location-services-tests'
import { Static, Type } from '@sinclair/typebox'
import { agnssRequestSchema, AGNSSType } from '../agnss/types.js'
import { ErrorInfo, ErrorType } from '../lib/ErrorInfo.js'
import { validateWithJSONSchema } from '../lib/validateWithJSONSchema.js'
import { validate } from '../lib/validate.js'
import { apiClient } from '../third-party/nrfcloud.com/apiclient.js'

const PositiveInteger = Type.Integer({ minimum: 1, title: 'positive integer' })
Expand All @@ -11,15 +11,15 @@ const apiRequestSchema = Type.Object(
{
eci: PositiveInteger,
tac: PositiveInteger,
requestType: Type.RegEx(/^custom$/),
requestType: Type.RegExp(/^custom$/),
mcc: Type.Integer({ minimum: 100, maximum: 999 }),
mnc: Type.Integer({ minimum: 0, maximum: 99 }),
customTypes: Type.Array(Type.Enum(AGNSSType), { minItems: 1 }),
},
{ additionalProperties: false },
)

const validateInput = validateWithJSONSchema(agnssRequestSchema)
const validateInput = validate(agnssRequestSchema)

export const resolveAgnssRequest =
(
Expand All @@ -37,7 +37,7 @@ export const resolveAgnssRequest =
return valid
}

const { mcc, mnc, cell, area, types } = valid
const { mcc, mnc, cell, area, types } = valid.value

// Split requests, so that request for Ephemerides is a separate one
const otherTypesInRequest = types.filter((t) => t !== AGNSSType.Ephemerides)
Expand Down
19 changes: 19 additions & 0 deletions lib/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Type } from '@sinclair/typebox'
import { validate } from './validate.js'
import { describe, test as it } from 'node:test'
import assert from 'node:assert/strict'

void describe('validate', () => {
void it('Should check input is valid', async () => {
const maybeValid = validate(Type.Number())(42)
if ('value' in maybeValid) {
assert.equal(maybeValid.value, 42)
} else {
throw new Error(`It should be valid!`)
}
})
void it("Should check as 'invalid' values less than 0", () => {
const maybeValid = validate(Type.Number({ minimum: 0 }))(-42)
assert.equal('error' in maybeValid, true)
})
})
32 changes: 32 additions & 0 deletions lib/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { Static, TSchema } from '@sinclair/typebox'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { ErrorInfo, ErrorType } from './ErrorInfo.js'

/**
* Validate the value against the given TypeBox schema
*/
export const validate = <T extends TSchema>(
schema: T,
): ((value: unknown) =>
| { value: Static<typeof schema> }
| {
error: ErrorInfo
}) => {
const C = TypeCompiler.Compile(schema)
return (value: unknown) => {
const firstError = C.Errors(value).First()
if (firstError !== undefined) {
return {
error: {
type: ErrorType.BadRequest,
message: 'Validation failed!',
detail: {
errors: [...C.Errors(value)],
input: value,
},
},
}
}
return { value: value as Static<typeof schema> }
}
}
31 changes: 0 additions & 31 deletions lib/validateWithJSONSchema.ts

This file was deleted.

Loading

0 comments on commit f2485c2

Please sign in to comment.