Skip to content

Commit

Permalink
refactor: remove validator package and use internal helpers for valid…
Browse files Browse the repository at this point in the history
…ation

BREAKING CHANGE: Types are exported by the submodule
  • Loading branch information
thetutlage committed Dec 30, 2024
1 parent 9126796 commit 164fa9b
Show file tree
Hide file tree
Showing 14 changed files with 1,128 additions and 145 deletions.
14 changes: 4 additions & 10 deletions bin/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { assert } from '@japa/assert'
import { expectTypeOf } from '@japa/expect-type'
import { specReporter } from '@japa/spec-reporter'
import { runFailedTests } from '@japa/run-failed-tests'
import { processCliArgs, configure, run } from '@japa/runner'
import { processCLIArgs, configure, run } from '@japa/runner'

/*
|--------------------------------------------------------------------------
Expand All @@ -17,14 +15,10 @@ import { processCliArgs, configure, run } from '@japa/runner'
|
| Please consult japa.dev/runner-config for the config docs.
*/
processCLIArgs(process.argv.slice(2))
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['tests/**/*.spec.ts'],
plugins: [assert(), runFailedTests(), expectTypeOf()],
reporters: [specReporter()],
importer: (filePath) => import(filePath),
},
files: ['tests/**/*.spec.ts'],
plugins: [assert(), expectTypeOf()],
})

/*
Expand Down
4 changes: 4 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { configPkg } from '@adonisjs/eslint-config'
export default configPkg({
ignores: ['coverage'],
})
3 changes: 1 addition & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
* file that was distributed with this source code.
*/

export * from './src/contracts'
export { schema } from './src/schema/index'
export { schema } from './src/schema/main.js'
173 changes: 84 additions & 89 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,109 @@
"name": "@poppinss/validator-lite",
"version": "1.0.3",
"description": "Simple validation library",
"main": "build/index.js",
"type": "module",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
"build",
"!build/bin",
"!build/tests"
],
"exports": {
".": "./build/index.js",
"./types": "./build/types.js"
},
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test": "npm run vscode:test",
"vscode:test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
"clean": "del-cli build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"test": "c8 npm run quick:test",
"lint": "eslint .",
"format": "prettier --write .",
"commit": "git-cz",
"release": "np --message=\"chore(release): %s\"",
"typecheck": "tsc --noEmit",
"precompile": "npm run lint",
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
"build": "npm run compile",
"version": "npm run build",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json poppinss/validator-lite"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"validator": "^13.9.0"
"prepublishOnly": "npm run build",
"release": "release-it",
"quick:test": "node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts"
},
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@adonisjs/require-ts": "^2.0.13",
"@japa/assert": "^1.4.1",
"@japa/expect-type": "^1.0.3",
"@japa/run-failed-tests": "^1.1.1",
"@japa/runner": "^2.5.1",
"@japa/spec-reporter": "^1.3.3",
"@types/node": "^18.15.2",
"@types/validator": "^13.7.14",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"del-cli": "^5.0.0",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.7.0",
"eslint-plugin-adonis": "^2.1.1",
"eslint-plugin-prettier": "^4.2.1",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"np": "^7.6.3",
"prettier": "^2.8.4",
"typescript": "^4.9.5"
"@adonisjs/eslint-config": "^2.0.0-beta.7",
"@adonisjs/prettier-config": "^1.4.0",
"@adonisjs/tsconfig": "^1.4.0",
"@japa/assert": "^4.0.0",
"@japa/expect-type": "^2.0.2",
"@japa/runner": "^3.1.4",
"@release-it/conventional-changelog": "^9.0.4",
"@swc/core": "^1.10.3",
"@types/node": "^22.10.2",
"c8": "^10.1.3",
"del-cli": "^6.0.0",
"eslint": "^9.17.0",
"prettier": "^3.4.2",
"release-it": "^17.11.0",
"ts-node-maintained": "^10.9.4",
"tsup": "^8.3.5",
"typescript": "^5.7.2"
},
"homepage": "https://github.com/poppinss/validator-lite#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/poppinss/validator-lite.git"
},
"author": "Julien Ripouteau <julien@ripouteau.com>,poppinss,virk",
"license": "MIT",
"bugs": {
"url": "https://github.com/poppinss/validator-lite/issues"
},
"homepage": "https://github.com/poppinss/validator-lite#readme",
"eslintConfig": {
"extends": [
"plugin:adonis/typescriptPackage",
"prettier"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
}
},
"eslintIgnore": [
"build"
"author": "Julien Ripouteau <julien@ripouteau.com>",
"contributors": [
"Harminder Virk <virk@adonisjs.com>"
],
"prettier": {
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"useTabs": false,
"quoteProps": "consistent",
"bracketSpacing": true,
"arrowParens": "always",
"printWidth": 100
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
"tsup": {
"entry": [
"index.ts"
],
"outDir": "./build",
"clean": true,
"format": "esm",
"dts": false,
"sourcemap": false,
"target": "esnext"
},
"np": {
"contents": ".",
"anyBranch": false
"release-it": {
"git": {
"requireCleanWorkingDir": true,
"requireUpstream": true,
"commitMessage": "chore(release): ${version}",
"tagAnnotation": "v${version}",
"push": true,
"tagName": "v${version}"
},
"github": {
"release": true
},
"npm": {
"publish": true,
"skipChecks": true
},
"plugins": {
"@release-it/conventional-changelog": {
"preset": {
"name": "angular"
}
}
}
},
"mrmConfig": {
"core": true,
"license": "MIT",
"services": [
"github-actions"
"c8": {
"reporter": [
"text",
"html"
],
"minNodeVersion": "16.13.1",
"probotApps": [],
"runGhActionsOnWindows": false
}
"exclude": [
"tests/**"
]
},
"prettier": "@adonisjs/prettier-config"
}
9 changes: 3 additions & 6 deletions src/schema/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* file that was distributed with this source code.
*/

import { SchemaFnOptions } from '../contracts'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'
import type { SchemaFnOptions } from '../types.js'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers.js'

/**
* Casts a string value to a boolean
Expand All @@ -22,10 +22,7 @@ function castToBoolean(key: string, value: string, message?: string): boolean {
return false
}

throw new Error(
message ||
`Value for environment variable "${key}" must be a boolean, instead received "${value}"`
)
throw new Error(message || `"${key}" env variable must be a boolean (Current value: "${value}")`)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/schema/index.ts → src/schema/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* file that was distributed with this source code.
*/

import { number } from './number'
import { string } from './string'
import { boolean } from './boolean'
import { oneOf } from './oneOf'
import { oneOf } from './one_of.js'
import { number } from './number.js'
import { string } from './string.js'
import { boolean } from './boolean.js'

export const schema = {
number,
Expand Down
11 changes: 4 additions & 7 deletions src/schema/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
* file that was distributed with this source code.
*/

import { SchemaFnOptions } from '../contracts'
import { ensureValue } from './helpers'
import { ensureValue } from './helpers.js'
import type { SchemaFnOptions } from '../types.js'

/**
* Casts the string to a number and ensures it is no NaN
*/
export function castToNumber(key: string, value: string, message?: string): number {
const castedValue = Number(value)
if (isNaN(castedValue)) {
throw new Error(
message ||
`Value for environment variable "${key}" must be numeric, instead received "${value}"`
)
if (Number.isNaN(castedValue)) {
throw new Error(message || `"${key}" env variable must be a number (Current value: "${value}")`)
}

return castedValue
Expand Down
28 changes: 22 additions & 6 deletions src/schema/oneOf.ts → src/schema/one_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* file that was distributed with this source code.
*/

import { SchemaFnOptions } from '../contracts'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'
import type { SchemaFnOptions } from '../types.js'
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers.js'

/**
* Validates the number to be present in the user defined choices.
Expand All @@ -26,7 +26,7 @@ function ensureOneOf(choices: readonly any[], key: string, value: any, message?:
value = true
} else {
const toNumber = Number(value)
if (!isNaN(toNumber)) {
if (!Number.isNaN(toNumber)) {
value = toNumber
}
}
Expand All @@ -44,9 +44,7 @@ function ensureOneOf(choices: readonly any[], key: string, value: any, message?:
*/
throw new Error(
message ||
`Value for environment variable "${key}" must be one of "${choices.join(
','
)}", instead received "${value}"`
`"${key}" env variable must be one of "${choices.join(',')}" (Current value: "${value}")`
)
}

Expand Down Expand Up @@ -74,3 +72,21 @@ oneOf.optional = function optionalEnum<K extends any>(
return ensureOneOf(choices, key, value, options?.message)
}
}

/**
* Same as the optional rule, but allows a condition to decide when to
* validate the value
*/
oneOf.optionalWhen = function optionalWhenEnum<K extends any>(
condition: boolean | ((key: string, value?: string) => boolean),
choices: readonly K[],
options?: SchemaFnOptions
) {
return function validate(key: string, value?: string): K | undefined {
if (typeof condition === 'function' ? condition(key, value) : condition) {
return oneOf.optional(choices, options)(key, value)
}

return oneOf(choices, options)(key, value)
}
}
Loading

0 comments on commit 164fa9b

Please sign in to comment.