-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for linked wearable mappings (#278)
- Loading branch information
1 parent
e1e2269
commit 30e09f4
Showing
10 changed files
with
510 additions
and
10 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { generateLazyValidator, JSONSchema, ValidateFunction } from '../../validation' | ||
import { KeywordDefinition } from 'ajv' | ||
|
||
/** | ||
* MappingType | ||
* @alpha | ||
*/ | ||
export enum MappingType { | ||
SINGLE = 'single', | ||
ANY = 'any', | ||
MULTIPLE = 'multiple', | ||
RANGE = 'range' | ||
} | ||
|
||
/** | ||
* Mapping | ||
* @alpha | ||
*/ | ||
export type Mapping = SingleMapping | AnyMapping | RangeMapping | MultipleMapping | ||
|
||
/** | ||
* SingleMapping | ||
* @alpha | ||
*/ | ||
export type SingleMapping = { | ||
type: MappingType.SINGLE | ||
id: string | ||
} | ||
|
||
/** | ||
* AnyMapping | ||
* @alpha | ||
*/ | ||
export type AnyMapping = { | ||
type: MappingType.ANY | ||
} | ||
|
||
/** | ||
* RangeMapping | ||
* @alpha | ||
*/ | ||
export type RangeMapping = { | ||
type: MappingType.RANGE | ||
from: string | ||
to: string | ||
} | ||
|
||
/** | ||
* MultipleMapping | ||
* @alpha | ||
*/ | ||
export type MultipleMapping = { | ||
type: MappingType.MULTIPLE | ||
ids: string[] | ||
} | ||
|
||
/** | ||
* SingleMapping | ||
* @alpha | ||
*/ | ||
export namespace SingleMapping { | ||
export const schema: JSONSchema<SingleMapping> = { | ||
type: 'object', | ||
properties: { | ||
type: { type: 'string', const: MappingType.SINGLE }, | ||
id: { type: 'string', pattern: '^[0-9]+$' } | ||
}, | ||
required: ['type', 'id'], | ||
additionalProperties: false | ||
} | ||
|
||
export const validate: ValidateFunction<Mapping> = generateLazyValidator(schema) | ||
} | ||
|
||
/** | ||
* AnyMapping | ||
* @alpha | ||
*/ | ||
export namespace AnyMapping { | ||
export const schema: JSONSchema<AnyMapping> = { | ||
type: 'object', | ||
properties: { | ||
type: { type: 'string', const: MappingType.ANY } | ||
}, | ||
required: ['type'], | ||
additionalProperties: false | ||
} | ||
|
||
export const validate: ValidateFunction<Mapping> = generateLazyValidator(schema) | ||
} | ||
|
||
/** | ||
* RangeMapping | ||
* @alpha | ||
*/ | ||
export namespace RangeMapping { | ||
export const _fromLessThanOrEqualTo: KeywordDefinition = { | ||
keyword: '_fromLessThanOrEqualTo', | ||
validate: function validate(schema: boolean, data: any) { | ||
if (!data || !data.from || !data.to) { | ||
return false | ||
} | ||
|
||
let { to, from } = data | ||
if (typeof from !== 'bigint' || typeof to !== 'bigint') { | ||
from = BigInt(from) | ||
to = BigInt(to) | ||
} | ||
|
||
return from <= to | ||
}, | ||
errors: false | ||
} | ||
|
||
export const schema: JSONSchema<RangeMapping> = { | ||
type: 'object', | ||
properties: { | ||
type: { type: 'string', const: MappingType.RANGE }, | ||
from: { type: 'string', pattern: '^[0-9]+$' }, | ||
to: { type: 'string', pattern: '^[0-9]+$' } | ||
}, | ||
required: ['type', 'from', 'to'], | ||
additionalProperties: false, | ||
_fromLessThanOrEqualTo: true | ||
} | ||
|
||
export const validate: ValidateFunction<Mapping> = generateLazyValidator(schema, [_fromLessThanOrEqualTo]) | ||
} | ||
|
||
/** | ||
* MultipleMapping | ||
* @alpha | ||
*/ | ||
export namespace MultipleMapping { | ||
export const schema: JSONSchema<MultipleMapping> = { | ||
type: 'object', | ||
properties: { | ||
type: { type: 'string', const: MappingType.MULTIPLE }, | ||
ids: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
pattern: '^[0-9]+$' | ||
}, | ||
minItems: 1, | ||
uniqueItems: true | ||
} | ||
}, | ||
required: ['type', 'ids'], | ||
additionalProperties: false | ||
} | ||
export const validate: ValidateFunction<Mapping> = generateLazyValidator(schema) | ||
} | ||
|
||
/** | ||
* Mappings | ||
* @alpha | ||
*/ | ||
export namespace Mapping { | ||
export const schema: JSONSchema<Mapping> = { | ||
type: 'object', | ||
properties: { | ||
type: { | ||
type: 'string', | ||
enum: Object.values(MappingType) | ||
} | ||
}, | ||
required: ['type'], | ||
oneOf: [SingleMapping.schema, AnyMapping.schema, RangeMapping.schema, MultipleMapping.schema] | ||
} | ||
|
||
export const validate: ValidateFunction<Mapping> = generateLazyValidator(schema, [ | ||
RangeMapping._fromLessThanOrEqualTo | ||
]) | ||
} |
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
Oops, something went wrong.