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

code-gen: add file validator support #649

Merged
merged 1 commit into from
Feb 2, 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ packages/cli/template/*
coverage
packages/code-gen/test-generated
lerna.json
generated/testing/client/anonymous-validators.js
52 changes: 46 additions & 6 deletions packages/code-gen/src/generator/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ function generateValidatorsForGroup(context, imports, anonymousImports, group) {
{},
)} | undefined, errors: ({ key: string, info: any }[])|undefined}}`;
}
return js`*
@returns {
${getTypeNameForType(context.context, data[name], "", {})}
}`;
return js`
* @returns {${getTypeNameForType(
context.context,
data[name],
"",
{},
)}}`;
}}
*/
export function validate${data[name].uniqueName}(value${withTypescript(
Expand Down Expand Up @@ -390,8 +393,7 @@ function anonymousValidatorForType(context, imports, type) {
case "date":
return anonymousValidatorDate(context, imports);
case "file":
// TODO: Implement for possible locations
return `return value;`;
return anonymousValidatorFile(context);
case "generic":
return anonymousValidatorGeneric(context, imports, type);
case "number":
Expand Down Expand Up @@ -640,6 +642,44 @@ function anonymousValidatorDate(context, imports) {
`;
}

/**
* @param {ValidatorContext} context
* @returns {string}
*/
function anonymousValidatorFile(context) {
if (context.context.options.isBrowser) {
return js`
// Blob result from api client
if (value instanceof Blob) {
return value;
}
// Blob input as post argument
if (value && value.blob instanceof Blob) {
return value;
}

${buildError("unknown", "{ propertyPath }")}
`;
}

return js`
// ReadableStream input to api call
if (typeof value.data?.pipe === "function" && typeof value.data?._read === "function") {
return value;
}
// ReadableStream as output of an api call
if (typeof value?.pipe === "function" && typeof value?._read === "function") {
return value;
}
// Object as parsed by the file body parsers
if (typeof value?.path === "string" && typeof value?.size === "number") {
return value;
}

${buildError("unknown", "{ propertyPath }")}
`;
}

/**
* @param {ValidatorContext} context
* @param {ImportCreator} imports
Expand Down
20 changes: 20 additions & 0 deletions packages/code-gen/test/e2e-server.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createReadStream } from "fs";
import { mainTestFn, test } from "@compas/cli";
import {
closeTestApp,
Expand Down Expand Up @@ -131,6 +132,17 @@ test("code-gen/e2e-server", async (t) => {
},
);

t.test("server - serverside validator of file is ok", async (t) => {
const { success } = await server.serverApi.setFile({
myFile: {
name: "foo.json",
data: createReadStream("./__fixtures__/code-gen/openapi.json"),
},
});

t.ok(success);
});

t.test("server - router - tags are available", (t) => {
t.deepEqual(server.serverTags.getId, ["tag"]);
t.deepEqual(server.serverTags.create, []);
Expand Down Expand Up @@ -204,5 +216,13 @@ async function buildTestApp() {
return next();
};

server.serverHandlers.setFile = (ctx, next) => {
ctx.body = {
success: true,
};

return next();
};

return app;
}
Loading