-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
7bc1d20
commit 0ea90d3
Showing
7 changed files
with
124 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ArraySchema } from './ArraySchema'; | ||
import { NumberSchema } from './NumberSchema'; | ||
|
||
describe('Array Schema', () => { | ||
it('should throw error when there is an error in object keys', () => { | ||
const schema = new ArraySchema(new NumberSchema()).setValue('3000'); | ||
schema.key = 'key'; | ||
|
||
expect(() => schema.validate()).toThrow( | ||
new TypeError( | ||
'Invalid configuration: The "key" expected to be "array" but a "string" was provided', | ||
), | ||
); | ||
}); | ||
|
||
it('should not throw with empty array', () => { | ||
const schema = new ArraySchema(new NumberSchema()).setValue([]); | ||
schema.key = 'key'; | ||
|
||
expect(() => schema.validate()).not.toThrow(); | ||
}); | ||
|
||
it('should not throw for valid values', () => { | ||
const schema = new ArraySchema(new NumberSchema({ coerce: true })).setValue( | ||
[3000, '3000'], | ||
); | ||
schema.key = 'key'; | ||
|
||
expect(() => schema.validate()).not.toThrow(); | ||
}); | ||
|
||
it('should not accept "undefined" or "null"', () => { | ||
const schema = new ArraySchema(new NumberSchema({ coerce: true })).setValue( | ||
[undefined], | ||
); | ||
schema.key = 'key'; | ||
|
||
const schema2 = new ArraySchema( | ||
new NumberSchema({ coerce: true }), | ||
).setValue([null]); | ||
schema2.key = 'key'; | ||
|
||
expect(() => schema.validate()).toThrow( | ||
new Error( | ||
'Invalid configuration: The "key.0" is required but the given value is "undefined"', | ||
), | ||
); | ||
expect(() => schema2.validate()).toThrow( | ||
new Error( | ||
'Invalid configuration: The "key.0" is required but the given value is "null"', | ||
), | ||
); | ||
}); | ||
|
||
it('should validate values', () => { | ||
const schema = new ArraySchema( | ||
new NumberSchema({ coerce: false }), | ||
).setValue([3000, 'string']); | ||
schema.key = 'key'; | ||
|
||
expect(() => schema.validate()).toThrow( | ||
new Error( | ||
'Invalid configuration: The "key.1" expected to be "number" but a "string" was provided', | ||
), | ||
); | ||
}); | ||
}); |
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,32 @@ | ||
import type { Guard } from '../Guard'; | ||
import { Schema } from './Schema'; | ||
import { TypeGuard } from './TypeGuard'; | ||
|
||
class ArrayGuard implements Guard<any[]> { | ||
constructor(private schema: Schema<any, any, boolean>) {} | ||
|
||
validate(input: unknown[], key: string) { | ||
input.forEach((v, i) => { | ||
this.schema.key = `${key}.${i}`; | ||
this.schema.setValue(v); | ||
this.schema.validate(); | ||
}); | ||
} | ||
} | ||
|
||
export class ArraySchema<TInput = any> extends Schema<TInput, TInput, boolean> { | ||
#type = 'array'; // eslint-disable-line no-unused-private-class-members | ||
|
||
constructor(schema: Schema<any, any, boolean>) { | ||
super({ | ||
typeConstructor: x => x, | ||
initialGuards: [ | ||
new TypeGuard('array', x => Array.isArray(x)), | ||
new ArrayGuard(schema), | ||
], | ||
}); | ||
schema.options.coerce ??= false; | ||
schema.required(); | ||
this.required(); | ||
} | ||
} |
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