Skip to content

Commit

Permalink
feat: added optional nullable support in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 committed Jun 26, 2024
1 parent 817211f commit 0a535c8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/test/http/apiLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Test APILogger with Request ConsoleLogging', () => {
});
it('should override log req default level', async () => {
const loggingOpts = mergeLoggingOptions({
logLevel: LogLevel.Debug,
logLevel: 'info',
});

const expectedConsoleLogs = [
Expand Down
5 changes: 4 additions & 1 deletion packages/schema/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { objectKeyEncode } from './utils';
import { isOptionalNullable, objectKeyEncode } from './utils';

/**
* Schema defines a type and its validation and mapping functions.
Expand Down Expand Up @@ -101,6 +101,9 @@ export function validateAndMap<T extends Schema<any, any>>(
);
const validationResult = schema.validateBeforeMap(value, contextCreator);
if (validationResult.length === 0) {
if (isOptionalNullable(schema.type())) {
return { errors: false, result: value };
}
return { errors: false, result: schema.map(value, contextCreator) };
} else {
return { errors: validationResult };
Expand Down
5 changes: 5 additions & 0 deletions packages/schema/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ export function objectKeyEncode(key: string): string {
export function isNullOrMissing(value: unknown): value is null | undefined {
return value === null || typeof value === 'undefined';
}

export function isOptionalNullable(type: string): boolean {
return type.startsWith('Optional<Nullable<') ||
type.startsWith('Nullable<Optional<');
}
16 changes: 15 additions & 1 deletion packages/schema/test/types/nullable.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nullable, string, validateAndMap, validateAndUnmap } from '../../src';
import { nullable, optional, string, validateAndMap, validateAndUnmap } from '../../src';

describe('Nullable', () => {
describe('Mapping', () => {
Expand All @@ -24,6 +24,20 @@ describe('Nullable', () => {
expect((output as any).result).toBeNull();
});

it('should accept null with nullable and optional', () => {
const schema = nullable(optional(string()));
const output = validateAndMap(null, schema);
expect(output.errors).toBeFalsy();
expect((output as any).result).toBeNull();
});

it('should accept undefined with nullable and optional', () => {
const schema = nullable(optional(string()));
const output = validateAndMap(undefined, schema);
expect(output.errors).toBeFalsy();
expect((output as any).result).toBeUndefined();
});

it('should fail on schema invalidation', () => {
const input = 123;
const schema = nullable(string());
Expand Down
16 changes: 15 additions & 1 deletion packages/schema/test/types/optional.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { optional, string, validateAndMap, validateAndUnmap } from '../../src';
import { nullable, optional, string, validateAndMap, validateAndUnmap } from '../../src';

describe('Optional', () => {
describe('Mapping', () => {
Expand All @@ -24,6 +24,20 @@ describe('Optional', () => {
expect((output as any).result).toBeUndefined();
});

it('should accept null with optional and nullable', () => {
const schema = optional(nullable(string()));
const output = validateAndMap(null, schema);
expect(output.errors).toBeFalsy();
expect((output as any).result).toBeNull();
});

it('should accept undefined with optional and nullable', () => {
const schema = optional(nullable(string()));
const output = validateAndMap(undefined, schema);
expect(output.errors).toBeFalsy();
expect((output as any).result).toBeUndefined();
});

it('should fail on schema invalidation', () => {
const input = 123;
const schema = optional(string());
Expand Down

0 comments on commit 0a535c8

Please sign in to comment.