Skip to content

Commit e6e4081

Browse files
authored
Merge pull request #1 from LMS007/convert-ts
Convert to TypeScript
2 parents 3e9d5e1 + ec9ab18 commit e6e4081

19 files changed

Lines changed: 969 additions & 203 deletions

.github/workflows/ci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [20.x, 22.x]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
cache: "yarn"
25+
26+
- name: Install dependencies
27+
run: yarn --frozen-lockfile
28+
29+
- name: Build
30+
run: yarn build
31+
32+
- name: Run linter
33+
run: yarn lint
34+
continue-on-error: true
35+
36+
- name: Run tests
37+
run: yarn test

.github/workflows/release.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: write
13+
id-token: write
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Setup Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20.x'
23+
registry-url: 'https://registry.npmjs.org/'
24+
25+
- name: Update npm to latest
26+
run: npm install -g npm@latest
27+
28+
- name: Install dependencies
29+
run: yarn --frozen-lockfile
30+
31+
- name: Build
32+
run: yarn build
33+
34+
- name: Run linter
35+
run: yarn lint
36+
37+
- name: Run tests
38+
run: yarn test
39+
40+
- name: Publish to npm (OIDC)
41+
run: npm publish --access public --provenance
42+
env:
43+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
44+
45+
- name: Get version
46+
id: version
47+
run: echo "number=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT
48+
49+
- name: Create GitHub Release
50+
uses: softprops/action-gh-release@v2
51+
with:
52+
body: |
53+
## NPM Package
54+
55+
Install via npm:
56+
```bash
57+
npm install @lms5400/easy-validation@${{ steps.version.outputs.number }}
58+
```
59+
60+
[View on npm](https://www.npmjs.com/package/@lms5400/easy-validation/v/${{ steps.version.outputs.number }})
61+
generate_release_notes: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules/.bin/audit-ci
22
node_modules/
3+
dist/

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"eslint.validate": ["javascript", "typescript"],
3+
"eslint.useFlatConfig": true,
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll.eslint": "explicit"
6+
}
7+
}

README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
# Simple JavaScript Object & JSON Validator
1+
# Simple JavaScript/TypeScript Object & JSON Validator
22

3-
This package is a lightweight schema validator that is useful for asserting keys and types inside an object or a JSON response, often from HTTP requests. It allows you to reject requests with incomplete or malformed payloads before passing the data along to other parts of the system. It is also helpful for providing users with feedback about what is malformed.
3+
This package is a lightweight schema validator that is useful for asserting keys and types inside an object or a JSON response, often from HTTP requests. It allows you to reject requests with incomplete or malformed payloads before passing the data along to other parts of the system. It is also helpful for providing users with feedback about what is malformed.
4+
5+
This package includes TypeScript type definitions out of the box.
46

57
This package pairs especially well with MongoDB because, after validation, you can confidently insert the JSON objects into the database.
68

9+
## Installation
10+
11+
```bash
12+
npm install @lms5400/easy-validation
13+
```
14+
715
## Simple Example
816

917
```js
@@ -140,19 +148,19 @@ import { types, conditions, validateData } from 'easy-validation';
140148
const schema1 = {
141149
name: types.isString,
142150
props: {
143-
x: types.isNumber,
144-
y: types.isNumber,
145-
z: types.isNumber,
151+
x: types.isNumeric,
152+
y: types.isNumeric,
153+
z: types.isNumeric,
146154
}
147155
};
148156

149157
const schema2 = {
150158
name: types.isString,
151159
props: types.isObject.and(
152160
conditions.ofShape({
153-
x: types.isNumber,
154-
y: types.isNumber,
155-
z: types.isNumber,
161+
x: types.isNumeric,
162+
y: types.isNumeric,
163+
z: types.isNumeric,
156164
}),
157165
conditions.required // This makes a difference!
158166
)
@@ -201,7 +209,7 @@ const result = await validateData(schema, sampleData);
201209
This is a more complete sample of what the API might look like in practice.
202210

203211
```js
204-
const {types, conditions, validateData} = require('./src/index');
212+
const {types, conditions, validateData} = require('easy-validation');
205213

206214
const color = {
207215
red: types.isInteger.and(conditions.range(0,255), conditions.required),
@@ -268,7 +276,7 @@ result:
268276
Use `isCustom` to pass asynchronous functions to value validation. This is useful for performing a database query to validate an ID asynchronously. Alternatively you may also create any custom condition you wish and add it to the `and()` parameters for a given type. Conditions are asynchronously by nature too.
269277

270278
```js
271-
const {types, conditions, validateData} = require('./src/index');
279+
const {types, conditions, validateData} = require('easy-validation');
272280

273281
async function validateKey(value) {
274282
try {

eslint.config.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import js from "@eslint/js";
2+
import eslintConfigPrettier from "eslint-config-prettier";
3+
import globals from "globals";
4+
import tseslint from "typescript-eslint";
5+
6+
7+
export default [
8+
{
9+
ignores: ["node_modules/**", "dist/**", "**/*.d.ts"],
10+
},
11+
{
12+
files: ["**/*.{js,ts}"],
13+
languageOptions: {
14+
ecmaVersion: 2020,
15+
globals: globals.node,
16+
parser: tseslint.parser,
17+
},
18+
plugins: {
19+
"@typescript-eslint": tseslint.plugin,
20+
},
21+
rules: {
22+
...js.configs.recommended.rules,
23+
"no-unused-vars": "off",
24+
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
25+
},
26+
},
27+
{
28+
files: ["**/*.test.{js,ts}", "**/tests/**/*.{js,ts}"],
29+
languageOptions: {
30+
globals: globals.jest,
31+
},
32+
},
33+
eslintConfigPrettier,
34+
];

jest.config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"bail": 1,
33
"verbose": true,
4-
"testEnvironment": "node"
4+
"testEnvironment": "node",
5+
"transform": {
6+
"^.+\\.tsx?$": "ts-jest"
7+
}
58
}

package.json

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
{
22
"name": "@lms5400/easy-validation",
3-
"version": "2.0.7",
3+
"version": "3.0.0",
44
"repository": {
55
"type": "git",
66
"url": "git@github.com:LMS007/easy-validation.git"
77
},
88
"description": "Simple JSON schema validation. Ideal for request payloads",
9-
"main": "src/index.js",
9+
"main": "dist/index.js",
10+
"types": "dist/index.d.ts",
11+
"files": [
12+
"dist"
13+
],
1014
"scripts": {
11-
"test": "jest --config ./jest.config.json"
15+
"test": "jest --config ./jest.config.json",
16+
"build": "tsc",
17+
"lint": "eslint src --ext .js,.ts"
1218
},
1319
"author": "kkeating@gmail.com",
1420
"license": "MIT",
1521
"publishConfig": {
1622
"access": "public"
1723
},
18-
"dependencies": {
19-
"jest": "^29.7.0"
24+
"devDependencies": {
25+
"@eslint/js": "^10.0.1",
26+
"@types/node": "^25.3.0",
27+
"@typescript-eslint/eslint-plugin": "^8.56.0",
28+
"@typescript-eslint/parser": "^8.56.0",
29+
"eslint": "^10.0.1",
30+
"eslint-config-prettier": "^10.1.8",
31+
"globals": "^17.3.0",
32+
"jest": "^29.7.0",
33+
"ts-jest": "^29.4.6",
34+
"typescript": "^5.9.3",
35+
"typescript-eslint": "^8.56.0"
2036
}
2137
}

0 commit comments

Comments
 (0)