josdejong/ducktype — dự án mã nguồn mở — đang có 92 sao GitHub. Flexible data validation using a ducktype interface. For JavaScript and Node.js.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Flexible data validation using a duck type interface for JavaScript and Node.js.
As JavaScript is a loosely typed language, any variable can contain any
type of data, and any type of data can be passed as arguments to any function.
When dealing with data inputs coming from external sources, there is a need
to validate the type and contents of the data. Ducktype offers an easy way
to validate both basic data types as well as complex structured data types
in a flexible way.
Replace this kind of type checking mess:
function save (contact) {
if (contact && isInteger(contact.id) && (contact.id > 0) && isString(contact.name) &&
contact.address && isString(contact.address.city) && isString(contact.address.street)) {
// ... save contact
}
else {
throw new Error('Invalid contact');
}
}
with this:
var contactType = ducktype({
id: ducktype(Number, {integer: true, min: 0}),
name: String,
address: {
city: String,
street: String,
}
});
function save (contact) {
contactType.validate(contact);
// ... save contact
}
var type = ducktype([Number, Number]);
function add (a, b) {
type.validate(arguments);
return a + b;
}
var sum = add(2, 3); // ok
var sum = add(2, 'string'); // will throw a TypeError
Alternatively, a ducktype wrapper can be created which validates the
function arguments against the ducktype:
var add = ducktype([Number, Number]).wrap(function add (a, b) {
return a + b;
});
var sum = add(2, 3); // ok
var sum = add(2, 'string'); // will throw a TypeError
A basic type. Choose from Array, Boolean, Date, Function, Number,
Object, RegExp, String, null, undefined.
Another ducktype.
An object. All properties of the object will be checked. Each property
can be a basic type, ducktype, object, or array.
An array.
An array can have zero, one or multiple elements which can be
a basic type, ducktype, object, or array.
Providing an array with zero elements will just return a ducktype(Array).
Providing an array with one element will return a ducktype which will
test each of tested arrays elements against the given type,
for example ducktype([Number]).test(1, 2, 3).
Providing an array with multiple elements will validate the length of
the tested array, and validate each of the array elements one to one
against the provided types. This can be used to test the number and type
of function arguments. Example: ducktype([Number, String]).test(2, 'str').
options is an object which can contain properties:
A string name
A boolean optional
A boolean nullable
A boolean integer. Test whether a number has an integer value.
Only applicable for Numbers.
A number min. Test whether a number is larger or equal to a minimum
value. Only applicable for Numbers.
A number max. Test whether a number is smaller or equal to a maximum
value. Only applicable for Numbers.
A created ducktype has functions:
test(object). A function which returns true when provided object matches
the ducktype, and false otherwise.
validate(object). A function which will throw a TypeError when the provided
object does not match the ducktype.
wrap(fn). Creates a wrapper function around the provided function, which
validates the function arguments against the ducktype.
Only applicable for ducktypes containing an array, as the ducktype is tested
against an array with the function arguments.
Implement a parser accepting a string describing a type in
annotations.
Implement support to define your own tests for custom types.
Implement non-strict type checking: when an object can be converted to the
desired type, it is ok. For example a string containing a numeric value can
be considered a valid Number, or a string containing an ISO date can be
considered a valid Date.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/josdejong/ducktype là nguồn chính thức.
josdejong/ducktype có bao nhiêu sao?
josdejong/ducktype có 92 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/josdejong/ducktype. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
josdejong/ducktype có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho josdejong/ducktype. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
josdejong/ducktype còn đang phát triển không?
Commit gần nhất trên josdejong/ducktype là 1 tháng trước (theo timestamp GitHub). Repo có 6 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
josdejong/ducktype viết bằng ngôn ngữ gì?
josdejong/ducktype chủ yếu viết bằng JavaScript. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về ducktype?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về ducktype.