Skip to content

Commit 164fa9b

Browse files
committed
refactor: remove validator package and use internal helpers for validation
BREAKING CHANGE: Types are exported by the submodule
1 parent 9126796 commit 164fa9b

14 files changed

+1128
-145
lines changed

bin/test.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { assert } from '@japa/assert'
22
import { expectTypeOf } from '@japa/expect-type'
3-
import { specReporter } from '@japa/spec-reporter'
4-
import { runFailedTests } from '@japa/run-failed-tests'
5-
import { processCliArgs, configure, run } from '@japa/runner'
3+
import { processCLIArgs, configure, run } from '@japa/runner'
64

75
/*
86
|--------------------------------------------------------------------------
@@ -17,14 +15,10 @@ import { processCliArgs, configure, run } from '@japa/runner'
1715
|
1816
| Please consult japa.dev/runner-config for the config docs.
1917
*/
18+
processCLIArgs(process.argv.slice(2))
2019
configure({
21-
...processCliArgs(process.argv.slice(2)),
22-
...{
23-
files: ['tests/**/*.spec.ts'],
24-
plugins: [assert(), runFailedTests(), expectTypeOf()],
25-
reporters: [specReporter()],
26-
importer: (filePath) => import(filePath),
27-
},
20+
files: ['tests/**/*.spec.ts'],
21+
plugins: [assert(), expectTypeOf()],
2822
})
2923

3024
/*

eslint.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configPkg } from '@adonisjs/eslint-config'
2+
export default configPkg({
3+
ignores: ['coverage'],
4+
})

index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77
* file that was distributed with this source code.
88
*/
99

10-
export * from './src/contracts'
11-
export { schema } from './src/schema/index'
10+
export { schema } from './src/schema/main.js'

package.json

+84-89
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,109 @@
22
"name": "@poppinss/validator-lite",
33
"version": "1.0.3",
44
"description": "Simple validation library",
5-
"main": "build/index.js",
5+
"type": "module",
66
"files": [
7-
"build/src",
8-
"build/index.d.ts",
9-
"build/index.js"
7+
"build",
8+
"!build/bin",
9+
"!build/tests"
1010
],
11+
"exports": {
12+
".": "./build/index.js",
13+
"./types": "./build/types.js"
14+
},
1115
"scripts": {
12-
"mrm": "mrm --preset=@adonisjs/mrm-preset",
1316
"pretest": "npm run lint",
14-
"test": "npm run vscode:test",
15-
"vscode:test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
16-
"clean": "del-cli build",
17-
"compile": "npm run lint && npm run clean && tsc",
18-
"build": "npm run compile",
19-
"prepublishOnly": "npm run build",
20-
"lint": "eslint . --ext=.ts",
17+
"test": "c8 npm run quick:test",
18+
"lint": "eslint .",
2119
"format": "prettier --write .",
22-
"commit": "git-cz",
23-
"release": "np --message=\"chore(release): %s\"",
20+
"typecheck": "tsc --noEmit",
21+
"precompile": "npm run lint",
22+
"compile": "tsup-node && tsc --emitDeclarationOnly --declaration",
23+
"build": "npm run compile",
2424
"version": "npm run build",
25-
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json poppinss/validator-lite"
26-
},
27-
"publishConfig": {
28-
"access": "public"
29-
},
30-
"dependencies": {
31-
"validator": "^13.9.0"
25+
"prepublishOnly": "npm run build",
26+
"release": "release-it",
27+
"quick:test": "node --import=ts-node-maintained/register/esm --enable-source-maps bin/test.ts"
3228
},
3329
"devDependencies": {
34-
"@adonisjs/mrm-preset": "^5.0.3",
35-
"@adonisjs/require-ts": "^2.0.13",
36-
"@japa/assert": "^1.4.1",
37-
"@japa/expect-type": "^1.0.3",
38-
"@japa/run-failed-tests": "^1.1.1",
39-
"@japa/runner": "^2.5.1",
40-
"@japa/spec-reporter": "^1.3.3",
41-
"@types/node": "^18.15.2",
42-
"@types/validator": "^13.7.14",
43-
"commitizen": "^4.3.0",
44-
"cz-conventional-changelog": "^3.3.0",
45-
"del-cli": "^5.0.0",
46-
"eslint": "^8.36.0",
47-
"eslint-config-prettier": "^8.7.0",
48-
"eslint-plugin-adonis": "^2.1.1",
49-
"eslint-plugin-prettier": "^4.2.1",
50-
"github-label-sync": "^2.3.1",
51-
"husky": "^8.0.3",
52-
"np": "^7.6.3",
53-
"prettier": "^2.8.4",
54-
"typescript": "^4.9.5"
30+
"@adonisjs/eslint-config": "^2.0.0-beta.7",
31+
"@adonisjs/prettier-config": "^1.4.0",
32+
"@adonisjs/tsconfig": "^1.4.0",
33+
"@japa/assert": "^4.0.0",
34+
"@japa/expect-type": "^2.0.2",
35+
"@japa/runner": "^3.1.4",
36+
"@release-it/conventional-changelog": "^9.0.4",
37+
"@swc/core": "^1.10.3",
38+
"@types/node": "^22.10.2",
39+
"c8": "^10.1.3",
40+
"del-cli": "^6.0.0",
41+
"eslint": "^9.17.0",
42+
"prettier": "^3.4.2",
43+
"release-it": "^17.11.0",
44+
"ts-node-maintained": "^10.9.4",
45+
"tsup": "^8.3.5",
46+
"typescript": "^5.7.2"
5547
},
48+
"homepage": "https://github.com/poppinss/validator-lite#readme",
5649
"repository": {
5750
"type": "git",
5851
"url": "git+https://github.com/poppinss/validator-lite.git"
5952
},
60-
"author": "Julien Ripouteau <[email protected]>,poppinss,virk",
61-
"license": "MIT",
6253
"bugs": {
6354
"url": "https://github.com/poppinss/validator-lite/issues"
6455
},
65-
"homepage": "https://github.com/poppinss/validator-lite#readme",
66-
"eslintConfig": {
67-
"extends": [
68-
"plugin:adonis/typescriptPackage",
69-
"prettier"
70-
],
71-
"plugins": [
72-
"prettier"
73-
],
74-
"rules": {
75-
"prettier/prettier": [
76-
"error",
77-
{
78-
"endOfLine": "auto"
79-
}
80-
]
81-
}
82-
},
83-
"eslintIgnore": [
84-
"build"
56+
"author": "Julien Ripouteau <[email protected]>",
57+
"contributors": [
58+
"Harminder Virk <[email protected]>"
8559
],
86-
"prettier": {
87-
"trailingComma": "es5",
88-
"semi": false,
89-
"singleQuote": true,
90-
"useTabs": false,
91-
"quoteProps": "consistent",
92-
"bracketSpacing": true,
93-
"arrowParens": "always",
94-
"printWidth": 100
60+
"license": "MIT",
61+
"publishConfig": {
62+
"access": "public",
63+
"provenance": true
9564
},
96-
"config": {
97-
"commitizen": {
98-
"path": "cz-conventional-changelog"
99-
}
65+
"tsup": {
66+
"entry": [
67+
"index.ts"
68+
],
69+
"outDir": "./build",
70+
"clean": true,
71+
"format": "esm",
72+
"dts": false,
73+
"sourcemap": false,
74+
"target": "esnext"
10075
},
101-
"np": {
102-
"contents": ".",
103-
"anyBranch": false
76+
"release-it": {
77+
"git": {
78+
"requireCleanWorkingDir": true,
79+
"requireUpstream": true,
80+
"commitMessage": "chore(release): ${version}",
81+
"tagAnnotation": "v${version}",
82+
"push": true,
83+
"tagName": "v${version}"
84+
},
85+
"github": {
86+
"release": true
87+
},
88+
"npm": {
89+
"publish": true,
90+
"skipChecks": true
91+
},
92+
"plugins": {
93+
"@release-it/conventional-changelog": {
94+
"preset": {
95+
"name": "angular"
96+
}
97+
}
98+
}
10499
},
105-
"mrmConfig": {
106-
"core": true,
107-
"license": "MIT",
108-
"services": [
109-
"github-actions"
100+
"c8": {
101+
"reporter": [
102+
"text",
103+
"html"
110104
],
111-
"minNodeVersion": "16.13.1",
112-
"probotApps": [],
113-
"runGhActionsOnWindows": false
114-
}
105+
"exclude": [
106+
"tests/**"
107+
]
108+
},
109+
"prettier": "@adonisjs/prettier-config"
115110
}

src/schema/boolean.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { SchemaFnOptions } from '../contracts'
11-
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'
10+
import type { SchemaFnOptions } from '../types.js'
11+
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers.js'
1212

1313
/**
1414
* Casts a string value to a boolean
@@ -22,10 +22,7 @@ function castToBoolean(key: string, value: string, message?: string): boolean {
2222
return false
2323
}
2424

25-
throw new Error(
26-
message ||
27-
`Value for environment variable "${key}" must be a boolean, instead received "${value}"`
28-
)
25+
throw new Error(message || `"${key}" env variable must be a boolean (Current value: "${value}")`)
2926
}
3027

3128
/**

src/schema/index.ts src/schema/main.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { number } from './number'
11-
import { string } from './string'
12-
import { boolean } from './boolean'
13-
import { oneOf } from './oneOf'
10+
import { oneOf } from './one_of.js'
11+
import { number } from './number.js'
12+
import { string } from './string.js'
13+
import { boolean } from './boolean.js'
1414

1515
export const schema = {
1616
number,

src/schema/number.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { SchemaFnOptions } from '../contracts'
11-
import { ensureValue } from './helpers'
10+
import { ensureValue } from './helpers.js'
11+
import type { SchemaFnOptions } from '../types.js'
1212

1313
/**
1414
* Casts the string to a number and ensures it is no NaN
1515
*/
1616
export function castToNumber(key: string, value: string, message?: string): number {
1717
const castedValue = Number(value)
18-
if (isNaN(castedValue)) {
19-
throw new Error(
20-
message ||
21-
`Value for environment variable "${key}" must be numeric, instead received "${value}"`
22-
)
18+
if (Number.isNaN(castedValue)) {
19+
throw new Error(message || `"${key}" env variable must be a number (Current value: "${value}")`)
2320
}
2421

2522
return castedValue

src/schema/oneOf.ts src/schema/one_of.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { SchemaFnOptions } from '../contracts'
11-
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers'
10+
import type { SchemaFnOptions } from '../types.js'
11+
import { ensureValue, BOOLEAN_NEGATIVES, BOOLEAN_POSITIVES } from './helpers.js'
1212

1313
/**
1414
* Validates the number to be present in the user defined choices.
@@ -26,7 +26,7 @@ function ensureOneOf(choices: readonly any[], key: string, value: any, message?:
2626
value = true
2727
} else {
2828
const toNumber = Number(value)
29-
if (!isNaN(toNumber)) {
29+
if (!Number.isNaN(toNumber)) {
3030
value = toNumber
3131
}
3232
}
@@ -44,9 +44,7 @@ function ensureOneOf(choices: readonly any[], key: string, value: any, message?:
4444
*/
4545
throw new Error(
4646
message ||
47-
`Value for environment variable "${key}" must be one of "${choices.join(
48-
','
49-
)}", instead received "${value}"`
47+
`"${key}" env variable must be one of "${choices.join(',')}" (Current value: "${value}")`
5048
)
5149
}
5250

@@ -74,3 +72,21 @@ oneOf.optional = function optionalEnum<K extends any>(
7472
return ensureOneOf(choices, key, value, options?.message)
7573
}
7674
}
75+
76+
/**
77+
* Same as the optional rule, but allows a condition to decide when to
78+
* validate the value
79+
*/
80+
oneOf.optionalWhen = function optionalWhenEnum<K extends any>(
81+
condition: boolean | ((key: string, value?: string) => boolean),
82+
choices: readonly K[],
83+
options?: SchemaFnOptions
84+
) {
85+
return function validate(key: string, value?: string): K | undefined {
86+
if (typeof condition === 'function' ? condition(key, value) : condition) {
87+
return oneOf.optional(choices, options)(key, value)
88+
}
89+
90+
return oneOf(choices, options)(key, value)
91+
}
92+
}

0 commit comments

Comments
 (0)