Skip to content

Commit

Permalink
fix(json-type): 🐛 correctly read node kind for codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 23, 2024
1 parent 1bc2dda commit ac273dd
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 37 deletions.
4 changes: 1 addition & 3 deletions src/json-type/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import type {Expr} from '../../json-expression';

export interface TType<Value = unknown> extends Display, Partial<Identifiable> {
/**
* "t" is short for "type". Double underscore "__" is borrowed from GraphQL,
* where they use "__typeName". Values are short strings, such as "str", "num",
* and "bin", borrowed from MessagePack.
* The type of the JSON Type node.
*/
kind: string;

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/AbstractType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export abstract class AbstractType<S extends schema.Schema> implements BaseType<
}

public getOptions(): schema.Optional<S> {
const {kind: __t, ...options} = this.schema;
const {kind, ...options} = this.schema;
return options as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/ArrayType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class ArrayType<T extends Type> extends AbstractType<schema.ArraySchema<S
}

public getOptions(): schema.Optional<schema.ArraySchema<SchemaOf<T>>> {
const {kind: __t, type, ...options} = this.schema;
const {kind, type, ...options} = this.schema;
return options as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/BinaryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class BinaryType<T extends Type> extends AbstractType<schema.BinarySchema
}

public getOptions(): schema.Optional<schema.ArraySchema<SchemaOf<T>>> {
const {kind: __t, type, ...options} = this.schema;
const {kind, type, ...options} = this.schema;
return options as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/ConstType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class ConstType<V = any> extends AbstractType<schema.ConstSchema<V>> {
}

public getOptions(): schema.Optional<schema.ConstSchema<V>> {
const {kind: __t, value, ...options} = this.schema;
const {kind, value, ...options} = this.schema;
return options as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/MapType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class MapType<T extends Type> extends AbstractType<schema.MapSchema<Schem
}

public getOptions(): schema.Optional<schema.MapSchema<SchemaOf<T>>> {
const {kind: __t, type, ...options} = this.schema;
const {kind, type, ...options} = this.schema;
return options as any;
}

Expand Down
30 changes: 6 additions & 24 deletions src/json-type/type/classes/ObjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ObjectFieldType<K extends string, V extends Type> extends AbstractT
}

public getOptions(): schema.Optional<schema.ObjectFieldSchema<K, SchemaOf<V>>> {
const {kind: __t, key, type, optional, ...options} = this.schema;
const {kind, key, type, optional, ...options} = this.schema;
return options as any;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ export class ObjectType<F extends ObjectFieldType<any, any>[] = ObjectFieldType<
}

public getOptions(): schema.Optional<schema.ObjectSchema<SchemaOfObjectFields<F>>> {
const {kind: __t, fields, ...options} = this.schema;
const {kind, fields, ...options} = this.schema;
return options as any;
}

Expand Down Expand Up @@ -180,28 +180,10 @@ export class ObjectType<F extends ObjectFieldType<any, any>[] = ObjectFieldType<
field.value.codegenValidator(ctx, keyPath, rv);
ctx.js(`}`);
} else {
// TODO: move this line into if-statement
ctx.js(/* js */ `var ${rv} = ${r}${accessor};`);
if (!canSkipObjectKeyUndefinedCheck((field.value as AbstractType<any>).getSchema().__t)) {
if (!canSkipObjectKeyUndefinedCheck((field.value as AbstractType<any>).getSchema().kind)) {
const err = ctx.err(ValidationError.KEY, [...path, field.key]);
const kind = field.value.getSchema().kind;
const cannotBeUndefined =
kind === 'bool' ||
kind === 'num' ||
kind === 'str' ||
kind === 'bin' ||
kind === 'arr' ||
kind === 'obj' ||
kind === 'tup' ||
kind === 'map' ||
kind === 'fn' ||
kind === 'fn$';
if (cannotBeUndefined) {
ctx.js(/* js */ `if (${rv} === undefined) return ${err};`);
} else {
// TODO: shorten "Object.hasOwnProperty.call"
ctx.js(`if (!Object.hasOwnProperty.call(${r}, ${JSON.stringify(field.key)})) return ${err};`);
}
ctx.js(/* js */ `var ${rv} = ${r}${accessor};`);
ctx.js(/* js */ `if (${rv} === undefined) return ${err};`);
}
field.value.codegenValidator(ctx, keyPath, `${r}${accessor}`);
}
Expand Down Expand Up @@ -572,7 +554,7 @@ if (${rLength}) {
}

public toString(tab: string = ''): string {
const {kind: __t, fields, ...rest} = this.getSchema();
const {kind, fields, ...rest} = this.getSchema();
return (
super.toString(tab) +
printTree(
Expand Down
4 changes: 2 additions & 2 deletions src/json-type/type/classes/OrType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class OrType<T extends Type[]> extends AbstractType<schema.OrSchema<{[K i

constructor(
protected types: T,
options?: Omit<schema.OrSchema, '__t' | 'type'>,
options?: Omit<schema.OrSchema, 'kind' | 'type'>,
) {
super();
this.schema = {
Expand All @@ -53,7 +53,7 @@ export class OrType<T extends Type[]> extends AbstractType<schema.OrSchema<{[K i
}

public getOptions(): schema.Optional<schema.OrSchema<{[K in keyof T]: SchemaOf<T[K]>}>> {
const {kind: __t, types, ...options} = this.schema;
const {kind, types, ...options} = this.schema;
return options as any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/json-type/type/classes/RefType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class RefType<T extends Type> extends AbstractType<schema.RefSchema<Schem
}

public getOptions(): schema.Optional<schema.RefSchema<SchemaOf<T>>> {
const {kind: __t, ref, ...options} = this.schema;
const {kind, ref, ...options} = this.schema;
return options as any;
}

Expand Down
4 changes: 2 additions & 2 deletions src/json-type/type/classes/TupleType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class TupleType<T extends Type[]> extends AbstractType<schema.TupleSchema

constructor(
public readonly types: T,
options?: Omit<schema.TupleSchema, '__t' | 'type'>,
options?: Omit<schema.TupleSchema, 'kind' | 'type'>,
) {
super();
this.schema = {...schema.s.Tuple(), ...options};
Expand All @@ -48,7 +48,7 @@ export class TupleType<T extends Type[]> extends AbstractType<schema.TupleSchema
}

public getOptions(): schema.Optional<schema.TupleSchema<{[K in keyof T]: SchemaOf<T[K]>}>> {
const {kind: __t, types, ...options} = this.schema;
const {kind, types, ...options} = this.schema;
return options as any;
}

Expand Down

0 comments on commit ac273dd

Please sign in to comment.