Skip to content

Commit

Permalink
chore: update dependencies (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay authored Mar 3, 2020
1 parent 87482f7 commit 91d7bf5
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 475 deletions.
4 changes: 2 additions & 2 deletions src/ComplexExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface TypeA {
}

export interface TypeB {
id: number;
id: number | null;
/**
* @format date-time
*/
value: string;
value: string | null;
}

export interface RequestA {
Expand Down
11 changes: 5 additions & 6 deletions src/Example.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const ExampleTypeSchema = {
type: 'number',
},
email: {
format: 'email',
type: 'string',
},
value: {
Expand All @@ -37,16 +36,16 @@ export const ExampleTypeSchema = {
};
export type ValidateFunction<T> = ((data: unknown) => data is T) &
Pick<Ajv.ValidateFunction, 'errors'>;
const rawValidateExampleType = ajv.compile(
ExampleTypeSchema,
) as ValidateFunction<ExampleType>;
export const isExampleType = ajv.compile(ExampleTypeSchema) as ValidateFunction<
ExampleType
>;
export default function validate(value: unknown): ExampleType {
if (rawValidateExampleType(value)) {
if (isExampleType(value)) {
return value;
} else {
throw new Error(
ajv.errorsText(
rawValidateExampleType.errors!.filter((e: any) => e.keyword !== 'if'),
isExampleType.errors!.filter((e: any) => e.keyword !== 'if'),
{dataVar: 'ExampleType'},
) +
'\n\n' +
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/output/ComplexExample.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export const Schema = {
TypeB: {
properties: {
id: {
type: 'number',
type: ['null', 'number'],
},
value: {
format: 'date-time',
type: 'string',
type: ['null', 'string'],
},
},
required: ['id', 'value'],
Expand Down
14 changes: 10 additions & 4 deletions src/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,17 @@ Object {
"TypeB": Object {
"properties": Object {
"id": Object {
"type": "number",
"type": Array [
"null",
"number",
],
},
"value": Object {
"format": "date-time",
"type": "string",
"type": Array [
"null",
"string",
],
},
},
"required": Array [
Expand Down Expand Up @@ -108,14 +114,14 @@ test('ajv', () => {
const ajv = new Ajv({coerceTypes: false, allErrors: true, useDefaults: true});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
ajv.addSchema(schema, 'root');
const validateMyEnum = ajv.getSchema('root#/definitions/MyEnum');
const validateMyEnum = ajv.getSchema('root#/definitions/MyEnum')!;
expect(validateMyEnum(1)).toBe(true);
expect(validateMyEnum(10)).toBe(false);
expect(
ajv.errorsText(validateMyEnum.errors, {dataVar: 'x'}),
).toMatchInlineSnapshot(`"x should be equal to one of the allowed values"`);

const validateRequestA = ajv.getSchema('root#/definitions/RequestA');
const validateRequestA = ajv.getSchema('root#/definitions/RequestA')!;
expect(
validateRequestA({query: {id: 'x', value: 'y'}, params: {e: 42}}),
).toBe(false);
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export {
export default function run(args?: string[]) {
const {files, options} = parseArgs(args);
const tsConfig = loadTsConfig();
const parsed = parse(files.map(f => f.fileName), tsConfig, options.schema);
const parsed = parse(
files.map(f => f.fileName),
tsConfig,
options.schema,
);

files.forEach(({fileName, typeName}) => {
const outputFileName = fileName.replace(/\.tsx?$/, '.validator.ts');
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"noUnusedParameters": true,
"declaration": true,
"lib": ["es2018"],
"incremental": true
"incremental": false
},
"include": ["src"],
"exclude": ["src/__tests__/build-parameters"]
Expand Down
Loading

0 comments on commit 91d7bf5

Please sign in to comment.