Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump zod from 3.9.8 to 3.10.3 #176

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"mime": "2.5.2",
"openapi3-ts": "2.0.1",
"winston": "3.3.3",
"zod": "3.9.8"
"zod": "3.10.3"
},
"devDependencies": {
"@tsconfig/node10": "^1.0.8",
Expand Down
32 changes: 14 additions & 18 deletions src/file-schema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
ParseContext,
ParseReturnType,
ZodIssueCode,
ZodParsedType,
ZodType,
INVALID,
OK,
ZodTypeDef
ZodTypeDef,
addIssueToContext
} from 'zod';
import {ParseInput} from 'zod/lib/helpers/parseUtil';
import {ErrMessage, errToObj} from './helpers';

const zodFileKind = 'ZodFile';
Expand All @@ -28,34 +28,30 @@ export interface ZodFileDef extends ZodTypeDef {
const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;

export class ZodFile extends ZodType<string, ZodFileDef> {
_parse(
ctx: ParseContext,
data: any,
parsedType: ZodParsedType
): ParseReturnType<string> {
if (parsedType !== ZodParsedType.string) {
this.addIssue(ctx, {
_parse(input: ParseInput): ParseReturnType<string> {
const { status, ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.string) {
addIssueToContext(ctx, {
code: ZodIssueCode.invalid_type,
expected: ZodParsedType.string,
received: parsedType,
}, { data });
received: ctx.parsedType,
});
return INVALID;
}
let invalid = false;

for (const check of this._def.checks) {
if (check.kind === 'base64') {
if (!base64Regex.test(data)) {
invalid = true;
this.addIssue(ctx, {
if (!base64Regex.test(ctx.data)) {
addIssueToContext(ctx, {
code: ZodIssueCode.custom,
message: check.message,
}, { data });
});
status.dirty();
}
}
}

return invalid ? INVALID : OK(data);
return { status: status.value, value: ctx.data };
}

binary = (message?: ErrMessage) =>
Expand Down
22 changes: 10 additions & 12 deletions src/upload-schema.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {UploadedFile} from 'express-fileupload';
import {
ParseContext,
ParseReturnType,
ZodIssueCode,
ZodParsedType,
ZodType,
INVALID,
OK,
ZodTypeDef
ZodTypeDef,
addIssueToContext
} from 'zod';
import {ParseInput} from 'zod/lib/helpers/parseUtil';

const zodUploadKind = 'ZodUpload';

Expand All @@ -25,20 +26,17 @@ const isUploadedFile = (data: any): data is UploadedFile =>
typeof data.md5 === 'string' && typeof data.mv === 'function';

export class ZodUpload extends ZodType<UploadedFile, ZodUploadDef> {
_parse(
ctx: ParseContext,
data: any,
parsedType: ZodParsedType
): ParseReturnType<UploadedFile> {
if (parsedType !== ZodParsedType.object || !isUploadedFile(data)) {
this.addIssue(ctx, {
_parse(input: ParseInput): ParseReturnType<UploadedFile> {
const { ctx } = this._processInputParams(input);
if (ctx.parsedType !== ZodParsedType.object || !isUploadedFile(ctx.data)) {
addIssueToContext(ctx, {
code: ZodIssueCode.custom,
message: `Expected file upload, received ${parsedType}`
}, { data });
message: `Expected file upload, received ${ctx.parsedType}`
});
return INVALID;
}

return OK(data);
return OK(ctx.data);
}

static create = () => new ZodUpload({
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/__snapshots__/upload-schema.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

exports[`ZodUpload _parse() should accept UploadedFile 1`] = `
Object {
"valid": true,
"value": Object {
"data": Object {
"data": Object {
"data": Array [
115,
Expand All @@ -27,5 +26,6 @@ Object {
"tempFilePath": "",
"truncated": false,
},
"success": true,
}
`;
89 changes: 29 additions & 60 deletions tests/unit/file-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {ParseContext, getParsedType} from 'zod';
import {ZodFile} from '../../src/file-schema';
import fs from 'fs';

Expand Down Expand Up @@ -40,90 +39,60 @@ describe('ZodFile', () => {

describe('_parse()', () => {
test('should handle wrong parsed type', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodFile.create();
const result = schema._parse(context, 123, 'number');
expect(result).toEqual({
valid: false
});
expect(context.issues).toEqual([{
code: 'invalid_type',
expected: 'string',
message: 'Expected string, received number',
path: [],
received: 'number',
}]);
const result = schema.safeParse(123);
expect(result.success).toBeFalsy();
if (!result.success) {
expect(result.error.issues).toEqual([{
code: 'invalid_type',
expected: 'string',
message: 'Expected string, received number',
path: [],
received: 'number',
}]);
}
});

test('should perform additional check for base64 file', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodFile.create().base64('this is not base64');
const result = schema._parse(context, '~~~~', 'string');
expect(result).toEqual({
valid: false
});
expect(context.issues).toEqual([{
code: 'custom',
message: 'this is not base64',
path: [],
}]);
const result = schema.safeParse('~~~~');
expect(result.success).toBeFalsy();
if (!result.success) {
expect(result.error.issues).toEqual([{
code: 'custom',
message: 'this is not base64',
path: [],
}]);
}
});

test('should accept string', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodFile.create();
const result = schema._parse(context, 'some string', 'string');
const result = schema.safeParse('some string');
expect(result).toEqual({
valid: true,
value: 'some string'
success: true,
data: 'some string'
});
expect(context.issues).toEqual([]);
});

test('should accept binary read string', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodFile.create().binary();
const data = fs.readFileSync('logo.svg', 'binary');
const type = getParsedType(data);
const result = schema._parse(context, data, type);
const result = schema.safeParse(data);
expect(result).toEqual({
valid: true,
value: data
success: true,
data
});
expect(context.issues).toEqual([]);
});

test('should accept base64 read string', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodFile.create().base64();
const data = fs.readFileSync('logo.svg', 'base64');
const type = getParsedType(data);
const result = schema._parse(context, data, type);
const result = schema.safeParse(data);
expect(result).toEqual({
valid: true,
value: data
success: true,
data
});
expect(context.issues).toEqual([]);
});
});
});
34 changes: 11 additions & 23 deletions tests/unit/upload-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {ParseContext} from 'zod';
import {ZodUpload} from '../../src/upload-schema';

describe('ZodUpload', () => {
Expand All @@ -12,32 +11,22 @@ describe('ZodUpload', () => {

describe('_parse()', () => {
test('should handle wrong parsed type', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodUpload.create();
const result = schema._parse(context, 123, 'number');
expect(result).toEqual({
valid: false
});
expect(context.issues).toEqual([{
code: 'custom',
message: 'Expected file upload, received number',
path: [],
}]);
const result = schema.safeParse(123);
expect(result.success).toBeFalsy();
if (!result.success) {
expect(result.error.issues).toEqual([{
code: 'custom',
message: 'Expected file upload, received number',
path: [],
}]);
}
});

test('should accept UploadedFile', () => {
const context = new ParseContext({
path: null,
issues: [],
async: false,
});
const schema = ZodUpload.create();
const buffer = Buffer.from('something');
const result = schema._parse(context, {
const result = schema.safeParse({
name: 'avatar.jpg',
mv: async () => Promise.resolve(),
encoding: 'utf-8',
Expand All @@ -47,9 +36,8 @@ describe('ZodUpload', () => {
truncated: false,
size: 100500,
md5: ''
}, 'object');
});
expect(result).toMatchSnapshot();
expect(context.issues).toEqual([]);
});
});
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5427,7 +5427,7 @@ yn@3.1.1:
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

zod@3.9.8:
version "3.9.8"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.9.8.tgz#562eda33925203542dea70100ce0d3766d04b0ab"
integrity sha512-pTNhdJd45PPOBpdxO8x00Tv+HhknYGx3WdgFQndazp+G1Gd2Cxf81L5yMfCIIcd/SA3VqrcTv/G4bFcr+DsZhA==
zod@3.10.3:
version "3.10.3"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.10.3.tgz#8ddcc95a52d5960251953362d3700f55dacb5969"
integrity sha512-ngMXcA/TNXnlXWAmAdH98U6rV5W0K8wNASafMIYUuOrBfwtO7LyPXaOi/riRZns1qfWuE1lHr7gt9N4rmyYFlQ==