Skip to content

Commit 3bbe3db

Browse files
committed
Generate the type name const enum Schema when serializing the schema.
1 parent ca92c9f commit 3bbe3db

File tree

9 files changed

+129
-5
lines changed

9 files changed

+129
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## v0.6.3
44

55
* Add `assertType<T>(data, ty, ctx?)` assertion type guard function.
6+
* Generate the type name const enum `Schema` when serializing the schema.
67
* Edit README.
78

89

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,14 @@ try {
302302
...
303303
import { deserializeFromObject } from 'tynder/modules/lib/serializer';
304304
import { Foo, A } from './path/to/schema-types/my-schema'; // type definitions (.d.ts)
305-
import mySchema_ from './path/to/schema-compiled/my-schema'; // pre-compiled schema (.ts)
305+
import mySchema_,
306+
{ Schema as MySchema } from './path/to/schema-compiled/my-schema'; // pre-compiled schema (.ts)
307+
// `MySchema` is auto generated string const enum.
306308
307309
const mySchema = deserializeFromObject(mySchema_);
308310
309311
const unknownInput: unknown = {a: 'x'};
310-
const validated = validate<A>(unknownInput, getType(mySchema, 'A'));
312+
const validated = validate<A>(unknownInput, getType(mySchema, MySchema.A));
311313
312314
if (validated) {
313315
const validatedInput = validated.value; // validatedInput is type-safe

examples/schema/_compiled/fields.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,4 +1759,19 @@ const schema = {
17591759
}
17601760
};
17611761
export default schema;
1762+
1763+
export const enum Schema {
1764+
NumberType = 'NumberType',
1765+
A = 'A',
1766+
B = 'B',
1767+
C = 'C',
1768+
D = 'D',
1769+
E = 'E',
1770+
ACL = 'ACL',
1771+
ErrorTypes = 'ErrorTypes',
1772+
Foo = 'Foo',
1773+
Bar = 'Bar',
1774+
Baz = 'Baz',
1775+
User = 'User',
1776+
}
17621777
// tslint:enable: object-literal-key-quotes

examples/schema/_compiled/filesys.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,12 @@ const schema = {
213213
}
214214
};
215215
export default schema;
216+
217+
export const enum Schema {
218+
ACL = 'ACL',
219+
EntryBase = 'EntryBase',
220+
File = 'File',
221+
Folder = 'Folder',
222+
Entry = 'Entry',
223+
}
216224
// tslint:enable: object-literal-key-quotes

examples/schema/_compiled/primitives.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,22 @@ const schema = {
9696
}
9797
};
9898
export default schema;
99+
100+
export const enum Schema {
101+
NumberType = 'NumberType',
102+
IntegerType = 'IntegerType',
103+
BigIntType = 'BigIntType',
104+
StringType = 'StringType',
105+
BooleanType = 'BooleanType',
106+
NullType = 'NullType',
107+
UndefinedType = 'UndefinedType',
108+
AnyType = 'AnyType',
109+
UnknownType = 'UnknownType',
110+
NeverType = 'NeverType',
111+
NumberValueType = 'NumberValueType',
112+
IntegerValueType = 'IntegerValueType',
113+
BigIntValueType = 'BigIntValueType',
114+
StringValueType = 'StringValueType',
115+
BooleanValueType = 'BooleanValueType',
116+
}
99117
// tslint:enable: object-literal-key-quotes

examples/schema/_compiled/tynder.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,4 +3412,43 @@ const schema = {
34123412
}
34133413
};
34143414
export default schema;
3415+
3416+
export const enum Schema {
3417+
RegExp = 'RegExp',
3418+
Map = 'Map',
3419+
PrimitiveValueTypes = 'PrimitiveValueTypes',
3420+
PrimitiveValueTypeNames = 'PrimitiveValueTypeNames',
3421+
OptionalPrimitiveValueTypeNames = 'OptionalPrimitiveValueTypeNames',
3422+
PlaceholderTypeNames = 'PlaceholderTypeNames',
3423+
OptionalPlaceholderTypeNames = 'OptionalPlaceholderTypeNames',
3424+
ErrorTypes = 'ErrorTypes',
3425+
ErrorMessages = 'ErrorMessages',
3426+
TypeAssertionErrorMessageConstraints = 'TypeAssertionErrorMessageConstraints',
3427+
TypeAssertionErrorMessage = 'TypeAssertionErrorMessage',
3428+
TypeAssertionBase = 'TypeAssertionBase',
3429+
NeverTypeAssertion = 'NeverTypeAssertion',
3430+
AnyTypeAssertion = 'AnyTypeAssertion',
3431+
UnknownTypeAssertion = 'UnknownTypeAssertion',
3432+
PrimitiveTypeAssertionConstraints = 'PrimitiveTypeAssertionConstraints',
3433+
PrimitiveTypeAssertion = 'PrimitiveTypeAssertion',
3434+
PrimitiveValueTypeAssertion = 'PrimitiveValueTypeAssertion',
3435+
RepeatedAssertionConstraints = 'RepeatedAssertionConstraints',
3436+
RepeatedAssertion = 'RepeatedAssertion',
3437+
SpreadAssertion = 'SpreadAssertion',
3438+
SequenceAssertion = 'SequenceAssertion',
3439+
OneOfAssertion = 'OneOfAssertion',
3440+
OptionalAssertion = 'OptionalAssertion',
3441+
EnumAssertion = 'EnumAssertion',
3442+
ObjectAssertionMember = 'ObjectAssertionMember',
3443+
AdditionalPropsKey = 'AdditionalPropsKey',
3444+
AdditionalPropsMember = 'AdditionalPropsMember',
3445+
ObjectAssertion = 'ObjectAssertion',
3446+
AssertionSymlink = 'AssertionSymlink',
3447+
TypeAssertion = 'TypeAssertion',
3448+
ValidationContext = 'ValidationContext',
3449+
TypeAssertionSetValue = 'TypeAssertionSetValue',
3450+
TypeAssertionMap = 'TypeAssertionMap',
3451+
SymbolResolverContext = 'SymbolResolverContext',
3452+
CodegenContext = 'CodegenContext',
3453+
}
34153454
// tslint:enable: object-literal-key-quotes

src/_spec/fixes-4.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { constraints as uniqueConstraints } from '../constraints/unique';
1616

1717

1818

19-
describe("fix-3", function() {
19+
describe("fix-4", function() {
2020
it("unique-1", function() {
2121
const schemas = [compile(`
2222
interface A {

src/_spec/fixes-5.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import { TypeAssertion,
3+
ValidationContext } from '../types';
4+
import { validate,
5+
isType,
6+
assertType,
7+
getType } from '../validator';
8+
import { pick,
9+
patch } from '../picker';
10+
import { compile } from '../compiler';
11+
import { generateTypeScriptCode } from '../codegen';
12+
import { serialize,
13+
deserialize,
14+
deserializeFromObject } from '../serializer';
15+
import { stereotypes as dateStereotypes } from '../stereotypes/date';
16+
import { constraints as uniqueConstraints } from '../constraints/unique';
17+
import primitivesSchema,
18+
{ Schema as PrimitivesSchema } from '../../examples/schema/_compiled/primitives';
19+
20+
21+
22+
describe("fix-5", function() {
23+
it("unique-1", function() {
24+
const ty = getType(deserializeFromObject(primitivesSchema), PrimitivesSchema.NumberType);
25+
expect(ty).toEqual({
26+
kind: 'primitive',
27+
primitiveName: 'number',
28+
typeName: 'NumberType',
29+
name: 'NumberType',
30+
});
31+
});
32+
});

src/serializer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { TypeAssertion,
88
SerializedSchemaInfo,
99
TypeAssertionSetValue,
1010
TypeAssertionMap } from './types';
11+
import { escapeString } from './lib/escape';
1112
import { resolveSchema } from './lib/resolver';
1213

1314

@@ -164,8 +165,16 @@ export function serialize(schema: TypeAssertionMap, asTs?: boolean): string {
164165
if (asTs) {
165166
return (
166167
`\n// tslint:disable: object-literal-key-quotes\n` +
167-
`const schema = ${JSON.stringify(ret, null, 2)};\nexport default schema;` +
168-
`\n// tslint:enable: object-literal-key-quotes\n`
168+
`const schema = ${JSON.stringify(ret, null, 2)};\nexport default schema;\n\n` +
169+
`export const enum Schema {\n${Object.keys(ret.ns['.']).filter(x => {
170+
return (!
171+
(/^[0-9]/.test(x) ||
172+
/[\u0000-\u001f\u007f]/.test(x) ||
173+
/\s/.test(x) ||
174+
/[@#$%^&+-=:;.,?!'"`/|{}()<>[\]\*\\]/.test(x))
175+
);
176+
}).map(x => ` ${x} = '${x}',\n`).join('')}` +
177+
`}\n// tslint:enable: object-literal-key-quotes\n`
169178
);
170179
} else {
171180
return JSON.stringify(ret, null, 2);

0 commit comments

Comments
 (0)