├── .babelrc ├── .eslintignore ├── .prettierrc ├── src ├── index.ts ├── validator.ts └── validator.spec.ts ├── config └── test │ └── jest.json ├── .editorconfig ├── .gitignore ├── typings └── index.d.ts ├── examples ├── ts │ ├── tsconfig.json │ ├── index.html │ ├── package.json │ ├── readme.md │ └── src │ │ └── index.ts └── js │ ├── index.html │ ├── package.json │ ├── readme.md │ └── src │ └── index.js ├── .circleci └── config.yml ├── .eslintrc.js ├── tsconfig.json ├── .vscode └── launch.json ├── LICENSE ├── rollup.config.js ├── package.json └── README.md /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/preset-env"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .rpt2_cache 3 | .vscode 4 | config 5 | dist 6 | *.spec.ts 7 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "es5", 4 | "endOfLine": "auto" 5 | } 6 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as charsNotBlackList from './validator'; 2 | 3 | export { charsNotBlackList }; 4 | -------------------------------------------------------------------------------- /config/test/jest.json: -------------------------------------------------------------------------------- 1 | { 2 | "rootDir": "../../", 3 | "preset": "ts-jest", 4 | "restoreMocks": true 5 | } 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | dist/ 4 | *.orig 5 | .idea/ 6 | */src/**/*.js.map 7 | *.log 8 | package-lock.json 9 | coverage/ 10 | .awcache/ 11 | .rpt2_cache 12 | react-app-env.d.ts 13 | .cache 14 | -------------------------------------------------------------------------------- /typings/index.d.ts: -------------------------------------------------------------------------------- 1 | import { FieldValidationFunctionSync } from '@lemoncode/fonk'; 2 | 3 | export namespace charsNotBlackList { 4 | export const validator: FieldValidationFunctionSync; 5 | export function setErrorMessage(message: string | string[]): void; 6 | } 7 | -------------------------------------------------------------------------------- /examples/ts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "jsx": "preserve", 5 | "esModuleInterop": true, 6 | "sourceMap": true, 7 | "allowJs": true, 8 | "lib": ["es6", "dom"], 9 | "rootDir": "src", 10 | "moduleResolution": "node" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/js/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | fonk-chars-not-black-list-validator, javascript example 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /examples/ts/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | fonk-chars-not-black-list-validator, typescript example 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | working_directory: ~/test-ci-code 5 | docker: 6 | - image: circleci/node:10 7 | steps: 8 | - checkout 9 | - run: 10 | name: install 11 | command: 'npm install' 12 | - run: 13 | name: validate 14 | command: 'npm run validate' 15 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | extends: [ 4 | 'plugin:@typescript-eslint/recommended', 5 | 'prettier/@typescript-eslint', 6 | 'plugin:prettier/recommended', 7 | ], 8 | plugins: ['@typescript-eslint', 'prettier'], 9 | rules: { 10 | '@typescript-eslint/no-explicit-any': 'off', 11 | '@typescript-eslint/explicit-function-return-type': 'off', 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es6", 4 | "module": "es6", 5 | "moduleResolution": "node", 6 | "declaration": false, 7 | "noImplicitAny": false, 8 | "sourceMap": true, 9 | "jsx": "react", 10 | "noLib": false, 11 | "allowJs": true, 12 | "suppressImplicitAnyIndexErrors": true, 13 | "skipLibCheck": true, 14 | "esModuleInterop": true 15 | }, 16 | "include": ["./src/**/*"] 17 | } 18 | -------------------------------------------------------------------------------- /examples/js/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fonk-chars-not-black-list-validator-js-example", 3 | "version": "1.0.0", 4 | "description": "fonk-chars-not-black-list-validator, javascript example", 5 | "main": "index.html", 6 | "scripts": { 7 | "start": "parcel index.html --open" 8 | }, 9 | "dependencies": { 10 | "@lemoncode/fonk": "latest", 11 | "@lemoncode/fonk-chars-not-black-list-validator": "latest" 12 | }, 13 | "devDependencies": { 14 | "@babel/core": "^7.6.0", 15 | "parcel-bundler": "^1.6.1" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /examples/ts/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fonk-chars-not-black-list-validator-ts-example", 3 | "version": "1.0.0", 4 | "description": "fonk-chars-not-black-list-validator, typescript example", 5 | "main": "index.html", 6 | "scripts": { 7 | "start": "parcel index.html --open" 8 | }, 9 | "dependencies": { 10 | "@lemoncode/fonk": "latest", 11 | "@lemoncode/fonk-chars-not-black-list-validator": "latest" 12 | }, 13 | "devDependencies": { 14 | "@babel/core": "^7.6.0", 15 | "parcel-bundler": "^1.6.1" 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "Jest selected file", 8 | "program": "${workspaceRoot}/node_modules/jest/bin/jest.js", 9 | "args": [ 10 | "${fileBasenameNoExtension}", 11 | "-c", 12 | "./config/test/jest.json", 13 | "--verbose", 14 | "-i", 15 | "--no-cache", 16 | "--watchAll" 17 | ], 18 | "console": "integratedTerminal", 19 | "internalConsoleOptions": "neverOpen" 20 | } 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /examples/js/readme.md: -------------------------------------------------------------------------------- 1 | # fonk-chars-not-black-list-validator example 2 | 3 | Example using `fonk-chars-not-black-list-validator`. 4 | 5 | [![See fonk-chars-not-black-list-validator example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/lemoncode/fonk-chars-not-black-list-validator/tree/master/examples/js) 6 | 7 | # About Basefactor + Lemoncode 8 | 9 | We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. 10 | 11 | [Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. 12 | 13 | [Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. 14 | 15 | For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend 16 | -------------------------------------------------------------------------------- /examples/ts/readme.md: -------------------------------------------------------------------------------- 1 | # fonk-chars-not-black-list-validator example 2 | 3 | Example using `fonk-chars-not-black-list-validator`. 4 | 5 | [![See fonk-chars-not-black-list-validator example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/lemoncode/fonk-chars-not-black-list-validator/tree/master/examples/ts) 6 | 7 | # About Basefactor + Lemoncode 8 | 9 | We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. 10 | 11 | [Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. 12 | 13 | [Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. 14 | 15 | For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Lemoncode 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/js/src/index.js: -------------------------------------------------------------------------------- 1 | import { createFormValidation } from '@lemoncode/fonk'; 2 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 3 | 4 | const validationSchema = { 5 | field: { 6 | myField: [ 7 | { 8 | validator: charsNotBlackList.validator, 9 | customArgs: { blackListChars: 'nw5' }, 10 | }, 11 | ], 12 | }, 13 | }; 14 | 15 | const formValidation = createFormValidation(validationSchema); 16 | 17 | Promise.all([ 18 | formValidation.validateField('myField', 'My invalid test'), 19 | formValidation.validateField('myField', 'My valid test'), 20 | ]).then(([failedResult, succeededResult]) => { 21 | document.getElementById('app').innerHTML = ` 22 |
23 |

Example with failed result:

24 | 25 |
26 |   formValidation.validateField('myField', 'My invalid test')
27 | 
28 | 29 |

Result:

30 |
31 | ${JSON.stringify(failedResult, null, 2)}
32 | 
33 |
34 | 35 |
36 |

Example with succeeded result:

37 | 38 |
39 | formValidation.validateField('myField', 'My valid test')
40 | 
41 | 42 |

Result:

43 |
44 | ${JSON.stringify(succeededResult, null, 2)}
45 | 
46 |
47 | `; 48 | }); 49 | -------------------------------------------------------------------------------- /examples/ts/src/index.ts: -------------------------------------------------------------------------------- 1 | import { ValidationSchema, createFormValidation } from '@lemoncode/fonk'; 2 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 3 | 4 | const validationSchema: ValidationSchema = { 5 | field: { 6 | myField: [ 7 | { 8 | validator: charsNotBlackList.validator, 9 | customArgs: { blackListChars: 'nw5' }, 10 | }, 11 | ], 12 | }, 13 | }; 14 | 15 | const formValidation = createFormValidation(validationSchema); 16 | 17 | Promise.all([ 18 | formValidation.validateField('myField', 'My invalid test'), 19 | formValidation.validateField('myField', 'My valid test'), 20 | ]).then(([failedResult, succeededResult]) => { 21 | document.getElementById('app').innerHTML = ` 22 |
23 |

Example with failed result:

24 | 25 |
26 |   formValidation.validateField('myField', 'My invalid test')
27 | 
28 | 29 |

Result:

30 |
31 | ${JSON.stringify(failedResult, null, 2)}
32 | 
33 |
34 | 35 |
36 |

Example with succeeded result:

37 | 38 |
39 | formValidation.validateField('myField', 'My valid test')
40 | 
41 | 42 |

Result:

43 |
44 | ${JSON.stringify(succeededResult, null, 2)}
45 | 
46 |
47 | `; 48 | }); 49 | -------------------------------------------------------------------------------- /src/validator.ts: -------------------------------------------------------------------------------- 1 | import { 2 | FieldValidationFunctionSync, 3 | parseMessageWithCustomArgs, 4 | } from '@lemoncode/fonk'; 5 | 6 | const VALIDATOR_TYPE = 'CHARS_NOT_BLACK_LIST'; 7 | 8 | let defaultMessage = `The field can not contain the following characters: '{{blackListChars}}'`; 9 | export const setErrorMessage = (message: string) => (defaultMessage = message); 10 | 11 | const containsBlackListChars = (input: string, blackListChars: string) => 12 | new RegExp(`[${blackListChars}]+`, 'g').test(input); 13 | 14 | const isDefined = (value: string) => 15 | value !== void 0 && value !== null && value !== ''; 16 | 17 | interface CustomValidatorArgs { 18 | blackListChars: string; 19 | } 20 | 21 | const validateType = (value: string) => typeof value === 'string'; 22 | 23 | export const validator: FieldValidationFunctionSync< 24 | CustomValidatorArgs 25 | > = fieldValidatorArgs => { 26 | const { value, message = defaultMessage, customArgs } = fieldValidatorArgs; 27 | const { blackListChars } = customArgs; 28 | 29 | const succeeded = 30 | !isDefined(value) || 31 | (validateType(value) && 32 | validateType(blackListChars) && 33 | !containsBlackListChars(value, blackListChars)); 34 | 35 | return { 36 | succeeded, 37 | message: succeeded 38 | ? '' 39 | : parseMessageWithCustomArgs( 40 | (message as string) || defaultMessage, 41 | customArgs 42 | ), 43 | type: VALIDATOR_TYPE, 44 | }; 45 | }; 46 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import resolve from 'rollup-plugin-node-resolve'; 2 | import babel from 'rollup-plugin-babel'; 3 | import commonjs from 'rollup-plugin-commonjs'; 4 | import typescript from 'rollup-plugin-typescript2'; 5 | import { terser } from 'rollup-plugin-terser'; 6 | import { DEFAULT_EXTENSIONS } from '@babel/core'; 7 | import pkg from './package.json'; 8 | 9 | const builds = [ 10 | { format: 'esm', minify: false }, 11 | { format: 'cjs', minify: false }, 12 | { format: 'umd', minify: false }, 13 | { format: 'umd', minify: true }, 14 | ]; 15 | const extensions = [...DEFAULT_EXTENSIONS, '.ts']; 16 | 17 | export default builds.map(({ format, minify }) => { 18 | const minExtension = minify ? '.min' : ''; 19 | return { 20 | input: 'src/index.ts', 21 | output: { 22 | name: pkg.name, 23 | exports: 'named', 24 | file: `dist/${pkg.name}.${format}${minExtension}.js`, 25 | format, 26 | globals: { 27 | '@lemoncode/fonk': 'Fonk', 28 | }, // Necessary for externals libraries and umd format 29 | }, 30 | external: Object.keys(pkg.peerDependencies || {}), 31 | plugins: [ 32 | resolve(), 33 | commonjs(), 34 | typescript({ 35 | tsconfig: 'tsconfig.json', 36 | rollupCommonJSResolveHack: true, // To be compatible with commonjs plugin 37 | }), 38 | babel({ 39 | extensions, 40 | exclude: 'node_modules/**', 41 | }), 42 | minify ? terser() : null, 43 | ], 44 | }; 45 | }); 46 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@lemoncode/fonk-chars-not-black-list-validator", 3 | "version": "1.0.0", 4 | "description": "This is a [fonk](https://github.com/Lemoncode/fonk) microlibrary that brings validation capabilities to validate if a field of a form only contains valid characters", 5 | "main": "dist/@lemoncode/fonk-chars-not-black-list-validator.cjs.js", 6 | "module": "dist/@lemoncode/fonk-chars-not-black-list-validator.esm.js", 7 | "typings": "typings/index.d.ts", 8 | "files": [ 9 | "dist", 10 | "typings" 11 | ], 12 | "scripts": { 13 | "build": "npm run clean && rollup --config", 14 | "clean": "rimraf dist .rpt2_cache package-lock.json", 15 | "validate": "npm run lint && npm run build && npm run test && npm run test:size", 16 | "lint": "eslint src --ext .ts ", 17 | "lint:fix": "npm run lint -- --fix", 18 | "test": "jest -c ./config/test/jest.json --verbose", 19 | "test:watch": "jest -c ./config/test/jest.json --verbose --watchAll -i", 20 | "test:size": "bundlesize", 21 | "deploy": "npm run validate && np", 22 | "deploy:beta": "npm run validate && np --tag=beta --any-branch" 23 | }, 24 | "bundlesize": [ 25 | { 26 | "path": "./dist/**/*.js", 27 | "maxSize": "1kB" 28 | } 29 | ], 30 | "repository": { 31 | "type": "git", 32 | "url": "git+https://github.com/Lemoncode/fonk-chars-not-black-list-validator.git" 33 | }, 34 | "keywords": [ 35 | "charsNotBlackList", 36 | "validator", 37 | "fonk", 38 | "form validation", 39 | "validate", 40 | "sync validation" 41 | ], 42 | "author": "Lemoncode", 43 | "license": "MIT", 44 | "bugs": { 45 | "url": "https://github.com/Lemoncode/fonk-chars-not-black-list-validator/issues" 46 | }, 47 | "homepage": "https://github.com/Lemoncode/fonk-chars-not-black-list-validator#readme", 48 | "peerDependencies": { 49 | "@lemoncode/fonk": "latest" 50 | }, 51 | "devDependencies": { 52 | "@babel/cli": "^7.5.5", 53 | "@babel/core": "^7.5.5", 54 | "@babel/preset-env": "^7.5.5", 55 | "@lemoncode/fonk": "latest", 56 | "@types/jest": "^24.0.18", 57 | "@typescript-eslint/eslint-plugin": "^2.0.0", 58 | "@typescript-eslint/parser": "^2.0.0", 59 | "bundlesize": "0.18.0", 60 | "eslint": "^6.2.1", 61 | "eslint-config-prettier": "^6.1.0", 62 | "eslint-plugin-prettier": "^3.1.0", 63 | "husky": "^3.0.4", 64 | "jest": "^24.9.0", 65 | "lint-staged": "^9.2.3", 66 | "np": "^5.0.3", 67 | "prettier": "^1.18.2", 68 | "pretty-quick": "^1.11.1", 69 | "rimraf": "^3.0.0", 70 | "rollup": "^1.20.0", 71 | "rollup-plugin-babel": "^4.3.3", 72 | "rollup-plugin-commonjs": "^10.0.2", 73 | "rollup-plugin-node-resolve": "^5.2.0", 74 | "rollup-plugin-terser": "^5.1.1", 75 | "rollup-plugin-typescript2": "^0.22.1", 76 | "ts-jest": "^24.0.2", 77 | "typescript": "^3.5.3" 78 | }, 79 | "husky": { 80 | "hooks": { 81 | "pre-commit": "lint-staged" 82 | } 83 | }, 84 | "lint-staged": { 85 | "src/**/*.{ts,tsx}": [ 86 | "npm run lint:fix", 87 | "pretty-quick — staged", 88 | "git add" 89 | ] 90 | }, 91 | "publishConfig": { 92 | "registry": "https://registry.npmjs.org/", 93 | "access": "public" 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fonk-chars-not-black-list-validator 2 | 3 | [![CircleCI](https://badgen.net/github/status/Lemoncode/fonk-chars-not-black-list-validator/master?icon=circleci&label=circleci)](https://circleci.com/gh/Lemoncode/fonk-chars-not-black-list-validator/tree/master) 4 | [![NPM Version](https://badgen.net/npm/v/@lemoncode/fonk-chars-not-black-list-validator?icon=npm&label=npm)](https://www.npmjs.com/package/@lemoncode/fonk-chars-not-black-list-validator) 5 | [![bundle-size](https://badgen.net/bundlephobia/min/@lemoncode/fonk-chars-not-black-list-validator)](https://bundlephobia.com/result?p=@lemoncode/fonk-chars-not-black-list-validator) 6 | 7 | This is a [fonk](https://github.com/Lemoncode/fonk) microlibrary that brings validation capabilities to: 8 | 9 | - Validate if a field of a form only contains valid characters. 10 | 11 | How to install it: 12 | 13 | ```bash 14 | npm install @lemoncode/fonk-chars-not-black-list-validator --save 15 | ``` 16 | 17 | How to add it to an existing form validation schema: 18 | 19 | We have the following form model: 20 | 21 | ``` 22 | const myFormValues = { 23 | product: 'shoes', 24 | price: 20, 25 | } 26 | ``` 27 | 28 | We can add a charsNotBlackList validation to the myFormValues. CustomArgs are required: 29 | 30 | ```javascript 31 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 32 | 33 | const validationSchema = { 34 | field: { 35 | product: [ 36 | { 37 | validator: charsNotBlackList.validator, 38 | customArgs: { blackListChars: 'nw5' }, 39 | }, 40 | ], 41 | }, 42 | }; 43 | ``` 44 | 45 | Some characters will need to be escaped on the blacklist because they are used in a RegExp. 46 | For example, if you want to include backslash (\\) or simple quote(') as blacklist characters you will need to escape them: 47 | 48 | ```javascript 49 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 50 | 51 | const validationSchema = { 52 | field: { 53 | product: [ 54 | { 55 | validator: charsNotBlackList.validator, 56 | customArgs: { blackListChars: 'n\'w\\5' }, 57 | }, 58 | ], 59 | }, 60 | }; 61 | ``` 62 | 63 | You can customize the error message displayed in two ways: 64 | 65 | - Globally, replace the default error message in all validationSchemas (e.g. porting to spanish): 66 | 67 | ```javascript 68 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 69 | 70 | charsNotBlackList.setErrorMessage('El campo contiene caracteres no válidos.'); 71 | ``` 72 | 73 | - Locally just override the error message for this validationSchema: 74 | 75 | ```javascript 76 | import { charsNotBlackList } from '@lemoncode/fonk-chars-not-black-list-validator'; 77 | 78 | const validationSchema = { 79 | field: { 80 | price: [ 81 | { 82 | validator: charsNotBlackList.validator, 83 | message: 'Error message only updated for the validation schema', 84 | }, 85 | ], 86 | }, 87 | }; 88 | ``` 89 | 90 | Please, refer to [fonk](https://github.com/Lemoncode/fonk) to know more. 91 | 92 | ## License 93 | 94 | [MIT](./LICENSE) 95 | 96 | # About Basefactor + Lemoncode 97 | 98 | We are an innovating team of Javascript experts, passionate about turning your ideas into robust products. 99 | 100 | [Basefactor, consultancy by Lemoncode](http://www.basefactor.com) provides consultancy and coaching services. 101 | 102 | [Lemoncode](http://lemoncode.net/services/en/#en-home) provides training services. 103 | 104 | For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend 105 | -------------------------------------------------------------------------------- /src/validator.spec.ts: -------------------------------------------------------------------------------- 1 | import { validator, setErrorMessage } from './validator'; 2 | 3 | const VALIDATOR_TYPE = 'CHARS_NOT_BLACK_LIST'; 4 | 5 | describe('fonk-chars-not-black-list-validator specs', () => { 6 | it('should return succeeded validation when it feeds value equals undefined', () => { 7 | // Arrange 8 | const value = void 0; 9 | 10 | // Act 11 | const result = validator({ 12 | value, 13 | customArgs: { blackListChars: 'tnw5' }, 14 | }); 15 | 16 | // Assert 17 | expect(result).toEqual({ 18 | succeeded: true, 19 | message: '', 20 | type: VALIDATOR_TYPE, 21 | }); 22 | }); 23 | 24 | it('should return succeeded validation when it feeds value equals null', () => { 25 | // Arrange 26 | const value = null; 27 | 28 | // Act 29 | const result = validator({ 30 | value, 31 | customArgs: { blackListChars: 'tnw5' }, 32 | }); 33 | 34 | // Assert 35 | expect(result).toEqual({ 36 | succeeded: true, 37 | message: '', 38 | type: VALIDATOR_TYPE, 39 | }); 40 | }); 41 | 42 | it('should return succeeded validation when it feeds value equals empty string', () => { 43 | // Arrange 44 | const value = ''; 45 | 46 | // Act 47 | const result = validator({ 48 | value, 49 | customArgs: { blackListChars: 'tnw5' }, 50 | }); 51 | 52 | // Assert 53 | expect(result).toEqual({ 54 | succeeded: true, 55 | message: '', 56 | type: VALIDATOR_TYPE, 57 | }); 58 | }); 59 | 60 | it('should overwrite default message when it feeds value and message', () => { 61 | // Arrange 62 | const value = 'test'; 63 | const message = 'other message'; 64 | 65 | // Act 66 | const result = validator({ 67 | value, 68 | message, 69 | customArgs: { blackListChars: 'tnw5' }, 70 | }); 71 | 72 | // Assert 73 | expect(result).toEqual({ 74 | succeeded: false, 75 | message: 'other message', 76 | type: VALIDATOR_TYPE, 77 | }); 78 | }); 79 | 80 | it('should overwrite default message when it feeds value and calls to setErrorMessage', () => { 81 | // Arrange 82 | const value = 'test'; 83 | 84 | setErrorMessage('other message'); 85 | 86 | // Act 87 | const result = validator({ 88 | value, 89 | customArgs: { blackListChars: 'tnw5' }, 90 | }); 91 | 92 | // Assert 93 | expect(result).toEqual({ 94 | succeeded: false, 95 | message: 'other message', 96 | type: VALIDATOR_TYPE, 97 | }); 98 | }); 99 | 100 | it('should return succeeded validation when it feeds value does not contain blacklist characters', () => { 101 | // Arrange 102 | const value = 'valid'; 103 | 104 | // Act 105 | const result = validator({ 106 | value, 107 | customArgs: { blackListChars: 'tnw5' }, 108 | }); 109 | 110 | // Assert 111 | expect(result).toEqual({ 112 | succeeded: true, 113 | message: '', 114 | type: VALIDATOR_TYPE, 115 | }); 116 | }); 117 | 118 | it('should return failed validation with interpolated message', () => { 119 | // Arrange 120 | const value = 'not valid'; 121 | 122 | // Act 123 | const result = validator({ 124 | value, 125 | message: `The field can not contain the following characters: '{{blackListChars}}'`, 126 | customArgs: { blackListChars: 'tnw5' }, 127 | }); 128 | 129 | // Assert 130 | expect(result).toEqual({ 131 | succeeded: false, 132 | message: `The field can not contain the following characters: 'tnw5'`, 133 | type: VALIDATOR_TYPE, 134 | }); 135 | }); 136 | }); 137 | --------------------------------------------------------------------------------