Skip to content

Commit

Permalink
standardize data type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskalmar committed Jan 30, 2021
1 parent f2c81f6 commit bbddf98
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
22 changes: 13 additions & 9 deletions src/engine/datatype/DataType.ts
@@ -1,12 +1,18 @@
import { Source } from 'graphql';
import { Entity, ShadowEntity, ViewEntity } from '../..';
import { Context } from '../context/Context';
import { passOrThrow, isFunction } from '../util';
import { ComplexDataType } from './ComplexDataType';

export type DataTypeSetup = {
name?: string;
description?: string;
mock?: () => any;
validate?: () => any;
validate?: (params: {
value?: unknown;
source?: Source;
context?: Context;
}) => void | Promise<void>;
enforceRequired?: boolean;
defaultValue?: () => any;
enforceIndex?: boolean;
Expand All @@ -21,7 +27,11 @@ export class DataType {
name: string;
description: string;
mock?: () => any;
validator?: (value: any, context: any) => any;
validate?: (params: {
value?: unknown;
source?: Source;
context?: Context;
}) => void | Promise<void>;
enforceRequired?: boolean;
defaultValue?: () => any;
enforceIndex?: boolean;
Expand Down Expand Up @@ -54,7 +64,7 @@ export class DataType {
() => `'Invalid validate function for data type '${name}'`,
);

this.validator = validate;
this.validate = validate;
}

if (defaultValue) {
Expand All @@ -79,12 +89,6 @@ export class DataType {
}
}

validate = async (value: any, context: any): Promise<void> => {
if (value && this.validator) {
await this.validator(value, context);
}
};

toString(): string {
return this.name;
}
Expand Down
60 changes: 44 additions & 16 deletions src/engine/validation.ts
@@ -1,24 +1,35 @@
import * as _ from 'lodash';
import { isObjectDataType } from './datatype/ObjectDataType';
import { isObjectDataType, ObjectDataType } from './datatype/ObjectDataType';
import { isListDataType } from './datatype/ListDataType';
import { isComplexDataType } from './datatype/ComplexDataType';
import { isMap, passOrThrow, isDefined, asyncForEach } from './util';
import { MUTATION_TYPE_CREATE, Mutation } from './mutation/Mutation';
import { Action } from './action/Action';
import { Entity } from '..';
import { DataType, Entity } from '..';
import { Attribute } from './attribute/Attribute';
import { Context } from './context/Context';
import { Source } from 'graphql';
import { isDataType } from './datatype/DataType';
// import { Attribute } from './attribute/Attribute';

const validateDataTypePayload = async (
paramType: any,
payload: any,
context?: Context,
): Promise<void> => {
const dataTypeValidator = paramType.validate;

if (dataTypeValidator) {
await dataTypeValidator(payload, context);
const validateDataTypePayload = async ({
dataType,
payload,
context,
source,
}: {
dataType: DataType | ObjectDataType;
payload: any;
context?: Context;
source: Source;
}): Promise<void> => {
if (isDataType(dataType)) {
const dataTypeValidator = dataType.validate;

if (dataTypeValidator) {
console.log(`==========> ${dataType.name}`);
await dataTypeValidator({ value: payload, context, source });
}
}
};

Expand All @@ -36,7 +47,12 @@ const validatePayload = async (
? param.type.getItemType()
: param.type;

await validateDataTypePayload(param.type, payload[paramName], context);
await validateDataTypePayload({
dataType: param.type,
payload: payload[paramName],
context,
source,
});

if (isObjectDataType(paramType)) {
const attributes = paramType.getAttributes();
Expand All @@ -63,7 +79,12 @@ const validatePayload = async (
await Promise.all(
payloadList.map(async (itemPayload) => {
if (isObjectDataType(paramType)) {
await validateDataTypePayload(paramType, itemPayload, context);
await validateDataTypePayload({
dataType: paramType,
payload: itemPayload,
context,
source,
});

const attributes = paramType.getAttributes();
const pathString = path.length ? `${path.join('.')}.` : '';
Expand Down Expand Up @@ -96,10 +117,17 @@ const validatePayload = async (
}

if (!isComplexDataType(paramType)) {
const attributeName = param.name;
const attributeValidator = param.validate;
const attribute: Attribute = param;

const attributeName = attribute.name;
const attributeValidator = attribute.validate;

await validateDataTypePayload(paramType, payload[attributeName], context);
await validateDataTypePayload({
dataType: paramType,
payload: payload[attributeName],
context,
source,
});

if (attributeValidator) {
if (
Expand Down

0 comments on commit bbddf98

Please sign in to comment.