Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: expose validators as package submodule #83

Merged
merged 3 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
node_modules
node_modules_
dist
__dist
__src
*.log
.vscode
.idea
.DS_Store
validators
57 changes: 34 additions & 23 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
All notable changes to this project will be documented in this file. See
[standard-version](https://github.com/conventional-changelog/standard-version)
for commit guidelines.

### [0.1.25](https://github.com/VirtusLab/formts/compare/v0.1.24...v0.1.25) (2021-03-19)

Expand All @@ -11,77 +13,86 @@ All notable changes to this project will be documented in this file. See [standa

### [0.1.24](https://github.com/VirtusLab/formts/compare/v0.1.23...v0.1.24) (2021-03-17)


### Bug Fixes

* typing improvements ([#81](https://github.com/VirtusLab/formts/issues/81)) ([a9ce089](https://github.com/VirtusLab/formts/commit/a9ce0891dc27a09c8061ec5c7dbb0615e1ddc75e))
- typing improvements ([#81](https://github.com/VirtusLab/formts/issues/81))
([a9ce089](https://github.com/VirtusLab/formts/commit/a9ce0891dc27a09c8061ec5c7dbb0615e1ddc75e))

### [0.1.23](https://github.com/VirtusLab/formts/compare/v0.1.22...v0.1.23) (2021-02-26)


### Features

* **examples:** complex form ([#77](https://github.com/VirtusLab/formts/issues/77)) ([8f3f8eb](https://github.com/VirtusLab/formts/commit/8f3f8eb71a569f66ff18a122f91304945aba7e71))
* **validators:** combine util ([#71](https://github.com/VirtusLab/formts/issues/71)) ([15a7aee](https://github.com/VirtusLab/formts/commit/15a7aeee18a6caffaa622cb33bb94160da22dcb3))

- **examples:** complex form
([#77](https://github.com/VirtusLab/formts/issues/77))
([8f3f8eb](https://github.com/VirtusLab/formts/commit/8f3f8eb71a569f66ff18a122f91304945aba7e71))
- **validators:** combine util
([#71](https://github.com/VirtusLab/formts/issues/71))
([15a7aee](https://github.com/VirtusLab/formts/commit/15a7aeee18a6caffaa622cb33bb94160da22dcb3))

### Bug Fixes

* initial values can lead to illeagl state ([#74](https://github.com/VirtusLab/formts/issues/74)) ([6c02400](https://github.com/VirtusLab/formts/commit/6c02400ddc9bf8c586c7ec91c5869b9207600815))
- initial values can lead to illeagl state
([#74](https://github.com/VirtusLab/formts/issues/74))
([6c02400](https://github.com/VirtusLab/formts/commit/6c02400ddc9bf8c586c7ec91c5869b9207600815))

### [0.1.22](https://github.com/VirtusLab/formts/compare/v0.1.21...v0.1.22) (2021-02-25)

### [0.1.21](https://github.com/VirtusLab/formts/compare/v0.1.20...v0.1.21) (2021-02-25)


### Bug Fixes

* build ([#70](https://github.com/VirtusLab/formts/issues/70)) ([6899305](https://github.com/VirtusLab/formts/commit/6899305b362a6a027d637725b4bfd4ab2a686116))
- build ([#70](https://github.com/VirtusLab/formts/issues/70))
([6899305](https://github.com/VirtusLab/formts/commit/6899305b362a6a027d637725b4bfd4ab2a686116))

### [0.1.20](https://github.com/VirtusLab/formts/compare/v0.1.19...v0.1.20) (2021-02-22)


### Features

* debounced validation ([#68](https://github.com/VirtusLab/formts/issues/68)) ([3a04328](https://github.com/VirtusLab/formts/commit/3a043287e805a6496612603176b2a402a0705786))
* validation optimisations and fixes ([#67](https://github.com/VirtusLab/formts/issues/67)) ([a4c2581](https://github.com/VirtusLab/formts/commit/a4c25812ded1fb6bc35d9b066ec0cd3c7036009c))
- debounced validation ([#68](https://github.com/VirtusLab/formts/issues/68))
([3a04328](https://github.com/VirtusLab/formts/commit/3a043287e805a6496612603176b2a402a0705786))
- validation optimisations and fixes
([#67](https://github.com/VirtusLab/formts/issues/67))
([a4c2581](https://github.com/VirtusLab/formts/commit/a4c25812ded1fb6bc35d9b066ec0cd3c7036009c))

### [0.1.19](https://github.com/VirtusLab/formts/compare/v0.1.18...v0.1.19) (2021-02-15)

### [0.1.18](https://github.com/VirtusLab/formts/compare/v0.1.17...v0.1.18) (2021-02-09)


### Features

* atom-based form state ([#63](https://github.com/VirtusLab/formts/issues/63)) ([b2b90b2](https://github.com/VirtusLab/formts/commit/b2b90b28c495422f30830a0be8664755e119b518))
- atom-based form state ([#63](https://github.com/VirtusLab/formts/issues/63))
([b2b90b2](https://github.com/VirtusLab/formts/commit/b2b90b28c495422f30830a0be8664755e119b518))

### [0.1.17](https://github.com/VirtusLab/formts/compare/v0.1.16...v0.1.17) (2021-01-21)


### Bug Fixes

* **core:** parsing date instance initial value ([#61](https://github.com/VirtusLab/formts/issues/61)) ([34b7818](https://github.com/VirtusLab/formts/commit/34b7818a4ae72fd9b44544b510ada81b87bb6998))
- **core:** parsing date instance initial value
([#61](https://github.com/VirtusLab/formts/issues/61))
([34b7818](https://github.com/VirtusLab/formts/commit/34b7818a4ae72fd9b44544b510ada81b87bb6998))

### [0.1.16](https://github.com/VirtusLab/formts/compare/v0.1.15...v0.1.16) (2021-01-18)


### Features

* **utils:** lenses & atoms ([#60](https://github.com/VirtusLab/formts/issues/60)) ([d70c61b](https://github.com/VirtusLab/formts/commit/d70c61b531ffc92dfc35106871a31ff9e8f91519))
- **utils:** lenses & atoms
([#60](https://github.com/VirtusLab/formts/issues/60))
([d70c61b](https://github.com/VirtusLab/formts/commit/d70c61b531ffc92dfc35106871a31ff9e8f91519))

### [0.1.15](https://github.com/VirtusLab/formts/compare/v0.1.14...v0.1.15) (2021-01-18)


### Features

* handle change method ([#58](https://github.com/VirtusLab/formts/issues/58)) ([a013729](https://github.com/VirtusLab/formts/commit/a0137297d112c4ac7626a666e3c57cbfa9e29fb7))
- handle change method ([#58](https://github.com/VirtusLab/formts/issues/58))
([a013729](https://github.com/VirtusLab/formts/commit/a0137297d112c4ac7626a666e3c57cbfa9e29fb7))

### [0.1.14](https://github.com/VirtusLab/formts/compare/v0.1.13...v0.1.14) (2021-01-11)


### Features

* validation dependencies support ([#57](https://github.com/VirtusLab/formts/issues/57)) ([22bbb6a](https://github.com/VirtusLab/formts/commit/22bbb6a2f9c505c02bf75122f599b16cbaa055d9))
- validation dependencies support
([#57](https://github.com/VirtusLab/formts/issues/57))
([22bbb6a](https://github.com/VirtusLab/formts/commit/22bbb6a2f9c505c02bf75122f599b16cbaa055d9))

### [0.1.13](https://github.com/VirtusLab/formts/compare/v0.1.12...v0.1.13) (2020-11-29)

Expand Down
21 changes: 10 additions & 11 deletions examples/advanced-forms/change-password-form/src/example.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {
BaseErrors,
createFormSchema,
createFormValidator,
FormProvider,
useField,
useFormController,
useFormHandle,
useFormValues,
validators as v,
} from "@virtuslab/formts";
import * as V from "@virtuslab/formts/validators";
import React from "react";
import ReactDOM from "react-dom";

Expand All @@ -26,16 +25,16 @@ const Schema = createFormSchema(
);

type FormError =
| BaseErrors.Required
| v.ErrorType<typeof passwordMultiValidator>
| V.Errors.Required
| V.ErrorType<typeof passwordMultiValidator>
| { code: "confirmPassMismatch" }
| { code: "invalidCurrentPass" };

const passwordMultiValidator = v.combine(
const passwordMultiValidator = V.combine(
[
v.minLength(MIN_PASS_LEN),
v.hasLowerCaseChar(),
v.hasUpperCaseChar(),
V.minLength(MIN_PASS_LEN),
V.hasLowerCaseChar(),
V.hasUpperCaseChar(),
val => (val.includes("42") ? null : ({ code: "hasTheAnswer" } as const)),
],
([minLen, lowerChar, upperChar, hasTheAnswer]) => ({
Expand All @@ -50,11 +49,11 @@ const passwordMultiValidator = v.combine(
);

const validator = createFormValidator(Schema, validate => [
validate(Schema.currentPass, v.required()),
validate(Schema.currentPass, V.required()),

validate(Schema.newPass, passwordMultiValidator),

validate(Schema.newPassConfirm, v.required()),
validate(Schema.newPassConfirm, V.required()),
validate({
field: Schema.newPassConfirm,
dependencies: [Schema.newPass],
Expand Down Expand Up @@ -103,7 +102,7 @@ const NewPassInput: React.FC = () => {
const field = useField(Schema.newPass);

const renderRequirement = (
rule: keyof v.ErrorType<typeof passwordMultiValidator>["rules"],
rule: keyof V.ErrorType<typeof passwordMultiValidator>["rules"],
label: string
) => {
const ruleError =
Expand Down
8 changes: 4 additions & 4 deletions examples/basic-forms/3-basic-form-validation/src/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
useField,
useFormValues,
FormProvider,
validators,
} from "@virtuslab/formts";
import { withError, required, minValue } from "@virtuslab/formts/validators";
import React from "react";
import ReactDOM from "react-dom";

Expand All @@ -27,13 +27,13 @@ const validator = createFormValidator(Schema, validate => [
validate(
Schema.name,
// you can mix built-in validators with custom functions
validators.withError(validators.required(), "Required"),
withError(required(), "Required"),
val => (val.length > 10 ? "Name must be max 10 characters long" : null)
),
validate(
Schema.age,
validators.withError(validators.required(), "Required"),
validators.withError(validators.minValue(18), "Age must be at least 18")
withError(required(), "Required"),
withError(minValue(18), "Age must be at least 18")
),
]);

Expand Down
8 changes: 4 additions & 4 deletions examples/basic-forms/4-basic-form-mui/src/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
useField,
useFormValues,
FormProvider,
validators,
} from "@virtuslab/formts";
import { withError, required, minValue } from "@virtuslab/formts/validators";
import React from "react";
import ReactDOM from "react-dom";

Expand All @@ -28,13 +28,13 @@ const validator = createFormValidator(Schema, validate => [
validate(
Schema.name,
// you can mix built-in validators with custom functions
validators.withError(validators.required(), "Required"),
withError(required(), "Required"),
val => (val.length > 10 ? "Name must be max 10 characters long" : null)
),
validate(
Schema.age,
validators.withError(validators.required(), "Required"),
validators.withError(validators.minValue(18), "Age must be at least 18")
withError(required(), "Required"),
withError(minValue(18), "Age must be at least 18")
),
]);

Expand Down
4 changes: 2 additions & 2 deletions examples/basic-forms/5-complex-form-mui/src/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
useField,
useFormValues,
FormProvider,
validators,
} from "@virtuslab/formts";
import { withError, required } from "@virtuslab/formts/validators";
import React from "react";
import ReactDOM from "react-dom";

Expand Down Expand Up @@ -60,7 +60,7 @@ const validator = createFormValidator(Schema, validate => [
validate(
Schema.name,
// you can mix built-in validators with custom functions
validators.withError(validators.required(), "Required"),
withError(required(), "Required"),
val => (val.length > 10 ? "Name must be max 10 characters long" : null)
),
validate({
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"author": "Mikołaj Klaman <mklaman@virtuslab.com>",
"license": "MIT",
"private": false,
"main": "dist/index.js",
"module": "dist/formts.esm.js",
"typings": "dist/index.d.ts",
"main": "__dist/index.js",
"module": "__dist/esm/index.js",
"typings": "__dist/index.d.ts",
"side-effects": false,
"files": [
"dist",
"src"
"__dist",
"__src",
"validators"
],
"keywords": [
"react",
Expand All @@ -23,13 +24,15 @@
"scripts": {
"start": "tsdx watch",
"typecheck": "tsc -p ./tsconfig.json",
"build": "yarn typecheck && tsdx build",
"build": "yarn typecheck && tsdx build && sh ./scripts/postbuild.sh",
"test": "tsdx test",
"test:ci": "tsdx test --ci --runInBand",
"lint": "tsdx lint",
"prepare": "tsdx build",
"prepare": "yarn build",
"size": "size-limit",
"analyze": "size-limit --why",
"prepack": "sh ./scripts/prepack.sh",
"postpack": "sh ./scripts/postpack.sh",
"release": "standard-version --releaseCommitMessageFormat \"chore(release): {{currentTag}} [ci skip]\""
},
"husky": {
Expand Down
13 changes: 13 additions & 0 deletions scripts/postbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# --- 1. rename dist into __dist to signal that it is not intended for imports
rm -rf ./__dist

mv ./dist ./__dist

# --- 2. prepare root validators dir to enable imports: 'import {} from "@virtuslab/formts/validators"'

mkdir ./validators

# 'dist/esm/index2' is a chunk created for 'src/validators/index.ts'
echo 'export * from "../__dist/esm/index2";' > ./validators/index.js

echo 'export * from "../__dist/validators";' > ./validators/index.d.ts
3 changes: 3 additions & 0 deletions scripts/postpack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# --- cleanup directories created by prepack.sh

rm -rf ./__src
6 changes: 6 additions & 0 deletions scripts/prepack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# --- copy src into __src to signal that it is not intended for imports

rm -rf ./__src

cp -r ./src ./__src

2 changes: 1 addition & 1 deletion src/core/builders/create-form-validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert, IsExact } from "conditional-type-checks";

import { Task } from "../../utils/task";
import { validators } from "../../validators";
import * as validators from "../../validators";
import { FieldDescriptor } from "../types/field-descriptor";
import { FormValidator } from "../types/form-validator";
import { impl } from "../types/type-mapper-util";
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./core";
export * from "./validators";
36 changes: 19 additions & 17 deletions src/validators/base-errors.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { Primitive } from "../utils/utility-types";

export type Required = { code: "required" };
export type OneOf = { code: "oneOf"; allowedValues: Primitive[] };
export namespace Errors {
export type Required = { code: "required" };
export type OneOf = { code: "oneOf"; allowedValues: Primitive[] };

export type Integer = { code: "integer" };
export type MinValue = { code: "minValue"; min: number };
export type MaxValue = { code: "maxValue"; max: number };
export type GreaterThan = { code: "greaterThan"; threshold: number };
export type LesserThan = { code: "lesserThan"; threshold: number };
export type Integer = { code: "integer" };
export type MinValue = { code: "minValue"; min: number };
export type MaxValue = { code: "maxValue"; max: number };
export type GreaterThan = { code: "greaterThan"; threshold: number };
export type LesserThan = { code: "lesserThan"; threshold: number };

export type Pattern = { code: "pattern"; regex: RegExp };
export type HasSpecialChar = { code: "hasSpecialChar" };
export type HasUpperCaseChar = { code: "hasUpperCaseChar" };
export type HasLowerCaseChar = { code: "hasLowerCaseChar" };
export type Pattern = { code: "pattern"; regex: RegExp };
export type HasSpecialChar = { code: "hasSpecialChar" };
export type HasUpperCaseChar = { code: "hasUpperCaseChar" };
export type HasLowerCaseChar = { code: "hasLowerCaseChar" };

export type MinLength = { code: "minLength"; min: number };
export type MaxLength = { code: "maxLength"; max: number };
export type ExactLength = { code: "exactLength"; expected: number };
export type MinLength = { code: "minLength"; min: number };
export type MaxLength = { code: "maxLength"; max: number };
export type ExactLength = { code: "exactLength"; expected: number };

export type ValidDate = { code: "validDate" };
export type MinDate = { code: "minDate"; min: Date };
export type MaxDate = { code: "maxDate"; max: Date };
export type ValidDate = { code: "validDate" };
export type MinDate = { code: "minDate"; min: Date };
export type MaxDate = { code: "maxDate"; max: Date };
}
6 changes: 2 additions & 4 deletions src/validators/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
// TODO: tree-shaking compatible way of importing validators
export * as validators from "./validators";

export * as BaseErrors from "./base-errors";
export * from "./validators";
export * from "./base-errors";
Loading