-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add custom error message for minlength validation errors. (#212)
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const getByDataPath = function ({ object, dataPath }: { object: any; dataPath: string }): any { | ||
const pathSegments = dataPath.split('.'); | ||
const nonEmptyPathSegments = pathSegments.filter((segment): boolean => segment !== ''); | ||
|
||
let value = object; | ||
|
||
for (const pathSegment of nonEmptyPathSegments) { | ||
if (value === undefined) { | ||
break; | ||
} | ||
|
||
value = value[pathSegment]; | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export { getByDataPath }; |
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,34 @@ | ||
import { assert } from 'assertthat'; | ||
import { getByDataPath } from '../../lib/getByDataPath'; | ||
|
||
suite('getByDataPath', (): void => { | ||
test('traverses an object by following an ajv data path.', async (): Promise<void> => { | ||
const object = { | ||
foo: { | ||
bar: { | ||
baz: 'bam' | ||
} | ||
} | ||
}; | ||
|
||
const result = getByDataPath({ object, dataPath: '.foo.bar.baz' }); | ||
|
||
assert.that(result).is.equalTo('bam'); | ||
}); | ||
|
||
test('returns undefined, if the path does not match anything.', async (): Promise<void> => { | ||
const object = {}; | ||
|
||
const result = getByDataPath({ object, dataPath: '.foo.bar.baz' }); | ||
|
||
assert.that(result).is.undefined(); | ||
}); | ||
|
||
test('returns undefined, if the object is undefined.', async (): Promise<void> => { | ||
const object = undefined; | ||
|
||
const result = getByDataPath({ object, dataPath: '.foo.bar.baz' }); | ||
|
||
assert.that(result).is.undefined(); | ||
}); | ||
}); |