Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn build

- name: Run linter
run: yarn lint
continue-on-error: true

- name: Run tests
run: yarn test
61 changes: 61 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Release

on:
push:
tags:
- 'v*'

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org/'

- name: Update npm to latest
run: npm install -g npm@latest

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build
run: yarn build

- name: Run linter
run: yarn lint

- name: Run tests
run: yarn test

- name: Publish to npm (OIDC)
run: npm publish --access public --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Get version
id: version
run: echo "number=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT

- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
body: |
## NPM Package

Install via npm:
```bash
npm install @lms5400/easy-validation@${{ steps.version.outputs.number }}
```

[View on npm](https://www.npmjs.com/package/@lms5400/easy-validation/v/${{ steps.version.outputs.number }})
generate_release_notes: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules/.bin/audit-ci
node_modules/
dist/
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"eslint.validate": ["javascript", "typescript"],
"eslint.useFlatConfig": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# Simple JavaScript Object & JSON Validator
# Simple JavaScript/TypeScript Object & JSON Validator

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.
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.

This package includes TypeScript type definitions out of the box.

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

## Installation

```bash
npm install @lms5400/easy-validation
```

## Simple Example

```js
Expand Down Expand Up @@ -140,19 +148,19 @@ import { types, conditions, validateData } from 'easy-validation';
const schema1 = {
name: types.isString,
props: {
x: types.isNumber,
y: types.isNumber,
z: types.isNumber,
x: types.isNumeric,
y: types.isNumeric,
z: types.isNumeric,
}
};

const schema2 = {
name: types.isString,
props: types.isObject.and(
conditions.ofShape({
x: types.isNumber,
y: types.isNumber,
z: types.isNumber,
x: types.isNumeric,
y: types.isNumeric,
z: types.isNumeric,
}),
conditions.required // This makes a difference!
)
Expand Down Expand Up @@ -201,7 +209,7 @@ const result = await validateData(schema, sampleData);
This is a more complete sample of what the API might look like in practice.

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

const color = {
red: types.isInteger.and(conditions.range(0,255), conditions.required),
Expand Down Expand Up @@ -268,7 +276,7 @@ result:
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.

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

async function validateKey(value) {
try {
Expand Down
34 changes: 34 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import js from "@eslint/js";
import eslintConfigPrettier from "eslint-config-prettier";
import globals from "globals";
import tseslint from "typescript-eslint";


export default [
{
ignores: ["node_modules/**", "dist/**", "**/*.d.ts"],
},
{
files: ["**/*.{js,ts}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.node,
parser: tseslint.parser,
},
plugins: {
"@typescript-eslint": tseslint.plugin,
},
rules: {
...js.configs.recommended.rules,
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
},
},
{
files: ["**/*.test.{js,ts}", "**/tests/**/*.{js,ts}"],
languageOptions: {
globals: globals.jest,
},
},
eslintConfigPrettier,
];
5 changes: 4 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"bail": 1,
"verbose": true,
"testEnvironment": "node"
"testEnvironment": "node",
"transform": {
"^.+\\.tsx?$": "ts-jest"
}
}
26 changes: 21 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
{
"name": "@lms5400/easy-validation",
"version": "2.0.7",
"version": "3.0.0",
"repository": {
"type": "git",
"url": "git@github.com:LMS007/easy-validation.git"
},
"description": "Simple JSON schema validation. Ideal for request payloads",
"main": "src/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest --config ./jest.config.json"
"test": "jest --config ./jest.config.json",
"build": "tsc",
"lint": "eslint src --ext .js,.ts"
},
"author": "kkeating@gmail.com",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {
"jest": "^29.7.0"
"devDependencies": {
"@eslint/js": "^10.0.1",
"@types/node": "^25.3.0",
"@typescript-eslint/eslint-plugin": "^8.56.0",
"@typescript-eslint/parser": "^8.56.0",
"eslint": "^10.0.1",
"eslint-config-prettier": "^10.1.8",
"globals": "^17.3.0",
"jest": "^29.7.0",
"ts-jest": "^29.4.6",
"typescript": "^5.9.3",
"typescript-eslint": "^8.56.0"
}
}
Loading