Skip to content

Commit

Permalink
Added typescript typings and es modules to the npm package (#16)
Browse files Browse the repository at this point in the history
* so far...

* package esm modules update - tests failing!

* fixed babel config for tests

* use es6 for esm release

* Added typescript typings
  • Loading branch information
fraliv13 authored and oncicaradupopovici committed Nov 4, 2019
1 parent 9acf14d commit 9376abd
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 46 deletions.
85 changes: 61 additions & 24 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,74 @@
module.exports = api => {

api.cache.using(() => process.env.NODE_ENV)

const defaultAlias = {
"@totalsoft/zion": "@totalsoft/zion/src",
"@totalsoft/pure-validations": "@totalsoft/pure-validations/src"
};

const presets = api.env("test")
? [
[
"@babel/preset-env",
{
targets: { node: "current" }
}
]
]
: [];

// const presets = api.env("test")
// ? [
// [
// "@babel/preset-env",
// {
// targets: { node: "current" }
// }
// ]
// ]
// : [];

const defaultPlugins = [["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }], "@babel/plugin-proposal-optional-chaining"];

const plugins = api.env("test")
? [
...defaultPlugins,
[
"babel-plugin-module-resolver",
{
root: ["./"],
alias: defaultAlias
}
]
]
: [...defaultPlugins, ["@babel/plugin-transform-modules-commonjs"]];
// const plugins = api.env("test")
// ? [
// ...defaultPlugins,
// [
// "babel-plugin-module-resolver",
// {
// root: ["./"],
// alias: defaultAlias
// }
// ]
// ]
// : [...defaultPlugins, ["@babel/plugin-transform-modules-commonjs"]];

return {
presets,
plugins
plugins: defaultPlugins,
env: {
cjs: {
presets: [
[
"@babel/preset-env",
{
modules: "commonjs"
}
]
],
},
esm: {
plugins: [["@babel/plugin-transform-runtime", { useESModules: true }]]
},
test: {
presets: [
[
"@babel/preset-env",
{
targets: { node: "current" }
}
]
],
plugins: [
[
"babel-plugin-module-resolver",
{
root: ["./"],
alias: defaultAlias
}
]
]
}
}
};
};
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
"@babel/plugin-transform-modules-commonjs": "^7.6.0",
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/plugin-proposal-pipeline-operator": "^7.5.0",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.5.5",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.9.0",
"babel-plugin-module-resolver": "^3.0.0",
"cross-env": "^6.0.3",
"dtslint": "^0.9.3",
"eslint": "^6.2.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-jest": "^22.16.0",
Expand All @@ -26,7 +29,9 @@
"i18next": "^17.0.11",
"react": "^16.9.0",
"react-i18next": "^9.0.0",
"glob": "^7.1.2"
"glob": "^7.1.2",
"tern": "0.24.2",
"ts-toolbelt": "4.9.15"
},
"workspaces": [
"packages/*"
Expand Down
9 changes: 7 additions & 2 deletions packages/pure-validations-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
"main": "lib/index.js",
"scripts": {
"prebuild": "rimraf build",
"build": "babel src/ --out-dir build/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" && node ../../scripts/copy-files.js",
"build": "yarn build:cjs && yarn build:esm && yarn build:copy-files",
"build:cjs": "cross-env BABEL_ENV=cjs babel src/ --out-dir build/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\"",
"build:esm": "cross-env BABEL_ENV=esm babel src/ --out-dir build/esm --ignore \"**/__tests__\" --ignore \"**/__mocks__\"",
"build:copy-files": "node ../../scripts/copy-files.js",

"prepare": "yarn run build",
"test": "jest --watchAll"
"test": "jest --watchAll",
"typescript": "tslint -p tsconfig.json \"{src,test}/**/*.{ts,tsx}\""
},
"repository": {
"type": "git",
Expand Down
8 changes: 8 additions & 0 deletions packages/pure-validations-react/src/dirtyInfo/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type DirtyInfo = {
[key: string]: boolean | DirtyInfo;
};

export function create(isDirty: boolean): DirtyInfo;
export function update(propertyPath: string, propertyDirtyInfo: boolean, dirtyInfo: DirtyInfo): void;
export function merge(sourceDirtyInfo: DirtyInfo, targetDirtyInfo: DirtyInfo): void;
export function isPropertyDirty(propertyPath: string, dirtyInfo: DirtyInfo): boolean;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, act } from "@testing-library/react-hooks";
import { useDirtyFieldValidation } from "../useDirtyFieldValidation";
import { useDirtyFieldValidation } from "../";
import { Validator, Success, Failure, isValid, validate, __clearMocks } from "@totalsoft/pure-validations";
import { eject } from "../../validationProxy";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, act } from "@testing-library/react-hooks";
import { useValidation } from "../useValidation";
import { useValidation } from "../";
import { isValid, Validator, Success, Failure, validate, logTo, filterFields, __clearMocks } from "@totalsoft/pure-validations";

describe("useValidation hook", () => {
Expand Down
42 changes: 42 additions & 0 deletions packages/pure-validations-react/src/hooks/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Validator, Logger, FilterFunc, Success, Failure } from "@totalsoft/pure-validations";
import { ValidatorContext } from "packages/pure-validations/src/validator";

/**
* React hook for model validation using the @totalsof/pure-validation library.
* Returns a statefull validation result, a function that performs the validation and a function that resets the validation state.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations-react#usevalidation-hook
*/
export function useValidation(
rules: Validator<any>,
options?: { isLogEnabled: boolean; logger: Logger; fieldFilterFunc: FilterFunc },
deps?: any[]
): [
// Validation result
Success | Failure,

// Validate function
(model: any, context?: ValidatorContext) => boolean,

// Reset validation function
() => void
];

/**
* React hook that uses dirty fields info to validate only the fields that were modified.
* Returns a statefull validation result, a function that performs the validation and a function that resets the validation state.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations-react#usedirtyfieldvalidation-hook
*/
export function useDirtyFieldValidation(
rules: Validator<any>,
options?: { isLogEnabled: boolean; logger: Logger; fieldFilterFunc: FilterFunc },
deps?: any[]
): [
// Validation result
Success | Failure,

// Validate function
(model: any, dirtyinfo?: any) => boolean,

// Reset validation function
() => void
];
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useCallback } from 'react';
import { useValidation } from './useValidation'
import { useValidation } from './'
import * as di from '../dirtyInfo';

export function useDirtyFieldValidation(rules, options = {}, deps = []) {
Expand Down
3 changes: 3 additions & 0 deletions packages/pure-validations-react/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./hooks";
export * from "./dirtyInfo";
export * from "./validationProxy";
14 changes: 14 additions & 0 deletions packages/pure-validations-react/src/validationProxy/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Success, Failure } from "@totalsoft/pure-validations";

export function eject(validationProxy: ValidationProxy): object;

export function getErrors(validationProxy: ValidationProxy, separator: string): string;

export function isValid(validationProxy: ValidationProxy): boolean;

export type Proxy<T> = {
[k in keyof T]: T[k];
};

export type ValidationProxy = Proxy<Success | Failure>;
export function ValidationProxy(validation: Success | Failure): ValidationProxy;
3 changes: 3 additions & 0 deletions packages/pure-validations-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig"
}
9 changes: 7 additions & 2 deletions packages/pure-validations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
"main": "index.js",
"scripts": {
"prebuild": "rimraf build",
"build": "babel src/ --out-dir build/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\" && node ../../scripts/copy-files.js",
"build": "yarn build:cjs && yarn build:esm && yarn build:copy-files",
"build:cjs": "cross-env BABEL_ENV=cjs babel src/ --out-dir build/ --ignore \"**/__tests__\" --ignore \"**/__mocks__\"",
"build:esm": "cross-env BABEL_ENV=esm babel src/ --out-dir build/esm --ignore \"**/__tests__\" --ignore \"**/__mocks__\"",
"build:copy-files": "node ../../scripts/copy-files.js",

"prepare": "yarn run build",
"test": "jest --watchAll"
"test": "jest --watchAll",
"typescript": "tslint -p tsconfig.json \"{src,test}/**/*.{ts,tsx}\""
},
"author": "TotalSoft",
"homepage": "https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations",
Expand Down
115 changes: 115 additions & 0 deletions packages/pure-validations/src/higherOrderValidators/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { ValidationError } from "../index";
import { Validator, ValidatorContext } from "../validator";

/**
* Used to reduce a list of validators. It calls all validators and concatenate their failures.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#concatfailure
*/
export function concatFailure<TModel>(validators: Array<Validator<TModel>>): Validator<TModel>;

/**
* Sets a custom error message for validation errors. The message can also be a translation key.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#errorMessage
*/
export function errorMessage<TModel>(message: string, validator: Validator<TModel>): Validator<TModel>;

/**
* Sets a custom error message for validation errors. The message can also be a translation key.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#errorMessage
*/
export function errorMessage<TModel>(message: string): (validator: Validator<TModel>) => Validator<TModel>;

/**
* Used to validate just a field of the model.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#field
*/
export function field(key: string, validator: Validator<any>): Validator<object>;

/**
* Used to validate just a field of the model.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#field
*/
export function field(key: string): (validator: Validator<any>) => Validator<object>;

export type FilterFunc = (ctx: ValidatorContext) => boolean;

/**
* Applies validation only on the fields filtered according to the speciffied predicate.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#filterFields
*/
export function filterFields(fieldFilter: FilterFunc, validator: Validator<object>): Validator<object>;

/**
* Applies validation only on the fields filtered according to the speciffied predicate.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#filterFields
*/
export function filterFields(fieldFilter: FilterFunc): (validator: Validator<object>) => Validator<object>;

/**
* Useful when you need the model in the composition process.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#frommodel
*/
export function fromModel<TModel>(validatorFactory: (model: TModel) => Validator<TModel>): Validator<TModel>;

/**
* Useful when you need the parent model in the composition process.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#fromparent
*/
export function fromParent<TModel>(validatorFactory: (parentModel: any) => Validator<TModel>): Validator<TModel>;

/**
* Useful when you need the root model in the composition process.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#fromroot
*/
export function fromRoot<TModel>(validatorFactory: (rootModel: any) => Validator<TModel>): Validator<TModel>;

/**
* Takes an item validator and produces a validator for each item in the provided collection.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#items
*/
export function items<TItem>(itemValidator: Validator<TItem>): Validator<TItem[]>;

export type Logger = { log: (message: string) => void };

/**
* Logs the validation process to the speficfied logger.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#logto
*/
export function logTo<TModel>(logger: Logger, validator: Validator<TModel>): Validator<TModel>;

/**
* Logs the validation process to the speficfied logger.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#logto
*/
export function logTo<TModel>(logger: Logger): (validator: Validator<TModel>) => Validator<TModel>;

export type Reader<TContext, TValue> = { computation: (ctx: TContext) => TValue };
export function parent(reader: Reader<any, any>): Reader<any, any>;

export function readFrom<TContext, TValue>(func: (ctx: TContext) => TValue): Reader<TContext, TValue>;

/**
* Used to compose a complex validator from field validators.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#shape
*/
export function shape<TValidatorObj extends { [k: string]: Validator<unknown> }>(
validatorObj: TValidatorObj
): Validator<{ [k in keyof TValidatorObj]: TValidatorObj[k] extends Validator<infer U> ? U : unknown }>;

/**
* Used to reduce a list of validators. It calls all the validators until it receives a failure.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#stoponfirstfailure
*/
export function stopOnFirstFailure<TModel>(validators: Array<Validator<TModel>>): Validator<TModel>;

/**
* Used to create a conditional validator by providing a predicate or condition and a validator.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#stoponfirstfailure
*/
export function when<TModel>(condition: boolean | ((model: TModel, ctx?: any) => boolean), validator: Validator<TModel>): Validator<TModel>;

/**
* Used to create a conditional validator by providing a predicate or condition and a validator.
* @see https://github.com/osstotalsoft/jsbb/tree/master/packages/pure-validations#stoponfirstfailure
*/
export function when<TModel>(condition: boolean | ((model: TModel, ctx?: any) => boolean)): (validator: Validator<TModel>) => Validator<TModel>;
5 changes: 5 additions & 0 deletions packages/pure-validations/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./higherOrderValidators";
export * from "./primitiveValidators";
export { Failure, Success, isValid, getErrors, getInner } from "./validation";
export { Validator, validate } from "./validator";
export { default as ValidationError } from "./validationError";
21 changes: 21 additions & 0 deletions packages/pure-validations/src/primitiveValidators/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Validator } from "../index";

export const atLeastOne: Validator<any[]>;

export const email: Validator<string>;

export function greaterThan(min: number): Validator<number>;

export function lessThan(max: number): Validator<number>;

export function between(min: number, max: number): Validator<number>;

export function matches(regex: RegExp): Validator<string>;

export function maxLength(max: number): Validator<string>;

export function minLength(min: number): Validator<string>;

export const required: Validator<any>;

export function unique(selector: string | string[], displayName: string): Validator<any[]>;
Loading

0 comments on commit 9376abd

Please sign in to comment.