Skip to content

Commit

Permalink
transform
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Oct 29, 2023
1 parent 1ca9393 commit 433ff0d
Showing 1 changed file with 1 addition and 16 deletions.
17 changes: 1 addition & 16 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { EncodeTransform, DecodeTransform, HasTransform, TransformDecodeCheckError, TransformEncodeCheckError } from '../value/transform'
import { IsArray, IsString, IsNumber, IsBigInt } from '../value/guard'
import { Errors, ValueErrorIterator } from '../errors/errors'
import { TypeSystemPolicy } from '../system/index'
Expand All @@ -42,10 +41,7 @@ export type CheckFunction = (value: unknown) => boolean
// TypeCheck
// -------------------------------------------------------------------
export class TypeCheck<T extends Types.TSchema> {
private readonly hasTransform: boolean
constructor(private readonly schema: T, private readonly references: Types.TSchema[], private readonly checkFunc: CheckFunction, private readonly code: string) {
this.hasTransform = HasTransform.Has(schema, references)
}
constructor(private readonly schema: T, private readonly references: Types.TSchema[], private readonly checkFunc: CheckFunction, private readonly code: string) {}
/** Returns the generated assertion code used to validate this type. */
public Code(): string {
return this.code
Expand All @@ -58,17 +54,6 @@ export class TypeCheck<T extends Types.TSchema> {
public Check(value: unknown): value is Types.Static<T> {
return this.checkFunc(value)
}
/** Decodes a value or throws if error */
public Decode(value: unknown): Types.StaticDecode<T> {
if (!this.checkFunc(value)) throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First()!)
return this.hasTransform ? DecodeTransform.Decode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value
}
/** Encodes a value or throws if error */
public Encode(value: unknown): Types.StaticEncode<T> {
const encoded = this.hasTransform ? EncodeTransform.Encode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value
if (!this.checkFunc(encoded)) throw new TransformEncodeCheckError(this.schema, value, this.Errors(value).First()!)
return encoded
}
}
// -------------------------------------------------------------------
// Character
Expand Down

0 comments on commit 433ff0d

Please sign in to comment.