Skip to content

Commit 91d7bf5

Browse files
chore: update dependencies (#31)
1 parent 87482f7 commit 91d7bf5

File tree

7 files changed

+465
-475
lines changed

7 files changed

+465
-475
lines changed

src/ComplexExample.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export interface TypeA {
1010
}
1111

1212
export interface TypeB {
13-
id: number;
13+
id: number | null;
1414
/**
1515
* @format date-time
1616
*/
17-
value: string;
17+
value: string | null;
1818
}
1919

2020
export interface RequestA {

src/Example.validator.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export const ExampleTypeSchema = {
2525
type: 'number',
2626
},
2727
email: {
28-
format: 'email',
2928
type: 'string',
3029
},
3130
value: {
@@ -37,16 +36,16 @@ export const ExampleTypeSchema = {
3736
};
3837
export type ValidateFunction<T> = ((data: unknown) => data is T) &
3938
Pick<Ajv.ValidateFunction, 'errors'>;
40-
const rawValidateExampleType = ajv.compile(
41-
ExampleTypeSchema,
42-
) as ValidateFunction<ExampleType>;
39+
export const isExampleType = ajv.compile(ExampleTypeSchema) as ValidateFunction<
40+
ExampleType
41+
>;
4342
export default function validate(value: unknown): ExampleType {
44-
if (rawValidateExampleType(value)) {
43+
if (isExampleType(value)) {
4544
return value;
4645
} else {
4746
throw new Error(
4847
ajv.errorsText(
49-
rawValidateExampleType.errors!.filter((e: any) => e.keyword !== 'if'),
48+
isExampleType.errors!.filter((e: any) => e.keyword !== 'if'),
5049
{dataVar: 'ExampleType'},
5150
) +
5251
'\n\n' +

src/__tests__/output/ComplexExample.validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ export const Schema = {
6666
TypeB: {
6767
properties: {
6868
id: {
69-
type: 'number',
69+
type: ['null', 'number'],
7070
},
7171
value: {
7272
format: 'date-time',
73-
type: 'string',
73+
type: ['null', 'string'],
7474
},
7575
},
7676
required: ['id', 'value'],

src/__tests__/parse.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ Object {
7474
"TypeB": Object {
7575
"properties": Object {
7676
"id": Object {
77-
"type": "number",
77+
"type": Array [
78+
"null",
79+
"number",
80+
],
7881
},
7982
"value": Object {
8083
"format": "date-time",
81-
"type": "string",
84+
"type": Array [
85+
"null",
86+
"string",
87+
],
8288
},
8389
},
8490
"required": Array [
@@ -108,14 +114,14 @@ test('ajv', () => {
108114
const ajv = new Ajv({coerceTypes: false, allErrors: true, useDefaults: true});
109115
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
110116
ajv.addSchema(schema, 'root');
111-
const validateMyEnum = ajv.getSchema('root#/definitions/MyEnum');
117+
const validateMyEnum = ajv.getSchema('root#/definitions/MyEnum')!;
112118
expect(validateMyEnum(1)).toBe(true);
113119
expect(validateMyEnum(10)).toBe(false);
114120
expect(
115121
ajv.errorsText(validateMyEnum.errors, {dataVar: 'x'}),
116122
).toMatchInlineSnapshot(`"x should be equal to one of the allowed values"`);
117123

118-
const validateRequestA = ajv.getSchema('root#/definitions/RequestA');
124+
const validateRequestA = ajv.getSchema('root#/definitions/RequestA')!;
119125
expect(
120126
validateRequestA({query: {id: 'x', value: 'y'}, params: {e: 42}}),
121127
).toBe(false);

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export {
2020
export default function run(args?: string[]) {
2121
const {files, options} = parseArgs(args);
2222
const tsConfig = loadTsConfig();
23-
const parsed = parse(files.map(f => f.fileName), tsConfig, options.schema);
23+
const parsed = parse(
24+
files.map(f => f.fileName),
25+
tsConfig,
26+
options.schema,
27+
);
2428

2529
files.forEach(({fileName, typeName}) => {
2630
const outputFileName = fileName.replace(/\.tsx?$/, '.validator.ts');

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"noUnusedParameters": true,
2222
"declaration": true,
2323
"lib": ["es2018"],
24-
"incremental": true
24+
"incremental": false
2525
},
2626
"include": ["src"],
2727
"exclude": ["src/__tests__/build-parameters"]

0 commit comments

Comments
 (0)