Skip to content

Commit

Permalink
fix: fix bitset serialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 21, 2022
1 parent 3c6b1b9 commit 74cc9a7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "schemastery",
"description": "Type driven schema validator",
"version": "3.4.2",
"version": "3.4.3",
"main": "lib/node.js",
"module": "lib/browser.js",
"typings": "lib/index.d.ts",
Expand Down
25 changes: 18 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace Schema {
sKey?: Schema
inner?: Schema
list?: Schema[]
dict?: Dict<any>
dict?: Dict<Schema>
bits?: Dict<number>
callback?: Function
value?: T
meta?: Meta<T>
Expand Down Expand Up @@ -85,7 +86,7 @@ namespace Schema {
natural(): Schema<number>
percent(): Schema<number>
boolean(): Schema<boolean>
bitset<K extends string>(dict: Dict<number, K>): Schema<number | readonly K[], number>
bitset<K extends string>(bits: Dict<number, K>): Schema<number | readonly K[], number>
function(): Schema<Function, (...args: any[]) => any>
is<T>(constructor: Constructor<T>): Schema<T>
array<X>(inner: X): Schema<TypeS<X>[], TypeT<X>[]>
Expand Down Expand Up @@ -268,14 +269,14 @@ Schema.extend('boolean', (data) => {
throw new TypeError(`expected boolean but got ${data}`)
})

Schema.extend('bitset', (data, { dict }) => {
Schema.extend('bitset', (data, { bits }) => {
if (typeof data === 'number') return [data]
if (!Array.isArray(data)) throw new TypeError(`expected array but got ${data}`)
let result = 0
for (const value of data) {
if (typeof value !== 'string') throw new TypeError(`expected string but got ${value}`)
if (!(value in dict)) throw new TypeError(`unknown value ${value}`)
result |= dict[value]
if (!(value in bits)) throw new TypeError(`unknown value ${value}`)
result |= bits[value]
}
return [result, result]
})
Expand Down Expand Up @@ -399,13 +400,23 @@ function defineMethod(name: string, keys: (keyof Schema.Base)[], format: Formatt
case 'inner': schema.inner = Schema.from(args[index]); break
case 'list': schema.list = args[index].map(Schema.from); break
case 'dict': schema.dict = valueMap(args[index], Schema.from); break
case 'bits': {
schema.bits = {}
for (const key in args[index]) {
if (typeof args[index][key] !== 'number') continue
schema.bits[key] = args[index][key]
}
break
}
default: schema[key] = args[index]
}
})
if (name === 'object' || name === 'dict') {
schema.meta.default = {}
} else if (name === 'array' || name === 'tuple' || name === 'bitset') {
} else if (name === 'array' || name === 'tuple') {
schema.meta.default = []
} else if (name === 'bitset') {
schema.meta.default = 0
}
return schema
},
Expand All @@ -419,7 +430,7 @@ defineMethod('const', ['value'], ({ value }) => typeof value === 'string' ? JSON
defineMethod('string', [], () => 'string')
defineMethod('number', [], () => 'number')
defineMethod('boolean', [], () => 'boolean')
defineMethod('bitset', ['dict'], () => 'bitset')
defineMethod('bitset', ['bits'], () => 'bitset')
defineMethod('function', [], () => 'function')
defineMethod('array', ['inner'], ({ inner }) => `${inner.toString(true)}[]`)
defineMethod('dict', ['inner', 'sKey'], ({ inner, sKey }) => `{ [key: ${sKey.toString()}]: ${inner.toString()} }`)
Expand Down

0 comments on commit 74cc9a7

Please sign in to comment.