-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): add data validation to insert and update queries
- Add Utils.validator to common package - Add data validation to insert and update graphql queries - Add automated tests for data validation - Add automated tests for insert and update query validations
- Loading branch information
Frantz Kati
committed
Nov 27, 2020
1 parent
952d48c
commit cc4d7db
Showing
15 changed files
with
560 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import * as validator from 'indicative/validator' | ||
import { EntityManager, ReferenceType } from '@mikro-orm/core' | ||
import { DataPayload, ResourceContract } from '@tensei/common' | ||
|
||
export class Validator { | ||
constructor( | ||
private resource: ResourceContract, | ||
private manager: EntityManager, | ||
private resourcesMap: { [key: string]: ResourceContract }, | ||
private modelId?: string | number | ||
) { | ||
let self = this | ||
|
||
validator.extend('unique', { | ||
async: true, | ||
async validate(data, field, args) { | ||
const whereOptions: any = { | ||
[args[0]]: data.original[args[0]] | ||
} | ||
|
||
if (args[1] && self.modelId) { | ||
whereOptions.id = { | ||
$ne: self.modelId | ||
} | ||
} | ||
|
||
const count = await self.manager.count( | ||
self.resource.data.pascalCaseName, | ||
whereOptions | ||
) | ||
|
||
return count === 0 | ||
} | ||
}) | ||
} | ||
|
||
getValidationRules = (creationRules = true) => { | ||
const fields = this.resource.data.fields.filter(field => | ||
creationRules | ||
? field.showHideField.showOnCreation | ||
: field.showHideField.showOnUpdate | ||
) | ||
|
||
const rules: { | ||
[key: string]: string | ||
} = {} | ||
|
||
fields.forEach(field => { | ||
const fieldValidationRules = Array.from( | ||
new Set([ | ||
...field.validationRules, | ||
...field[ | ||
creationRules | ||
? 'creationValidationRules' | ||
: 'updateValidationRules' | ||
] | ||
]) | ||
).join('|') | ||
|
||
if (field.relatedProperty.reference) { | ||
const relatedResource = this.resourcesMap[ | ||
field.relatedProperty.type! | ||
] | ||
|
||
const primaryFieldType = | ||
relatedResource.getPrimaryField()!.property.type === | ||
'number' | ||
? 'number' | ||
: 'string' | ||
|
||
if ( | ||
[ | ||
ReferenceType.MANY_TO_MANY, | ||
ReferenceType.ONE_TO_MANY | ||
].includes(field.relatedProperty.reference!) | ||
) { | ||
rules[field.databaseField] = 'array' | ||
rules[`${field.databaseField}.*`] = primaryFieldType | ||
} else { | ||
rules[field.databaseField] = primaryFieldType | ||
} | ||
} | ||
|
||
if (fieldValidationRules) { | ||
rules[field.databaseField] = fieldValidationRules | ||
} | ||
}) | ||
|
||
return rules | ||
} | ||
|
||
getResourceFieldsFromPayload = (payload: DataPayload) => { | ||
let validPayload: DataPayload = {} | ||
|
||
this.resource.data.fields.forEach(field => { | ||
if (Object.keys(payload).includes(field.databaseField)) { | ||
validPayload[field.databaseField] = payload[field.databaseField] | ||
} | ||
}) | ||
|
||
return validPayload | ||
} | ||
|
||
breakFieldsIntoRelationshipsAndNonRelationships = ( | ||
payload: DataPayload | ||
) => { | ||
const relationshipFieldsPayload: DataPayload = {} | ||
const nonRelationshipFieldsPayload: DataPayload = {} | ||
|
||
this.resource.data.fields.forEach(field => { | ||
if (Object.keys(payload).includes(field.databaseField)) { | ||
if (field.relatedProperty.reference) { | ||
relationshipFieldsPayload[field.databaseField] = | ||
payload[field.databaseField] | ||
} else { | ||
nonRelationshipFieldsPayload[field.databaseField] = | ||
payload[field.databaseField] | ||
} | ||
} | ||
}) | ||
|
||
return { | ||
relationshipFieldsPayload, | ||
nonRelationshipFieldsPayload | ||
} | ||
} | ||
|
||
validate = async ( | ||
payload: DataPayload, | ||
creationRules: boolean = true, | ||
modelId?: string | number | ||
): Promise<DataPayload> => { | ||
try { | ||
const parsedPayload: DataPayload = await validator.validateAll( | ||
this.getResourceFieldsFromPayload(payload), | ||
this.getValidationRules(creationRules), | ||
this.resource.data.validationMessages | ||
) | ||
|
||
return [true, parsedPayload] | ||
} catch (errors) { | ||
return [false, errors] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Validator } from './Validator' | ||
import { EntityManager } from '@mikro-orm/core' | ||
import { ResourceContract } from '@tensei/core' | ||
|
||
export const Utils = { | ||
validator: ( | ||
resource: ResourceContract, | ||
manager: EntityManager, | ||
resourcesMap: { | ||
[key: string]: ResourceContract | ||
}, | ||
modelId?: string | number | ||
) => new Validator(resource, manager, resourcesMap, modelId) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../../../helpers' |
Oops, something went wrong.