Node.js 용 유효성 검사 라이브러리
다음에 대한 변수의 유효성을 검사하는 node.js에 대한 좋은 유효성 검사 프레임 워크가 있습니까?
- 문자열, 날짜, 숫자 등의 유형 인 경우
- 최대 및 최소 길이
- 이메일, 전화
- 기타...
최근 에 chriso의 노드 유효성 검사기 를 발견 했습니다 .
예
var check = require('validator').check,
sanitize = require('validator').sanitize
//Validate
check('test@email.com').len(6, 64).isEmail(); //Methods are chainable
check('abc').isInt(); //Throws 'Invalid integer'
check('abc', 'Please enter a number').isInt(); //Throws 'Please enter a number'
check('abcdefghijklmnopzrtsuvqxyz').is(/^[a-z]+$/);
//Sanitize / Filter
var int = sanitize('0123').toInt(); //123
var bool = sanitize('true').toBoolean(); //true
var str = sanitize(' \s\t\r hello \n').trim(); //'hello'
var str = sanitize('aaaaaaaaab').ltrim('a'); //'b'
var str = sanitize(large_input_str).xss();
var str = sanitize('<a>').entityDecode(); //'<a>'
레일과 cakephp 스타일 유효성 검사에 루비를 원했습니다. 나는 그것이 내가 계속해서 사용할 것이라는 것을 알았 으므로이 빠른 npm 모듈을 만들었습니다. https://npmjs.org/package/iz
재스민처럼 의미 론적으로 읽으며 클라이언트 또는 서버 측에서 사용할 수 있습니다. 즉, JSON을 통해 전달 된 유효성 검사 규칙과 함께 commonjs 및 amd에 대한 지원이 제공됩니다.
그것은 꽤 잘 단위 테스트를 거쳤고, 생산 의존성이 없으며, 범위는 단지 유효성 검사로 잠겨 있습니다. 우리는 지금 작은 커뮤니티가있는 것 같습니다. 아이디어, 피드백 및 풀 요청은 모두 환영합니다.
현재 라이브러리 기능 :
iz.alphaNumeric(*); // Is number or string(contains only numbers or strings)
iz.between(number, start, end); // Number is start or greater but less than or equal to end, all params numeric
iz.blank(*); // Empty string, undefined or null
iz.boolean(*); // true, false, 0, 1
iz.cc(*); // Luhn checksum approved value
iz.date(*); // Is a data obj or is a string that is easily converted to a date
iz.decimal(*); // Contains 1 decimal point and potentially can have a - at the beginning
iz.email(*); // Seems like a valid email address
iz.extension(ob1, ob2); // If obj2's methods are all found in obj1
iz.fileExtension(arr, value); // Checks if the extension of value is in arr. An obj can be provide, but must have indexOf defined.
iz.fileExtensionAudio(value); // Check against mp3, ogg, wav, aac
iz.fileExtensionImage(value); // Check against png, jpg, jpeg, gif, bmp, svg, gif
iz.inArray(arr, value); // If * is in the array
iz.int(*, bool (optional)); // Is an int. If the 2nd variable is true (false by default) a decimal is allowed
iz.ip(str); // str resembles an IPV4 or IPV6 address
iz.minLen(val, min); // val (str or arr) is greater than min
iz.maxLen(val, max); // val (str or arr) is shorter than max
iz.multiple(num, mult); // Number is multiple of another number
iz.number(*); // Is either an int or decimal
iz.ofType(obj, typeName); // If it is a named object, and the name matches the string
iz.phone(str, canHaveExtension?); // Is an american phone number. Any punctuations are allowed.
iz.postal(*); // Is a postal code or zip code
iz.ssn(*); // Is a social security number
Node-validator 는 문자열 유효성 검사, 필터링 및 삭제 방법의 라이브러리입니다.
따라서 숫자 및 배열에 대한 더 나은 지원을 원하면 Chai.js를 사용해보십시오 . 다음은 몇 가지 예입니다.
var expect = require('chai').expect;
try {
expect([1, 2, 3]).to.have.length.below(4);
expect(5).to.be.within(3,6);
expect('test').to.have.length(4);
} catch (e) {
// should not occur
}
이것이 스키마 모듈이하는 일이라고 생각합니다 . "개발 중"이라는 레이블이 지정되어 있습니다 (v0.1a로 태그 됨). 나는 그것을 직접 시도하지 않았지만 README에 표시된 예제에서 꽤 좋아 보인다.
변수 수준이 아니라 함수 인수 수준 :
http://github.com/torvalamo/argtype.js
Date currently needs to pass as type 'object'. It is definately something that I have forgotten, and will put on the todo-list. ;)
Specific max and min length is not supported, and will probably not be implemented (but who knows). Email, phone and all that can be checked by regex. See the example on the github page, which includes a (simple) regex example.
I recommend valida there is lack of documentation however it is pretty simple to understand looking at the examples.
Valida features are:
- Sanitization
- Synchronous and asynchronous validation
- Groups
- Extensible
I'm finishing writing a library on Javascript validations (both node and browser), I'll be writing the docs on the next few days, but the code is almost ready: https://github.com/wilkerlucio/composed-validations
Please let me know if you have any questions/suggestions on it :)
참고URL : https://stackoverflow.com/questions/4088723/validation-library-for-node-js
'Program Tip' 카테고리의 다른 글
C # : 반환 형식 재정의 (0) | 2020.10.24 |
---|---|
C #에서 하위 목록을 가져 오는 방법 (0) | 2020.10.24 |
Android : API 수준 VS. (0) | 2020.10.24 |
벡터 중 하나만 사용하는 기준을 사용하여 동일한 방식으로 두 벡터를 정렬하려면 어떻게해야합니까? (0) | 2020.10.24 |
WebView의 메모리 누수 (0) | 2020.10.24 |