Skip to content

Commit

Permalink
feat: use ECMAScript modules (ESM) instead of CommonJS (#95)
Browse files Browse the repository at this point in the history
* feat: use ECMAScript modules (ESM) instead of CommonJS

BREAKING CHANGE

* chore: remove dead code and fix grammar's internal typing

* feat: drop support for node 14 and 16

BREAKING CHANGE

* chore: remove CJS packaging instructions

* fix: imports

* fix: typo (package name)

* fix: nvm node version
  • Loading branch information
ssaarela authored Oct 13, 2023
1 parent 3657969 commit 92e9118
Show file tree
Hide file tree
Showing 40 changed files with 736 additions and 1,888 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: [ubuntu-latest]
strategy:
matrix:
node: ['14', '16', '18', '20']
node: ['18', '20']
name: Node ${{ matrix.node }} sample
timeout-minutes: 5
steps:
Expand Down
3 changes: 1 addition & 2 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
14

18
28 changes: 18 additions & 10 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,40 @@
{
"type": "node",
"request": "launch",
"name": "Jest All",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["--runInBand"],
"name": "Test All",
"program": "${workspaceFolder}/node_modules/.bin/vitest",
"args": ["--run", "--no-threads"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"timeout": 20000
},
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"name": "Test Current",
"program": "${workspaceFolder}/node_modules/.bin/vitest",
"args": ["--run", "--no-threads", "${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"timeout": 20000
},
{
"type": "node",
"request": "launch",
"name": "Watch Current",
"program": "${workspaceFolder}/node_modules/.bin/vitest",
"args": ["--no-threads", "${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"timeout": 20000
},
{
"name": "Run Current file",
"name": "Run Current File",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "${workspaceFolder}/node_modules/ts-node/register/transpile-only"],

"runtimeArgs": ["--nolazy", "--loader", "ts-node/esm", "-r", "${workspaceFolder}/node_modules/ts-node/register/transpile-only"],
"args": ["${relativeFile}"],

"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
Expand Down
9 changes: 0 additions & 9 deletions jest.config.js

This file was deleted.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"private": true,
"repository": "github:finnair/v-validation",
"type": "module",
"scripts": {
"build": "lerna run build",
"test": "jest",
"test": "vitest --run",
"boot": "lerna bootstrap",
"clean": "yarn clean:dist && yarn clean:modules && yarn clean:tscache",
"clean:dist": "find . -type f -path '*/dist/*' -delete && find . -type d -name dist -delete -empty",
"clean:modules": "find . -type f -path '*/node_modules/*' -delete && find . -type d -name node_modules -delete -empty",
"clean:tscache": "find . -type f -name 'tsconfig.tsbuildinfo' -delete"
},
"devDependencies": {
"@types/jest": "28.1.1",
"@types/node": "14.14.31",
"jest": "28.1.3",
"@types/node": "20.8.4",
"@vitest/coverage-v8": "0.34.5",
"vitest": "0.34.5",
"lerna": "5.5.2",
"prettier": "2.7.1",
"ts-jest": "28.0.8",
"ts-node": "10.9.1",
"typescript": "4.8.2",
"typescript": "5.2.2",
"moment": "2.29.4",
"luxon": "3.2.1",
"@types/luxon": "3.0.1"
Expand All @@ -36,7 +36,7 @@
"arrowParens": "avoid"
},
"engines": {
"node": ">= 14.15.0"
"node": ">= 18"
},
"resolutions": {
"minimist": ">=0.2.1"
Expand Down
11 changes: 9 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
"version": "4.3.0",
"private": false,
"description": "V-validation core package",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"license": "MIT",
"homepage": "https://github.com/finnair/v-validation/tree/master/packages/core#readme",
"bugs": "https://github.com/finnair/v-validation/issues",
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/V.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test, expect } from 'vitest'
import {
Validator,
Violation,
Expand All @@ -21,9 +22,10 @@ import {
EnumMismatch,
SyncPromise,
} from './validators';
import { V } from './V';
import { V } from './V.js';
import { Path } from '@finnair/path';
import { expectUndefined, expectValid, expectViolations, verifyValid } from './testUtil.spec';
import { expectUndefined, expectValid, expectViolations, verifyValid } from './testUtil.spec.js';
import { fail } from 'assert';

const ROOT = Path.ROOT,
index = Path.index,
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/V.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { default as validateUuid } from 'uuid-validate';
import { SchemaValidator, SchemaModel } from './schema';
import { SchemaValidator, SchemaModel } from './schema.js';
import { Path } from '@finnair/path';
import {
IgnoreValidator,
Expand Down Expand Up @@ -53,7 +53,7 @@ import {
HasValueValidator,
JsonValidator,
RequiredValidator,
} from './validators';
} from './validators.js';

const ignoreValidator = new IgnoreValidator(),
anyValidator = new AnyValidator(),
Expand All @@ -63,9 +63,9 @@ const ignoreValidator = new IgnoreValidator(),
nullOrUndefinedValidator = new IsNullOrUndefinedValidator(),
notEmptyValidator = new NotEmptyValidator(),
notBlankValidator = new NotBlankValidator(),
emptyToNullValidator = new ValueMapper(val => (isNullOrUndefined(val) || val === '' ? null : val)),
emptyToUndefinedValidator = new ValueMapper(val => (isNullOrUndefined(val) || val === '' ? undefined : val)),
undefinedToNullValidator = new ValueMapper(val => (val === undefined ? null : val)),
emptyToNullValidator = new ValueMapper((value: any) => (isNullOrUndefined(value) || value === '' ? null : value)),
emptyToUndefinedValidator = new ValueMapper((value: any) => (isNullOrUndefined(value) || value === '' ? undefined : value)),
undefinedToNullValidator = new ValueMapper((value: any) => (value === undefined ? null : value)),
booleanValidator = new BooleanValidator(),
numberValidator = new NumberValidator(NumberFormat.number),
toNumberValidator = new NumberNormalizer(NumberFormat.number),
Expand Down Expand Up @@ -110,9 +110,9 @@ export const V = {

undefinedToNull: () => undefinedToNullValidator,

emptyTo: (defaultValue: any) => new ValueMapper(val => (isNullOrUndefined(val) || val === '' ? defaultValue : val)),
emptyTo: (defaultValue: any) => new ValueMapper((value: any) => (isNullOrUndefined(value) || value === '' ? defaultValue : value)),

uuid: (version?: number) => new AssertTrueValidator(value => !isNullOrUndefined(value) && validateUuid(value, version), 'UUID'),
uuid: (version?: number) => new AssertTrueValidator((value: any) => !isNullOrUndefined(value) && validateUuid(value, version), 'UUID'),

pattern: (pattern: string | RegExp, flags?: string) => new PatternValidator(pattern, flags),

Expand Down Expand Up @@ -148,9 +148,9 @@ export const V = {

nullTo: (defaultValue: string | number | bigint | boolean | symbol) => new ValueMapper(value => (isNullOrUndefined(value) ? defaultValue : value)),

nullToObject: () => new ValueMapper(value => (isNullOrUndefined(value) ? {} : value)),
nullToObject: () => new ValueMapper((value: any) => (isNullOrUndefined(value) ? {} : value)),

nullToArray: () => new ValueMapper(value => (isNullOrUndefined(value) ? [] : value)),
nullToArray: () => new ValueMapper((value: any) => (isNullOrUndefined(value) ? [] : value)),

array: (...items: Validator[]) => new ArrayValidator(maybeAllOfValidator(items)),

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './V';
export * from './validators';
export * from './V.js';
export * from './validators.js';
9 changes: 5 additions & 4 deletions packages/core/src/schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SchemaValidator, DiscriminatorViolation } from './schema';
import { V } from './V';
import { defaultViolations, TypeMismatch, ObjectValidator, HasValueViolation, Violation } from './validators';
import { expectViolations, expectValid } from './testUtil.spec';
import { describe, test, expect } from 'vitest'
import { SchemaValidator, DiscriminatorViolation } from './schema.js';
import { V } from './V.js';
import { defaultViolations, TypeMismatch, ObjectValidator, HasValueViolation, Violation } from './validators.js';
import { expectViolations, expectValid } from './testUtil.spec.js';
import { Path } from '@finnair/path';

const ROOT = Path.ROOT,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Violation,
TypeMismatch,
ObjectModel,
} from './validators';
} from './validators.js';
import { Path } from '@finnair/path';

export interface DiscriminatorFn {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/testUtil.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Validator, Violation, ValidationResult, ValidatorOptions } from './validators';
import { test, expect } from 'vitest'
import { Validator, Violation, ValidationResult, ValidatorOptions } from './validators.js';

export async function expectViolations(value: any, validator: Validator, ...violations: Violation[]) {
const result = await validator.validate(value);
Expand Down Expand Up @@ -26,4 +27,4 @@ export function verifyValid(result: ValidationResult, value: any, convertedValue
return result;
}

test.skip('do not fail build because of no tests found', () => {});
test.skip('do not fail build because of no tests found', () => { });
11 changes: 9 additions & 2 deletions packages/luxon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
"version": "4.3.0",
"private": false,
"description": "Luxon validators",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"license": "MIT",
"homepage": "https://github.com/finnair/v-validation/tree/master/packages/luxon#readme",
"bugs": "https://github.com/finnair/v-validation/issues",
Expand Down
7 changes: 4 additions & 3 deletions packages/luxon/src/Vluxon.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, beforeAll, afterAll, test, expect } from 'vitest'
import { Validator, ValidatorOptions, ValidationResult, V, Violation, defaultViolations, TypeMismatch } from '@finnair/v-validation';
import { LuxonValidator, Vluxon } from './Vluxon';
import { LuxonValidator, Vluxon } from './Vluxon.js';
import {
LocalDateLuxon,
DateTimeLuxon,
Expand All @@ -9,7 +10,7 @@ import {
LuxonDateTime,
LocalTimeLuxon,
LocalDateTimeLuxon,
} from './luxon';
} from './luxon.js';
import { DateTime, Duration, FixedOffsetZone, IANAZone, Settings } from 'luxon';
import { Path } from '@finnair/path';

Expand All @@ -20,7 +21,7 @@ async function expectViolations(value: any, validator: Validator, ...violations:

async function expectValid(value: any, validator: Validator, convertedValue?: any, ctx?: ValidatorOptions) {
const result = await validator.validate(value, ctx);
return verifyValid(result, value, convertedValue);
verifyValid(result, value, convertedValue);
}

function verifyValid(result: ValidationResult, value: any, convertedValue?: any) {
Expand Down
2 changes: 1 addition & 1 deletion packages/luxon/src/Vluxon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
LuxonDateTime,
LocalTimeLuxon,
LocalDateTimeLuxon,
} from './luxon';
} from './luxon.js';

export type LuxonInput = string | DateTime | LuxonDateTime;

Expand Down
4 changes: 2 additions & 2 deletions packages/luxon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './Vluxon';
export * from './luxon';
export * from './Vluxon.js';
export * from './luxon.js';
11 changes: 9 additions & 2 deletions packages/moment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
"version": "4.3.0",
"private": false,
"description": "Moment validators",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"license": "MIT",
"homepage": "https://github.com/finnair/v-validation/tree/master/packages/moment#readme",
"bugs": "https://github.com/finnair/v-validation/issues",
Expand Down
3 changes: 2 additions & 1 deletion packages/moment/src/Vmoment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, test, expect } from 'vitest'
import moment, { Moment } from 'moment';
import { V, defaultViolations, Validator, ValidatorOptions, ValidationResult, Violation, TypeMismatch } from '@finnair/v-validation';
import { Path } from '@finnair/path';
import { Vmoment, dateUtcMoment, dateTimeUtcMoment, dateTimeMoment, timeMoment, dateMoment, dateTimeMillisUtcMoment, dateTimeMillisMoment } from './Vmoment';
import { Vmoment, dateUtcMoment, dateTimeUtcMoment, dateTimeMoment, timeMoment, dateMoment, dateTimeMillisUtcMoment, dateTimeMillisMoment } from './Vmoment.js';

async function expectViolations(value: any, validator: Validator, ...violations: Violation[]) {
const result = await validator.validate(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/moment/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './Vmoment';
export * from './Vmoment.js';
12 changes: 10 additions & 2 deletions packages/path-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
"version": "4.0.0",
"private": false,
"description": "Simple object path as array of strings and numbers",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"license": "MIT",
"homepage": "https://github.com/finnair/v-validation/tree/master/packages/path-matcher-parser#readme",
"bugs": "https://github.com/finnair/v-validation/issues",
Expand All @@ -25,6 +32,7 @@
"dependencies": {
"@finnair/path": "^4.0.0",
"@types/nearley": "2.11.2",
"@types/moo": "0.5.1",
"nearley": "2.20.1"
}
}
4 changes: 2 additions & 2 deletions packages/path-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { parsePath } from './parsePath';
export { parsePathMatcher } from './parsePathMatcher';
export { parsePath } from './parsePath.js';
export { parsePathMatcher } from './parsePathMatcher.js';
2 changes: 2 additions & 0 deletions packages/path-parser/src/matcherGrammar.ne
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# WARNING! This is the original grammar, but the result has been modified for typescript and ESM!

# Usage: https://nearley.js.org/
@preprocessor typescript
@{%
Expand Down
Loading

0 comments on commit 92e9118

Please sign in to comment.