Skip to content

Commit de10fb3

Browse files
committed
feat: 🎸 improve "bin" kind validation
1 parent 883060f commit de10fb3

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/type/classes/BinaryType.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {printTree} from 'tree-dump/lib/printTree';
44
import * as schema from '../../schema';
55
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
66
import {stringifyBinary} from '@jsonjoy.com/json-pack/lib/json-binary';
7-
import {validateTType} from '../../schema/validate';
7+
import {validateMinMax, validateTType} from '../../schema/validate';
88
import type {ValidatorCodegenContext} from '../../codegen/validator/ValidatorCodegenContext';
99
import type {ValidationPath} from '../../codegen/validator/types';
1010
import {ValidationError} from '../../constants';
@@ -23,6 +23,17 @@ import type {json_string} from '@jsonjoy.com/util/lib/json-brand';
2323
import type * as ts from '../../typescript/types';
2424
import type {TypeExportContext} from '../../system/TypeExportContext';
2525

26+
const formats = new Set<schema.BinarySchema['format']>([
27+
'bencode',
28+
'bson',
29+
'cbor',
30+
'ion',
31+
'json',
32+
'msgpack',
33+
'resp3',
34+
'ubjson',
35+
]);
36+
2637
export class BinaryType<T extends Type> extends AbstractType<schema.BinarySchema> {
2738
protected schema: schema.BinarySchema;
2839

@@ -54,7 +65,13 @@ export class BinaryType<T extends Type> extends AbstractType<schema.BinarySchema
5465
}
5566

5667
public validateSchema(): void {
57-
validateTType(this.getSchema(), 'bin');
68+
const schema = this.getSchema();
69+
validateTType(schema, 'bin');
70+
const {min, max, format} = schema;
71+
validateMinMax(min, max);
72+
if (format !== undefined) {
73+
if (!formats.has(format)) throw new Error('FORMAT');
74+
}
5875
this.type.validateSchema();
5976
}
6077

0 commit comments

Comments
 (0)