Skip to content

Commit ebaeaab

Browse files
tomohisaotaForbesLindesay
authored andcommitted
feat: export raw validator function as isTypeName(value): value is TypeName (#22)
1 parent 9ce784a commit ebaeaab

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/__tests__/build-parameters.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {exec as execCB, ExecOptions} from 'child_process';
44
import * as path from 'path';
55
import {promisify} from 'util';
66

7+
jest.setTimeout(30000);
8+
79
const rimraf = promisify(rimrafCB);
810

911
const testDir = path.join(__dirname, 'build-parameters');

src/template.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,19 @@ export const compileSchema = (schemaName: string, typeName: string) =>
6262
export const validateFn = (
6363
typeName: string,
6464
schemaName: string,
65-
) => `const rawValidate${typeName} = ${compileSchema(schemaName, typeName)};
65+
) => `export const is${typeName} = ${compileSchema(schemaName, typeName)};
6666
export default function validate(value: unknown): ${typeName} {
67-
if (rawValidate${typeName}(value)) {
67+
if (is${typeName}(value)) {
6868
return value;
6969
} else {
7070
throw new Error(
71-
ajv.errorsText(rawValidate${typeName}.errors!.filter((e: any) => e.keyword !== 'if'), {dataVar: '${typeName}'}) +
71+
ajv.errorsText(is${typeName}.errors!.filter((e: any) => e.keyword !== 'if'), {dataVar: '${typeName}'}) +
7272
'\\n\\n' +
7373
inspect(value),
7474
);
7575
}
76-
}`;
76+
}
77+
`;
7778

7879
function typeOf(typeName: string, property: string, schema: TJS.Definition) {
7980
if (schema.definitions && schema.definitions[typeName]) {

0 commit comments

Comments
 (0)