@sinclair/typebox 0.28.17 → 0.28.18
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.
- package/compiler/compiler.d.ts +1 -1
- package/compiler/compiler.js +15 -10
- package/package.json +1 -1
package/compiler/compiler.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare class TypeCompilerTypeGuardError extends Error {
|
|
|
27
27
|
constructor(schema: Types.TSchema);
|
|
28
28
|
}
|
|
29
29
|
export interface TypeCompilerOptions {
|
|
30
|
-
language
|
|
30
|
+
language?: 'typescript' | 'javascript';
|
|
31
31
|
}
|
|
32
32
|
/** Compiles Types for Runtime Type Checking */
|
|
33
33
|
export declare namespace TypeCompiler {
|
package/compiler/compiler.js
CHANGED
|
@@ -207,7 +207,7 @@ var TypeCompiler;
|
|
|
207
207
|
if (schema.uniqueItems === true)
|
|
208
208
|
yield `((function() { const set = new Set(); for(const element of ${value}) { const hashed = hash(element); if(set.has(hashed)) { return false } else { set.add(hashed) } } return true })())`;
|
|
209
209
|
const expression = CreateExpression(schema.items, references, 'value');
|
|
210
|
-
const parameter = CreateParameter('value');
|
|
210
|
+
const parameter = CreateParameter('value', 'any');
|
|
211
211
|
yield `${value}.every((${parameter}) => ${expression})`;
|
|
212
212
|
}
|
|
213
213
|
function* BigInt(schema, references, value) {
|
|
@@ -521,20 +521,24 @@ var TypeCompiler;
|
|
|
521
521
|
functions: new Set(),
|
|
522
522
|
customs: new Map(), // custom type data
|
|
523
523
|
};
|
|
524
|
-
function CreateExpression(schema, references, value) {
|
|
525
|
-
return `(${[...Visit(schema, references, value)].join(' && ')})`;
|
|
526
|
-
}
|
|
527
524
|
function CreateFunctionName($id) {
|
|
528
525
|
return `check_${Identifier.Encode($id)}`;
|
|
529
526
|
}
|
|
530
|
-
function
|
|
531
|
-
|
|
527
|
+
function CreateExpression(schema, references, value) {
|
|
528
|
+
return `(${[...Visit(schema, references, value)].join(' && ')})`;
|
|
529
|
+
}
|
|
530
|
+
function CreateParameter(name, type) {
|
|
531
|
+
const annotation = state.language === 'typescript' ? `: ${type}` : '';
|
|
532
532
|
return `${name}${annotation}`;
|
|
533
533
|
}
|
|
534
|
+
function CreateReturns(type) {
|
|
535
|
+
return state.language === 'typescript' ? `: ${type}` : '';
|
|
536
|
+
}
|
|
534
537
|
function CreateFunction(name, schema, references, value) {
|
|
535
538
|
const expression = [...Visit(schema, references, value, true)].map((condition) => ` ${condition}`).join(' &&\n');
|
|
536
|
-
const parameter = CreateParameter('value');
|
|
537
|
-
|
|
539
|
+
const parameter = CreateParameter('value', 'any');
|
|
540
|
+
const returns = CreateReturns('boolean');
|
|
541
|
+
return `function ${name}(${parameter})${returns} {\n return (\n${expression}\n )\n}`;
|
|
538
542
|
}
|
|
539
543
|
function PushFunction(functionBody) {
|
|
540
544
|
state.variables.add(functionBody);
|
|
@@ -553,10 +557,11 @@ var TypeCompiler;
|
|
|
553
557
|
function Build(schema, references) {
|
|
554
558
|
const check = CreateFunction('check', schema, references, 'value'); // interior visit
|
|
555
559
|
const locals = GetLocals();
|
|
556
|
-
const parameter = CreateParameter('value');
|
|
560
|
+
const parameter = CreateParameter('value', 'any');
|
|
561
|
+
const returns = CreateReturns('boolean');
|
|
557
562
|
// prettier-ignore
|
|
558
563
|
return IsString(schema.$id) // ensure top level schemas with $id's are hoisted
|
|
559
|
-
? `${locals.join('\n')}\nreturn function check(${parameter}) {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
564
|
+
? `${locals.join('\n')}\nreturn function check(${parameter})${returns} {\n return ${CreateFunctionName(schema.$id)}(value)\n}`
|
|
560
565
|
: `${locals.join('\n')}\nreturn ${check}`;
|
|
561
566
|
}
|
|
562
567
|
/** Returns the generated assertion code used to validate this type. */
|