Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Redo data validation #17

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Redo data validation #17

wants to merge 8 commits into from

Conversation

isaiahdoyle
Copy link
Contributor

This branch strips out all dynamoose calls from the routers and adds data validation using AJV given our new shiny schema!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can i leave this here?

@@ -41,15 +45,17 @@ userRouter.put('/', async (req, res) => {
console.error(`Error: ${e} - Status Code ${res.statusCode}`);
}
});
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isn't any spec for updating a user in the new schema. do we still want this router?

Copy link
Contributor

@seiyaterada seiyaterada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome!

Copy link
Contributor

@BraidenCutforth BraidenCutforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks great. I think you might be able to move the AJV compilations to the tops of the files. You can remove all of the new Course() and friends calls, you will just pass the validated data right to the db. Then when checking for valid data and what not, make sure that when invalid data is given or required data is missing that a error response is sent. (some cases you check for valid data and respond properly but no response is given if the data is missing)

src/routers/course.ts Outdated Show resolved Hide resolved
src/routers/course.ts Outdated Show resolved Hide resolved
src/routers/course.ts Outdated Show resolved Hide resolved
src/routers/semester.ts Outdated Show resolved Hide resolved
src/routers/semester.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@BraidenCutforth BraidenCutforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just a couple more places where that else statement is needed to throw so we are guaranteed to call res.send

const checkPost = ajv.compile(schemas.CourseCreate);
const checkPut = ajv.compile(schemas.CourseUpdate);

const ERROR_RESPONSE = 'Course not found.';

courseRouter.get('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');

if (id && userID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to, you could invert this statement

if(!id || !userID) { 
	throw ...
}

// do normal stuff here

// };

// TODO: call db operation
res.status(200); // + .json(semester)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the else statement here as well (or invert the if statement and throw)

res.send('Semester deleted');
if (id && userID) {
// TODO: call db operation
res.status(200);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the else statement here

}
if (!id || !userID) throw Error(`invalid ${id ? 'user' : 'course'} id`);

// TODO: call db operation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note for the create methods that we will want to make sure that we set the body.ownerId property to userID as we don't want people to be able to create items for other people.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants