Skip to content

Commit f61343e

Browse files
authored
Merge pull request #4 from mrbrunelli/feat/add-typescript
feat/add-typescript
2 parents 563c401 + e8d983c commit f61343e

11 files changed

Lines changed: 133 additions & 52 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
3-
.npmrc
3+
.npmrc
4+
dist

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A Javascript object validator for tiny schemas.
1515
#### How to use
1616

1717
```js
18-
const Validate = require('@mrbrunelli/object-validator')
18+
const Validate = require('@mrbrunelli/object-validator').default
1919

2020
const objectExample = {
2121
foo: {

index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Validate from './lib/validate'
2+
3+
export default new Validate()

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = {
3131
// ],
3232

3333
// Indicates which provider should be used to instrument code for coverage
34-
coverageProvider: "v8",
34+
coverageProvider: 'v8',
3535

3636
// A list of reporter names that Jest uses when writing coverage reports
3737
// coverageReporters: [
@@ -93,7 +93,7 @@ module.exports = {
9393
// notifyMode: "failure-change",
9494

9595
// A preset that is used as a base for Jest's configuration
96-
// preset: undefined,
96+
preset: 'ts-jest/presets/js-with-ts'
9797

9898
// Run tests from one or more projects
9999
// projects: undefined,
@@ -191,4 +191,4 @@ module.exports = {
191191

192192
// Whether to use watchman for file crawling
193193
// watchman: true,
194-
};
194+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const Validate = require('./validate')
1+
import Validate from './validate'
2+
import { IValidate } from './validate.types'
23

34
const makeObjectStub = () => {
45
return {
@@ -21,14 +22,11 @@ const makeObjectStub = () => {
2122
}
2223
}
2324

24-
const makeSut = () => Validate
25+
const makeSut = () => new Validate()
2526

2627
describe('validate', () => {
27-
/**
28-
* @type {Validate}
29-
*/
30-
let sut
31-
let objectStub
28+
let sut: IValidate
29+
let objectStub: object
3230

3331
beforeEach(() => {
3432
sut = makeSut()
@@ -59,7 +57,7 @@ describe('validate', () => {
5957
})
6058

6159
test('should return false if provided field is empty', () => {
62-
const isValid = sut.isValid(objectStub)
60+
const isValid = sut.isValid(objectStub, [])
6361
expect(isValid).toBeFalsy()
6462
})
6563

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
class Validate {
2-
/**
3-
* @example
4-
* const object = { foo: { bar: { message: 'Hello!' } } }
5-
* const isValid = Validate.isValid(object, [['foo'], ['foo.bar.message']])
6-
* console.log(isValid) // returns true
7-
*
8-
* @example
9-
* const object = { foo: { bar: { message: 'Hello!' } } }
10-
* const isValid = Validate.isValid(object, [['foo.bar.message', 'Wrong!']])
11-
* console.log(isValid) // returns false
12-
*
13-
* @param {object} object Accepts an object schema.
14-
* @param {string[]} fields Accepts string array. It is used to loop provided object and validate fields.
15-
*/
16-
static isValid(object, fields) {
17-
const hasFields = fields && Array.isArray(fields) && fields.length > 0
18-
const hasObject =
19-
object && typeof object === 'object' && Object.keys(object).length > 0
20-
if (!hasFields || !hasObject) return false
1+
import { IValidate, IObject, IFields } from './validate.types'
2+
3+
export default class Validate implements IValidate {
4+
isValid(object: IObject, fields: IFields): boolean {
5+
if (!this.isValidProps(object, fields)) return false
216

227
return fields.every(([field, value]) => {
238
const splitedFields = field.split('.')
@@ -29,7 +14,7 @@ class Validate {
2914
const existsFieldInObject = splicedField in object
3015

3116
if (existsFieldInObject && existsMoreFields) {
32-
return Validate.isValid(object[splicedField], [[joinedFields, value]])
17+
return this.isValid(object[splicedField], [[joinedFields, value]])
3318
}
3419

3520
const isNecessaryToCompareValues = !existsMoreFields && existsValue
@@ -38,6 +23,11 @@ class Validate {
3823
return existsFieldInObject && !existsMoreFields
3924
})
4025
}
41-
}
4226

43-
module.exports = Validate
27+
private isValidProps(object: IObject, fields: IFields) {
28+
const hasFields = fields && Array.isArray(fields) && fields.length > 0
29+
const hasObject =
30+
object && typeof object === 'object' && Object.keys(object).length > 0
31+
return hasObject && hasFields
32+
}
33+
}

lib/validate.types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface IValidate {
2+
isValid(object: IObject, fields: IFields): boolean
3+
}
4+
5+
export type IObject = { [key: string]: any }
6+
7+
export type IFields = Array<[string, any?]>

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
{
22
"name": "@mrbrunelli/object-validator",
3-
"version": "2.0.1",
3+
"version": "2.0.5",
44
"description": "A simple object validator for tiny schemas",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"test": "jest --runInBand --verbose --passWithNoTests",
89
"tdd": "yarn test --watch",
910
"test:ci": "jest --runInBand --verbose --coverage",
11+
"build": "tsc",
12+
"prepublishOnly": "yarn build",
1013
"prepare": "husky install"
1114
},
1215
"repository": {
1316
"type": "git",
1417
"url": "git+https://github.com/mrbrunelli/object-validator.git"
1518
},
16-
"keywords": ["validator", "schema", "object-validator", "schema-validator"],
19+
"keywords": [
20+
"validator",
21+
"schema",
22+
"object-validator",
23+
"schema-validator"
24+
],
1725
"author": "Matheus R. Brunelli <matheus.brunelli@gmail.com>",
1826
"license": "MIT",
1927
"bugs": {
@@ -28,6 +36,8 @@
2836
"git-commit-msg-linter": "^4.0.7",
2937
"husky": "^7.0.0",
3038
"jest": "^27.4.7",
31-
"prettier": "^2.5.1"
39+
"prettier": "^2.5.1",
40+
"ts-jest": "^27.1.3",
41+
"typescript": "^4.5.5"
3242
}
3343
}

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"exclude": ["node_modules", "**/*.spec.ts"],
3+
"include": ["*.ts"],
4+
"compilerOptions": {
5+
"allowJs": true,
6+
"declaration": true,
7+
"declarationMap": true,
8+
"target": "es2016",
9+
"module": "commonjs",
10+
"outDir": ".",
11+
"esModuleInterop": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"strict": true,
14+
"skipLibCheck": true
15+
}
16+
}

0 commit comments

Comments
 (0)