mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Refactoring: Put all data validations in one place
@ulfgebhardt @mattwr18 @tirokk Here's how I imagined the data validation middleware. If we roll our own input validations I would suggest to put them all in one place. @ulfgebhardt this commit is a great example of how tests can speed you up: Since I can rely on existing tests, I don't have to check the validations manually. With tests you can refactor with confidence! 👍
This commit is contained in:
parent
303df6b869
commit
47d7c615a5
@ -10,14 +10,14 @@ import permissionsMiddleware from './permissionsMiddleware'
|
||||
import userMiddleware from './userMiddleware'
|
||||
import includedFieldsMiddleware from './includedFieldsMiddleware'
|
||||
import orderByMiddleware from './orderByMiddleware'
|
||||
import validUrlMiddleware from './validUrlMiddleware'
|
||||
import validationMiddleware from './validation'
|
||||
import notificationsMiddleware from './notifications'
|
||||
|
||||
export default schema => {
|
||||
let middleware = [
|
||||
passwordMiddleware,
|
||||
dateTimeMiddleware,
|
||||
validUrlMiddleware,
|
||||
validationMiddleware,
|
||||
sluggifyMiddleware,
|
||||
excerptMiddleware,
|
||||
notificationsMiddleware,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import dotenv from 'dotenv'
|
||||
import { UserInputError } from 'apollo-server'
|
||||
|
||||
import createOrUpdateLocations from './nodes/locations'
|
||||
|
||||
@ -13,13 +12,9 @@ export default {
|
||||
return result
|
||||
},
|
||||
UpdateUser: async (resolve, root, args, context, info) => {
|
||||
const USERNAME_MIN_LENGTH = 3 // TODO move to the correct place
|
||||
if (!args.name || args.name.length < USERNAME_MIN_LENGTH) {
|
||||
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} characters long!`)
|
||||
}
|
||||
const result = await resolve(root, args, context, info)
|
||||
await createOrUpdateLocations(args.id, args.locationName, context.driver)
|
||||
return result
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +71,8 @@ describe('userMiddleware', () => {
|
||||
|
||||
it('with too short name', async () => {
|
||||
const variables = {
|
||||
id: 'u1'
|
||||
id: 'u1',
|
||||
name: ' '
|
||||
}
|
||||
const expected = 'Username must be at least 3 characters long!'
|
||||
await expect(client.request(mutation, variables))
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
const validURL = str => {
|
||||
const isValid = str.match(/^(?:https?:\/\/)(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
|
||||
return !!isValid
|
||||
}
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
CreateSocialMedia: async (resolve, root, args, context, info) => {
|
||||
let socialMedia
|
||||
if (validURL(args.url)) {
|
||||
socialMedia = await resolve(root, args, context, info)
|
||||
} else {
|
||||
throw Error('Input is not a URL')
|
||||
}
|
||||
return socialMedia
|
||||
}
|
||||
}
|
||||
}
|
||||
30
backend/src/middleware/validation/index.js
Normal file
30
backend/src/middleware/validation/index.js
Normal file
@ -0,0 +1,30 @@
|
||||
import { UserInputError } from 'apollo-server'
|
||||
|
||||
const USERNAME_MIN_LENGTH = 3
|
||||
|
||||
const validateUsername = async (resolve, root, args, context, info) => {
|
||||
if (args.name && args.name.length >= USERNAME_MIN_LENGTH) {
|
||||
return await resolve(root, args, context, info)
|
||||
} else {
|
||||
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} characters long!`)
|
||||
}
|
||||
}
|
||||
|
||||
const validateUrl = async (resolve, root, args, context, info) => {
|
||||
const { url } = args
|
||||
const isValid = url.match(/^(?:https?:\/\/)(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
|
||||
if (isValid) {
|
||||
return await resolve(root, args, context, info)
|
||||
} else {
|
||||
throw new UserInputError('Input is not a URL')
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
// CreateUser: validateUsername,
|
||||
UpdateUser: validateUsername,
|
||||
CreateSocialMedia: validateUrl
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user