-
Notifications
You must be signed in to change notification settings - Fork 1
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
17 changed files
with
1,007 additions
and
68 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,43 @@ | ||
// node -r esm example/test3.js | ||
import Type, { string, Self } from '../src/type.js' | ||
// const Person = new Type({ | ||
// name: string, | ||
// friend: Self, | ||
// }); | ||
// const me = { | ||
// name: 'Peter', | ||
// friend: { | ||
// name: 'Bod', | ||
// friend: { | ||
// name: 'John', | ||
// } | ||
// } | ||
// }; | ||
// const errors = Person.validate(me); | ||
// console.log(Person.schema); | ||
// console.log(errors); | ||
|
||
const Person = new Type({ | ||
name: string, | ||
friends: [Self], | ||
}); | ||
const me = { | ||
name: 'Peter', | ||
friends: [ | ||
{ | ||
name: 'Bod', | ||
}, | ||
{ | ||
name: 'Ana', | ||
friends: [ | ||
{ | ||
name: 'John', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const errors = Person.validate(me); | ||
console.log(Person.schema); | ||
console.log(errors); |
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 @@ | ||
// node -r esm example/test4.js | ||
// test class | ||
import Type, { promise } from '../src/type.js'; | ||
|
||
class Person { | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
}; | ||
|
||
const PersonType = new Type({ | ||
bestFriend: Object, | ||
age: Number, | ||
myFriends: [Person], | ||
}); | ||
|
||
const me = { | ||
bestFriend: new Person('Hana'), // instanceof class Object | ||
age: new Number(123), // instanceof class Number | ||
myFriends: [new Person('John'), new Person('Peter')], | ||
}; | ||
|
||
const errors = PersonType.validate(me); // no errors | ||
console.log(errors); |
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,29 @@ | ||
import { | ||
Type, | ||
isArray, | ||
} from '../src/type'; | ||
|
||
it('Test array', () => { | ||
class Person { | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
const NewType = new Type({ | ||
bestFriend: Object, | ||
age: Number, | ||
myFriends: [Person], | ||
}); | ||
|
||
const me = { | ||
bestFriend: new Person('Hana'), | ||
// eslint-disable-next-line no-new-wrappers | ||
age: new Number(123), | ||
myFriends: [new Person('John'), new Person('Peter')], | ||
}; | ||
|
||
const errors = NewType.validate(me); | ||
expect(isArray(errors)).toBe(true); | ||
expect(errors.length).toBe(0); | ||
}); |
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
Oops, something went wrong.