+1
-0
@@ -30,1 +30,2 @@ "use strict"; | ||
| exports.valyd = { email: exports.email, url: exports.url, json: exports.json }; | ||
| exports["default"] = exports.valyd; |
+3
-1
@@ -7,2 +7,4 @@ export type StringValidator = (str:string) => boolean | ||
| json: StringValidator | ||
| } | ||
| } | ||
| export default valyd; |
+15
-5
| const isEmail = require('isemail'); | ||
| const isString = (val:any) => { | ||
| return typeof val === 'string'; | ||
| } | ||
| export const email = (str:string) => { | ||
| return isEmail.validate(str, {errorLevel:false}) as boolean; | ||
| return isString(str) && isEmail.validate(str, {errorLevel:false}) as boolean; | ||
| } | ||
@@ -19,3 +23,3 @@ | ||
| '(\\#[-a-z\\d_]*)?$','i'); // fragment locator | ||
| return !!pattern.test(str); | ||
| return isString(str) && !!pattern.test(str); | ||
| } | ||
@@ -25,4 +29,8 @@ | ||
| try { | ||
| JSON.parse(str); | ||
| return true; | ||
| if (isString(str)){ | ||
| JSON.parse(str); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } catch (e) { | ||
@@ -33,2 +41,4 @@ return false; | ||
| export const valyd = { email, url, json }; | ||
| export const valyd = { email, url, json }; | ||
| export default valyd; |
+1
-1
| { | ||
| "name": "valyd", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "A dead simple package for validating various types of strings.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
+12
-0
| # valyd | ||
| A dead simple library for validating various types of strings. Ships with Typescript bindings. | ||
| ## Usage | ||
| ```javascript | ||
| import valyd from 'valyd'; | ||
| valyd.email('example@gmail.com') // Returns true | ||
| valyd.json('"{"testObj":{"a":1,"b":2}}"') // Returns true | ||
| valyd.url('https://google.com') // Returns true | ||
| ``` | ||
| ## API | ||
| All methods are synchronous. | ||
| - `valyd.json(str:string) => boolean`: Returns true if the provided string can be successfully JSON parsed. | ||
| - `valyd.email(str:string) => boolean`: Returns true if the provided string is a valid email, as determined by `isEmail` in its simplest configuration. | ||
| - `valyd.url(str:string) => boolean`: Returns true if the provided string is a valid URL, as determined by [the Regex from the top-rated Stack Overflow post](https://stackoverflow.com/a/5717133/2128308). |
3985
14.12%69
16.95%19
171.43%