Skip to content

Commit

Permalink
signed-off: added stricter scalar type checks + build
Browse files Browse the repository at this point in the history
Signed-off-by: Ifeora Okechukwu <[email protected]>
  • Loading branch information
Ifeora Okechukwu committed May 9, 2020
1 parent 1439c42 commit d366d01
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 35 deletions.
5 changes: 3 additions & 2 deletions packages/openapi-to-graphql/lib/schema_builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { PreprocessingData } from './types/preprocessing_data';
import { Operation, DataDefinition } from './types/operation';
import { ParameterObject } from './types/oas3';
import { SchemaObject, ParameterObject } from './types/oas3';
import { Args, GraphQLType } from './types/graphql';
declare type GetArgsParams = {
requestPayloadDef?: DataDefinition;
Expand All @@ -13,6 +13,7 @@ declare type GetArgsParams = {
};
declare type CreateOrReuseComplexTypeParams = {
def: DataDefinition;
schema?: SchemaObject;
operation?: Operation;
iteration?: number;
isInputObjectType?: boolean;
Expand All @@ -21,7 +22,7 @@ declare type CreateOrReuseComplexTypeParams = {
/**
* Creates and returns a GraphQL type for the given JSON schema.
*/
export declare function getGraphQLType({ def, operation, data, iteration, isInputObjectType }: CreateOrReuseComplexTypeParams): GraphQLType;
export declare function getGraphQLType({ def, schema, operation, data, iteration, isInputObjectType }: CreateOrReuseComplexTypeParams): GraphQLType;
/**
* Creates the arguments for resolving a field
*/
Expand Down
67 changes: 61 additions & 6 deletions packages/openapi-to-graphql/lib/schema_builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/schema_builder.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions packages/openapi-to-graphql/lib/types/oas3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ declare type ExternalDocumentationObject = {
export declare type SchemaObject = {
$ref?: string;
title?: string;
minimum?: number;
maximum?: number;
maxLength?: number;
minLength?: number;
pattern?: string;
type?: 'string' | 'number' | 'object' | 'array' | 'boolean' | 'integer';
format?: string;
nullable?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/openapi-to-graphql/lib/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export declare const mitigations: {
LIMIT_ARGUMENT_NAME_COLLISION: string;
OAUTH_SECURITY_SCHEME: string;
};
/**
* get the correct type of a variable
*/
export declare function strictTypeOf(value: any, type: any): boolean;
/**
* Utilities that are specific to OpenAPI-to-GraphQL
*/
Expand Down
48 changes: 48 additions & 0 deletions packages/openapi-to-graphql/lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 25 additions & 25 deletions packages/openapi-to-graphql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,54 +55,54 @@ export const mitigations = {
* check if a literal is falsy or not
*/
const isLiteralFalsey = (variable): boolean => {
return (variable === "" || variable === false || variable === 0)
return variable === '' || variable === false || variable === 0
}

/**
* provide the name of primitive and/or reference types
*/
const checkTypeName = (target, type): boolean => {
let typeName = ""
let typeName = ''

if(isLiteralFalsey(target)){
typeName = (typeof target)
}else{
typeName = ("" + (target && target.constructor.name))
if (isLiteralFalsey(target)) {
typeName = typeof target
} else {
typeName = '' + (target && target.constructor.name)
}
return !!(typeName.toLowerCase().indexOf(type) + 1)
}

/**
* get the correct type of a variable
*/
export function strictTypeOf (value, type): boolean {
export function strictTypeOf(value, type): boolean {
let result = false

type = type || []

if(typeof type === 'object'){
if(typeof type.length !== 'number'){
return result
}

let bitPiece = 0
if (typeof type === 'object') {
if (typeof type.length !== 'number') {
return result
}

type = [].slice.call(type)
let bitPiece = 0

type.forEach( _type => {
if(typeof _type === 'function'){
_type = (_type.name || _type.displayName).toLowerCase()
}
bitPiece |= Number(checkTypeName(value, _type))
});
type = [].slice.call(type)

result = Boolean(bitPiece)
}else{
if(typeof type === 'function'){
type = (type.name || type.displayName).toLowerCase()
type.forEach(_type => {
if (typeof _type === 'function') {
_type = (_type.name || _type.displayName).toLowerCase()
}
bitPiece |= Number(checkTypeName(value, _type))
})

result = Boolean(bitPiece)
} else {
if (typeof type === 'function') {
type = (type.name || type.displayName).toLowerCase()
}

result = checkTypeName(value, type)
result = checkTypeName(value, type)
}
return result
}
Expand Down

0 comments on commit d366d01

Please sign in to comment.