-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19560d4
commit f6071ae
Showing
5 changed files
with
104 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
import { validateWithTypeBox } from '@hello.nrfcloud.com/proto' | ||
import { | ||
validate, | ||
validators, | ||
type LwM2MObjectInstance, | ||
} from '@hello.nrfcloud.com/proto-map/lwm2m' | ||
DeviceIdentity, | ||
Shadow, | ||
SingleCellGeoLocation, | ||
LwM2MObjectUpdate, | ||
} from '@hello.nrfcloud.com/proto/hello' | ||
import { Type, type Static } from '@sinclair/typebox' | ||
import type { ValueError } from '@sinclair/typebox/errors' | ||
|
||
const validateInstance = validate(validators) | ||
const IncomingMessage = Type.Union([ | ||
SingleCellGeoLocation, | ||
DeviceIdentity, | ||
Shadow, | ||
LwM2MObjectUpdate, | ||
]) | ||
type IncomingMessageType = Static<typeof IncomingMessage> | ||
export const incomingMessageValidator = validateWithTypeBox(IncomingMessage) | ||
|
||
export const validPassthrough = ( | ||
v: unknown, | ||
onDropped?: (v: unknown, error: Error) => unknown, | ||
): LwM2MObjectInstance | null => { | ||
const maybeValidInstance = validateInstance(v) | ||
if ('error' in maybeValidInstance) { | ||
onDropped?.(v, maybeValidInstance.error) | ||
onDropped?: (v: unknown, errors: Array<ValueError>) => unknown, | ||
): IncomingMessageType | null => { | ||
const maybeValidMessage = incomingMessageValidator(v) | ||
if ('errors' in maybeValidMessage) { | ||
onDropped?.(v, maybeValidMessage.errors) | ||
return null | ||
} | ||
return maybeValidInstance.object | ||
return maybeValidMessage.value | ||
} |