-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
240 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {empty} from '../src/empty'; | ||
const EMPTY_ARRAY: string[] = []; | ||
|
||
describe('empty', () => { | ||
it(`should return false when value is undefined`, () => { | ||
expect(empty<any>(undefined)).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is null`, () => { | ||
expect(empty<any>(null)).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a non-empty array`, () => { | ||
expect(empty<any>([11, 'aa'])).toBe(false); | ||
}); | ||
|
||
it(`should return true when value is an empty array`, () => { | ||
expect(empty<any>(EMPTY_ARRAY)).toBe(true); | ||
}); | ||
|
||
it(`should return true when value is an empty object`, () => { | ||
expect(empty<any>({})).toBe(true); | ||
}); | ||
|
||
it(`should return false when value is a non-empty string`, () => { | ||
expect(empty<any>('aaaaaa')).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a non-empty string`, () => { | ||
expect(empty<any>('aaaaaa')).toBe(false); | ||
}); | ||
|
||
it(`should return true when value is a space-padded empty string`, () => { | ||
let value = ' '; | ||
|
||
for (let i = 0; i < 5; i++) { | ||
expect(empty<any>(value)).toBe(true); | ||
value += ' '; | ||
} | ||
}); | ||
|
||
it(`should return true when value is an non-empty object`, () => { | ||
expect( | ||
empty<any>({ | ||
one: 'aaaaa' | ||
}) | ||
).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a boolean (true)`, () => { | ||
expect(empty<boolean>(true)).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a boolean (true)`, () => { | ||
expect(empty<boolean>(false)).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a falsy number (0)`, () => { | ||
expect(empty<number>(0)).toBe(false); | ||
}); | ||
|
||
it(`should return false when value is a truthy number`, () => { | ||
expect(empty<number>(1)).toBe(false); | ||
expect(empty<number>(10000)).toBe(false); | ||
expect(empty<number>(-1)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import {SchemaFieldType} from '../../../../src'; | ||
import {schemaFieldArraysAllowed} from '../../../../src/schema/field/arrays/allowed'; | ||
import {schemaFieldTypes} from '../../../../src/schema/field/types'; | ||
|
||
const EMPTY_STRING = ''; | ||
const EMPTY_OBJECT = {}; | ||
const EMPTY_STR_ARRAY: string[] = []; | ||
|
||
const NON_ARRAY_TYPES: SchemaFieldType[] = [ | ||
'bigint', | ||
'BigInt', | ||
'boolean', | ||
'datetime', | ||
'dbl', | ||
'double', | ||
'float', | ||
'int', | ||
'iterable', | ||
'json', | ||
'null', | ||
'number', | ||
'serialized', | ||
'string', | ||
'time', | ||
'uint', | ||
'undefined', | ||
'url' | ||
]; | ||
|
||
const ARRAY_TYPES: SchemaFieldType<string>[] = [ | ||
'bigint[]', | ||
'BigInt[]', | ||
'boolean[]', | ||
'datetime[]', | ||
'datetime[]', | ||
'dbl[]', | ||
'double[]', | ||
'float[]', | ||
'int[]', | ||
'iterable[]', | ||
'json[]', | ||
'null[]', | ||
'number[]', | ||
'serialized[]', | ||
'string[]', | ||
'time[]', | ||
'undefined[]', | ||
'url[]' | ||
]; | ||
|
||
describe('schemaFieldArraysAllowed', () => { | ||
it(`should return false when items arg is an empty string`, () => { | ||
expect(schemaFieldArraysAllowed<string>(EMPTY_STRING as any)).toBe(false); | ||
}); | ||
|
||
it(`should return false when items arg is a string`, () => { | ||
expect(schemaFieldArraysAllowed<string>('f087145081-998132' as any)).toBe(false); | ||
}); | ||
|
||
it(`should return false when items arg is an empty array`, () => { | ||
expect(schemaFieldArraysAllowed<string>(EMPTY_STR_ARRAY as any)).toBe(false); | ||
}); | ||
|
||
it(`should return false when items arg is an empty object`, () => { | ||
expect(schemaFieldArraysAllowed<string>(EMPTY_OBJECT as any)).toBe(false); | ||
}); | ||
|
||
it(`should return false for all built-in non-array types'`, () => { | ||
expect(schemaFieldArraysAllowed<string>(NON_ARRAY_TYPES)).toBe(false); | ||
}); | ||
|
||
it(`should return true when one item is an array type among non-array types`, () => { | ||
const input: SchemaFieldType<string>[] = [...NON_ARRAY_TYPES, 'number[]']; | ||
expect(schemaFieldArraysAllowed<string>(input)).toBe(true); | ||
}); | ||
|
||
it(`should return true when items array has one array type`, () => { | ||
expect(schemaFieldArraysAllowed<string>(['BigInt[]'])).toBe(true); | ||
}); | ||
|
||
for (const fieldType of NON_ARRAY_TYPES) { | ||
it(`should return false when items arg is a single non-array type '${fieldType}'`, () => { | ||
expect(schemaFieldArraysAllowed<string>([fieldType])).toBe(false); | ||
}); | ||
} | ||
|
||
for (const fieldType of ARRAY_TYPES) { | ||
it(`should return true when items array has one array type '${fieldType}'`, () => { | ||
expect(schemaFieldArraysAllowed<string>([fieldType])).toBe(true); | ||
}); | ||
} | ||
|
||
it(`should return true when the last item is an array type and all other elements are skipped due to being non-strings`, () => { | ||
expect( | ||
schemaFieldArraysAllowed<string>([ | ||
undefined, | ||
null, | ||
1000, | ||
{}, | ||
[], | ||
'___', | ||
EMPTY_STRING, | ||
'BigInt[]' | ||
] as any) | ||
).toBe(true); | ||
}); | ||
|
||
it(`should return true when items arg contains a non-builtin type ending in '[]'`, () => { | ||
expect( | ||
schemaFieldArraysAllowed<string>([ | ||
null, | ||
undefined, | ||
10, | ||
100, | ||
91910, | ||
'---', | ||
'string', | ||
'undefined', | ||
'customtype[]' | ||
] as any) | ||
).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {Primitive} from '@toreda/types'; | ||
import {transformVerified} from '../../src/transform/verified'; | ||
import {schemaError} from '../../src'; | ||
import {Levels, Log} from '@toreda/log'; | ||
|
||
describe('transformVerified', () => { | ||
let verifiedMap: Map<string, string>; | ||
let base: Log; | ||
|
||
beforeAll(() => { | ||
verifiedMap = new Map<string, string>(); | ||
base = new Log({ | ||
consoleEnabled: true, | ||
globalLevel: Levels.ALL, | ||
groupsStartEnabled: true | ||
}); | ||
}); | ||
|
||
beforeEach(() => { | ||
verifiedMap.clear(); | ||
}); | ||
|
||
describe('Base parameters', () => { | ||
it(`should fail with code when base arg is undefined`, async () => { | ||
const result = await transformVerified<Primitive, any>(verifiedMap, undefined as any); | ||
expect(result.ok()).toBe(false); | ||
|
||
expect(result.errorCode()).toBe(schemaError('missing_argument', 'transformVerified', 'base')); | ||
}); | ||
|
||
it(`should fail with code when base arg is null`, async () => { | ||
const result = await transformVerified<Primitive, any>(verifiedMap, null as any); | ||
expect(result.ok()).toBe(false); | ||
|
||
expect(result.errorCode()).toBe(schemaError('missing_argument', 'transformVerified', 'base')); | ||
}); | ||
|
||
it(`should succeed when input is an empty map`, async () => { | ||
expect(verifiedMap.size).toBe(0); | ||
|
||
const result = await transformVerified<Primitive, any>(verifiedMap, base); | ||
expect(result.ok()).toBe(true); | ||
expect(result.data).toStrictEqual({}); | ||
}); | ||
}); | ||
}); |