-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added typescript typings and es modules to the npm package (#16)
* so far... * package esm modules update - tests failing! * fixed babel config for tests * use es6 for esm release * Added typescript typings
- Loading branch information
1 parent
9acf14d
commit 9376abd
Showing
24 changed files
with
648 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
] | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
2 changes: 1 addition & 1 deletion
2
packages/pure-validations-react/src/hooks/__tests__/useDirtyFieldValidation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/pure-validations-react/src/hooks/__tests__/useValidation.tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; |
2 changes: 1 addition & 1 deletion
2
packages/pure-validations-react/src/hooks/useDirtyFieldValidation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
packages/pure-validations-react/src/validationProxy/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/pure-validations/src/higherOrderValidators/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
packages/pure-validations/src/primitiveValidators/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]>; |
Oops, something went wrong.