@sinclair/typebox 0.24.0 → 0.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,5 @@
1
1
  import * as Types from '../typebox';
2
- export declare type CheckFunction = (value: unknown) => CheckOk | CheckFail;
3
- export interface CheckOk {
4
- ok: true;
5
- }
6
- export interface CheckFail {
7
- ok: false;
8
- }
2
+ export declare type CheckFunction = (value: unknown) => boolean;
9
3
  export declare class TypeCheckAssertError extends Error {
10
4
  readonly schema: Types.TSchema;
11
5
  readonly value: unknown;
@@ -51,16 +51,14 @@ class TypeCheck {
51
51
  }
52
52
  /** Returns true if the value is valid. */
53
53
  Check(value) {
54
- const result = this.checkFunc(value);
55
- return result.ok;
54
+ return this.checkFunc(value);
56
55
  }
57
56
  /** Asserts the given value and throws a TypeCheckAssertError if invalid. */
58
57
  Assert(value) {
59
58
  // The return type for this function should be 'asserts value is Static<T>' but due
60
59
  // to a limitation in TypeScript, this currently isn't possible. See issue below.
61
60
  // https://github.com/microsoft/TypeScript/issues/36931
62
- const result = this.checkFunc(value);
63
- if (!result.ok)
61
+ if (!this.checkFunc(value))
64
62
  throw new TypeCheckAssertError(this.schema, value);
65
63
  }
66
64
  }
@@ -187,11 +185,11 @@ var TypeCompiler;
187
185
  PushLocal(body);
188
186
  }
189
187
  const func = CreateFunctionName(schema.$ref);
190
- yield CreateCondition(schema, path, `(${func}(${path}).ok)`);
188
+ yield CreateCondition(schema, path, `(${func}(${path}))`);
191
189
  }
192
190
  function* Self(schema, path) {
193
191
  const func = CreateFunctionName(schema.$ref);
194
- yield CreateCondition(schema, path, `(${func}(${path}).ok)`);
192
+ yield CreateCondition(schema, path, `(${func}(${path}))`);
195
193
  }
196
194
  function* String(schema, path) {
197
195
  yield CreateCondition(schema, path, `(typeof ${path} === 'string')`);
@@ -240,7 +238,7 @@ var TypeCompiler;
240
238
  const name = CreateFunctionName(schema.$id);
241
239
  const body = CreateFunction(name, conditions);
242
240
  PushLocal(body);
243
- yield CreateCondition(schema, path, `(${name}(${path}).ok)`);
241
+ yield CreateCondition(schema, path, `(${name}(${path}))`);
244
242
  return;
245
243
  }
246
244
  const anySchema = schema;
@@ -326,8 +324,8 @@ var TypeCompiler;
326
324
  return `check_${$id.replace(/-/g, '_')}`;
327
325
  }
328
326
  function CreateFunction(name, conditions) {
329
- const statements = conditions.map((condition, index) => ` if(!${condition.expr}) { return { ok: false } }`);
330
- return `function ${name}(value) {\n${statements.join('\n')}\n return { ok: true }\n}`;
327
+ const statements = conditions.map((condition, index) => ` if(!${condition.expr}) { return false }`);
328
+ return `function ${name}(value) {\n${statements.join('\n')}\n return true\n}`;
331
329
  }
332
330
  // -------------------------------------------------------------------
333
331
  // Compiler
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
package/typebox.d.ts CHANGED
@@ -84,15 +84,6 @@ export interface TEnum<T extends Record<string, string | number> = Record<string
84
84
  static: T[keyof T];
85
85
  anyOf: TLiteral<string | number>[];
86
86
  }
87
- export interface TExclude<T extends TUnion, U extends TUnion> extends TUnion {
88
- [Kind]: 'Union';
89
- static: Exclude<Static<T, this['params']>, Static<U, this['params']>>;
90
- }
91
- export interface TExtract<T extends TSchema, U extends TUnion> extends TUnion {
92
- [Kind]: 'Union';
93
- static: Extract<Static<T, this['params']>, Static<U, this['params']>>;
94
- }
95
- export declare type TExtends<T extends TSchema, U extends TSchema, X extends TSchema, Y extends TSchema> = T extends TAny ? (U extends TUnknown ? X : U extends TAny ? X : TUnion<[X, Y]>) : T extends U ? X : Y;
96
87
  export declare type TFunctionParameters<T extends readonly TSchema[], P extends unknown[]> = [...{
97
88
  [K in keyof T]: T[K] extends TSchema ? Static<T[K], P> : never;
98
89
  }];