Skip to content

Commit

Permalink
refactor: resolved PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 committed Jul 18, 2024
1 parent 7d7a6a1 commit 3b120ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/schema/src/types/discriminatedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function discriminatedObject<
const selectSchemaWithDisc = (
value: unknown,
discriminatorProp: string | TDiscrimProp | TDiscrimMappedProp,
isAttr: boolean
isAttr?: boolean
) => {
if (
typeof value === 'object' &&
Expand Down Expand Up @@ -51,16 +51,16 @@ export function discriminatedObject<
const selectSchema = (
value: unknown,
discriminatorProp: string | TDiscrimProp | TDiscrimMappedProp,
checker: (schema: TSchema) => SchemaValidationError[],
isAttr: boolean = false
validater: (schema: TSchema) => SchemaValidationError[],
isAttr?: boolean
) => {
const schema = selectSchemaWithDisc(value, discriminatorProp, isAttr);
if (typeof schema !== 'undefined') {
return schema;
}
// Try checking with discriminator matching
for (const key in allSchemas) {
if (checker(allSchemas[key]).length === 0) {
if (validater(allSchemas[key]).length === 0) {
return allSchemas[key];
}
}
Expand Down
6 changes: 2 additions & 4 deletions packages/schema/src/types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OptionalizeObject } from '../typeUtils';
import {
isOptional,
isOptionalNullable,
isOptionalOrNullableType,
literalToString,
objectEntries,
objectKeyEncode,
Expand Down Expand Up @@ -377,10 +378,7 @@ function validateValueObject({
ctxt.createChild(propTypePrefix + key, valueObject[key], schema)
)
);
} else if (
!schema.type().startsWith('Optional<') &&
!schema.type().startsWith('Nullable<')
) {
} else if (!isOptionalOrNullableType(schema.type())) {
// Add to missing keys if it is not an optional property
missingProps.add(key);
}
Expand Down
13 changes: 10 additions & 3 deletions packages/schema/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,16 @@ export function isOptional(type: string, value: unknown): boolean {
}

export function isOptionalNullable(type: string, value: unknown): boolean {
return isOptionalAndNullableType(type) && isNullOrMissing(value);
}

export function isOptionalAndNullableType(type: string): boolean {
return (
(type.startsWith('Optional<Nullable<') ||
type.startsWith('Nullable<Optional<')) &&
isNullOrMissing(value)
type.startsWith('Optional<Nullable<') ||
type.startsWith('Nullable<Optional<')
);
}

export function isOptionalOrNullableType(type: string): boolean {
return type.startsWith('Optional<') || type.startsWith('Nullable<');
}

0 comments on commit 3b120ab

Please sign in to comment.