-
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.
Showing
11 changed files
with
152 additions
and
4 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,24 @@ | ||
import { InvalidTypeError, NotOptionalError } from '../errors/index.js'; | ||
import { converterFunct, RecordType } from '../interfaces/index.js'; | ||
import { recursiveConv } from './recursive-conv.js'; | ||
|
||
export const recordConv: converterFunct<RecordType> = (d, t, p) => { | ||
if (t == null) { | ||
if (!d.optional) { | ||
throw new NotOptionalError(p); | ||
} else { | ||
return undefined; | ||
} | ||
} else if (typeof t !== 'object') { | ||
throw new InvalidTypeError(d.type, p); | ||
} else { | ||
const resp: any = {}; | ||
for (const key of Object.keys(t)) { | ||
const path = [...p, key]; | ||
const item = recursiveConv(d.items, t[key], path); | ||
resp[key] = item; | ||
} | ||
|
||
return resp; | ||
} | ||
}; |
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
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,22 @@ | ||
import { BaseType } from './base-type.js'; | ||
import { NumberType } from './number-type.js'; | ||
import { StringType } from './string-type.js'; | ||
import { BooleanType } from './boolean-type.js'; | ||
import { ObjectType } from './object-type.js'; | ||
import { ArrayType } from './array-type.js'; | ||
|
||
export interface RecordType extends BaseType<'record'> { | ||
/** | ||
* With this option you can specify the structure of every | ||
* item stored in the record, using the same options described | ||
* in the past types described. __You can declare nested arrays, | ||
* or object arrays too.__ | ||
*/ | ||
items: | ||
ArrayType | | ||
RecordType | | ||
NumberType | | ||
StringType | | ||
ObjectType | | ||
BooleanType; | ||
} |
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,58 @@ | ||
import test from 'ava'; | ||
|
||
import { Auditor } from '../auditor.js'; | ||
import { InvalidTypeError } from '../errors/index.js'; | ||
|
||
test('Test Record<string, string>', t => { | ||
const targetOk = { | ||
foo: 'bar', | ||
goo: 'baz' | ||
}; | ||
|
||
const targetEr = { | ||
foo: 'bar', | ||
goo: 777 | ||
}; | ||
|
||
const auditor = new Auditor({ | ||
type: 'record', | ||
items: { type: 'string' } | ||
}); | ||
|
||
const result = auditor.audit(targetOk); | ||
t.deepEqual(result, targetOk); | ||
t.throws( | ||
() => { auditor.audit(targetEr) }, | ||
{ instanceOf: InvalidTypeError } | ||
); | ||
}); | ||
|
||
test('Test Record<string, { id: number; nick: string; }>', t => { | ||
const targetOk = { | ||
joder: { id: 666, nick: 'ghostlug' }, | ||
shavo: { id: 666, nick: 'dir en grey' }, | ||
}; | ||
|
||
const targetEr = { | ||
joder: { id: 'lol', nick: 'ghostlug' }, | ||
shavo: { id: 666, nick: 'dir en grey' }, | ||
}; | ||
|
||
const auditor = new Auditor({ | ||
type: 'record', | ||
items: { | ||
type: 'object', | ||
keys: { | ||
id: { type: 'number' }, | ||
nick: { type: 'string' } | ||
} | ||
} | ||
}); | ||
|
||
const resp = auditor.audit(targetOk); | ||
t.deepEqual(resp, targetOk); | ||
t.throws( | ||
() => { auditor.audit(targetEr) }, | ||
{ instanceOf: InvalidTypeError } | ||
); | ||
}); |