Skip to content

Commit

Permalink
fix: fix some confused TS types
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Sep 26, 2022
1 parent da4ab6b commit 52b0908
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 25 deletions.
15 changes: 7 additions & 8 deletions src/SimpleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
SchemaDefinition,
SchemaDefinitionWithShorthand,
SchemaKeyDefinition,
SchemaKeyTypeDefinition,
SchemaKeyTypeDefinitionWithShorthand,
SchemaKeyDefinitionWithOneType,
SimpleSchemaOptions,
StandardSchemaKeyDefinition,
StandardSchemaKeyDefinitionWithSimpleTypes,
Expand Down Expand Up @@ -331,7 +330,7 @@ class SimpleSchema {
// Resolve all the types and convert to a normal array to make it easier to use.
if (Array.isArray(defs.type?.definitions)) {
result.type = defs.type.definitions.map((typeDef) => {
const newTypeDef: (SchemaKeyTypeDefinition & { type: SupportedTypes }) = {
const newTypeDef: SchemaKeyDefinitionWithOneType = {
type: String // will be overwritten
}
Object.keys(typeDef).forEach(getPropIterator(typeDef, newTypeDef))
Expand Down Expand Up @@ -870,7 +869,7 @@ class SimpleSchema {
*/
get (
key: string,
prop: keyof StandardSchemaKeyDefinitionWithSimpleTypes | keyof SchemaKeyTypeDefinition,
prop: keyof StandardSchemaKeyDefinitionWithSimpleTypes | keyof StandardSchemaKeyDefinition,
functionContext?: Record<string, unknown>
): any {
const def = this.getDefinition(key, ['type', prop], functionContext)
Expand All @@ -884,7 +883,7 @@ class SimpleSchema {
const oneType = def.type[0]
if (oneType === SimpleSchema.Any) return undefined

return (oneType as SchemaKeyTypeDefinition)?.[prop as keyof SchemaKeyTypeDefinition]
return (oneType as SchemaKeyDefinitionWithOneType)?.[prop as keyof StandardSchemaKeyDefinition]
}

// shorthand for getting defaultValue
Expand Down Expand Up @@ -955,7 +954,7 @@ class SimpleSchema {
return (schema as SimpleSchema).validate(obj, options)
}

static oneOf (...definitions: SchemaKeyTypeDefinitionWithShorthand[]): SimpleSchemaGroup {
static oneOf (...definitions: Array<SchemaKeyDefinitionWithOneType | SupportedTypes | RegExpConstructor>): SimpleSchemaGroup {
return new SimpleSchemaGroup(...definitions)
}

Expand Down Expand Up @@ -1087,14 +1086,14 @@ function standardizeDefinition (def: SchemaKeyDefinition): StandardSchemaKeyDefi
if (def.type instanceof SimpleSchemaGroup) {
standardizedDef.type = def.type.clone()
} else {
const groupProps: Partial<SchemaKeyTypeDefinition> = {}
const groupProps: Partial<SchemaKeyDefinitionWithOneType> = {}
for (const prop of Object.keys(def)) {
if (oneOfProps.includes(prop)) {
// @ts-expect-error Copying properties
groupProps[prop] = def[prop]
}
}
standardizedDef.type = new SimpleSchemaGroup(groupProps as SchemaKeyTypeDefinition)
standardizedDef.type = new SimpleSchemaGroup(groupProps as SchemaKeyDefinitionWithOneType)
}

return standardizedDef as StandardSchemaKeyDefinition
Expand Down
8 changes: 4 additions & 4 deletions src/SimpleSchemaGroup.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import MongoObject from 'mongo-object'

import { SchemaKeyTypeDefinition, SchemaKeyTypeDefinitionWithShorthand, SupportedTypes } from './types.js'
import { SchemaKeyDefinitionWithOneType, SupportedTypes } from './types.js'

class SimpleSchemaGroup {
public definitions: SchemaKeyTypeDefinition[] = []
public definitions: SchemaKeyDefinitionWithOneType[] = []

constructor (...definitions: SchemaKeyTypeDefinitionWithShorthand[]) {
constructor (...definitions: Array<SchemaKeyDefinitionWithOneType | SupportedTypes | RegExpConstructor>) {
this.definitions = definitions.map((definition) => {
if (MongoObject.isBasicObject(definition)) {
return { ...definition as SchemaKeyTypeDefinition }
return { ...definition as SchemaKeyDefinitionWithOneType }
}

if (definition instanceof RegExp) {
Expand Down
7 changes: 3 additions & 4 deletions src/doValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
DocValidatorContext,
FieldInfo,
FunctionPropContext,
SchemaKeyTypeDefinition,
SchemaKeyDefinitionWithOneType,
StandardSchemaKeyDefinitionWithSimpleTypes,
SupportedTypes,
ValidationError,
ValidatorContext,
ValidatorFunction
Expand Down Expand Up @@ -191,14 +190,14 @@ function doValidation ({
// and add them to inner props like "type" and "min"
definition: {
...definitionWithoutType,
...typeDef as SchemaKeyTypeDefinition & { type: SupportedTypes }
...typeDef as SchemaKeyDefinitionWithOneType
}
}

// Add custom field validators to the list after the built-in
// validators but before the schema and global validators.
const fieldValidators = validators.slice(0)
const customFn = (typeDef as SchemaKeyTypeDefinition).custom
const customFn = (typeDef as SchemaKeyDefinitionWithOneType).custom
if (customFn != null) fieldValidators.splice(builtInValidators.length, 0, customFn)

// We use _.every just so that we don't continue running more validator
Expand Down
8 changes: 1 addition & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ export interface TypeDefinitionProps {
trim?: boolean
}

export interface SchemaKeyTypeDefinition extends TypeDefinitionProps {
type: SupportedTypes
}

export type SchemaKeyTypeDefinitionWithShorthand = SchemaKeyTypeDefinition | SupportedTypes | RegExpConstructor | SimpleSchemaGroup

export interface FunctionOptionContext {
key?: string | null
[prop: string]: unknown
Expand All @@ -150,7 +144,7 @@ export interface StandardSchemaKeyDefinition extends SchemaKeyDefinitionBase {
}

export interface StandardSchemaKeyDefinitionWithSimpleTypes extends SchemaKeyDefinitionBase {
type: Array<(SchemaKeyTypeDefinition & { type: SupportedTypes }) | '___Any___'>
type: Array<SchemaKeyDefinitionWithOneType | '___Any___'>
}

export type SchemaKeyDefinition = StandardSchemaKeyDefinition | SchemaKeyDefinitionWithOneType
Expand Down
4 changes: 2 additions & 2 deletions src/validation/typeValidator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SimpleSchema } from '../../SimpleSchema.js'
import { SchemaKeyTypeDefinition, TypeValidatorContext, ValidationErrorResult } from '../../types.js'
import { SchemaKeyDefinitionWithOneType, TypeValidatorContext, ValidationErrorResult } from '../../types.js'
import checkArrayValue from './checkArrayValue.js'
import checkDateValue from './checkDateValue.js'
import checkNumberValue from './checkNumberValue.js'
Expand Down Expand Up @@ -62,7 +62,7 @@ export function checkValueType (info: TypeValidatorContext): ValidationErrorResu
}
}

export function isValueTypeValid (typeDefinitions: SchemaKeyTypeDefinition[], value: any, operator: string | null): boolean {
export function isValueTypeValid (typeDefinitions: SchemaKeyDefinitionWithOneType[], value: any, operator: string | null): boolean {
return typeDefinitions.some((definition) => {
const typeValidationError = checkValueType({
valueShouldBeChecked: true,
Expand Down

0 comments on commit 52b0908

Please sign in to comment.