Skip to content

Commit

Permalink
major: stateful validations (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed May 22, 2020
1 parent de7498b commit 7872203
Show file tree
Hide file tree
Showing 126 changed files with 5,425 additions and 2,284 deletions.
59 changes: 59 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports = {
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 10,
sourceType: 'module',
ecmaFeatures: {
impliedStrict: true,
},
babelOptions: {
configFile: 'config/babel/babel.config.js',
},
},
plugins: ['jest'],
env: {
es6: true,
jest: true,
node: true,
},
globals: {
VEST_VERSION: true,
},
extends: [
'eslint:recommended',
'plugin:jest/recommended',
'plugin:jest/style',
'plugin:import/errors',
'plugin:import/warnings',
'prettier',
],
rules: {
'no-trailing-spaces': [2, { ignoreComments: false }],
'no-implicit-globals': 2,
'import/no-self-import': 2,
'import/no-useless-path-segments': 2,
'import/order': [
'error',
{
alphabetize: {
order: 'asc',
},
pathGroupsExcludedImportTypes: ['builtin'],
},
],
'import/newline-after-import': 2,
'no-console': 2,
'no-multi-spaces': 1,
'no-warning-comments': 2,
'no-useless-return': 2,
'prefer-const': 2,
'prefer-arrow-callback': 2,
'no-var': 2,
'no-useless-computed-key': 2,
'jest/expect-expect': 0,
'jest/no-identical-title': 0,
'arrow-body-style': [2, 'as-needed'],
'object-shorthand': [2, 'always', { avoidQuotes: true }],
},
ignorePatterns: ['dist', 'node_modules', 'playground'],
};
45 changes: 0 additions & 45 deletions .eslintrc.json

This file was deleted.

8 changes: 6 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ packages/
├── vest/ # Main Vest package.
│ ├── src/ # Vest's prebuilt source code.
│ │ └── core/ # Modules required for vest's functionality.
│ │ │ └── Context/ # Vest's shared runtime, Used across the whole library.
│ │ │ └── Context/ # Vest's shared runtime. Used across the whole library.
│ │ │ └── createSuite/ # Initializes the suite and creates a context and state.
│ │ │ └── produce/ # Generates the out put object and callbaks.
│ │ │ └── state/ # Vest's persistent state. Used across the whole library.
│ │ │ └── suiteResult/ # The output object is generated here.
│ │ │ └── test/ # Contains the test function and its lifecycle.
│ │ │ └── validate/ # Initializes the suite and creates a context.
│ │ │ └── validate/ # Creates and runs a stateless suite.
│ │ └── hooks/ # Functions that extend vest's functionality. They all use Context.
│ │ │ └── draft/ # Allows access to the intermediate test result.
│ │ │ └── exclusive/ # Allows including or excluding fields in runtime.
│ │ │ └── warn/ # Allows setting warn-only fields.
│ │ └── lib/ # Shared helper functions.
│ │ └── testUtils/ # Test helper functions.
├── eslint-plugin-vest/ # Eslint plugin with vest specific rules
│ ├── lib/ # Contains all rules
```
Expand Down
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Vest](https://cdn.jsdelivr.net/gh/ealush/vest@assets/logo.png "Vest")
![Vest](https://cdn.jsdelivr.net/gh/ealush/vest@assets/logo.png 'Vest')

# Vest - Validation Testing

Expand All @@ -12,44 +12,48 @@
## What is Vest?

Vest is a validations library for JS apps that derives its syntax from modern JS frameworks such as Mocha or Jest. It is easy to learn due to its use of already common declarative patterns.
It works great with user-input validation and with validating upon user interaction to provide the best possible user experience.

The idea behind Vest is that your validations can be described as a 'spec' or a contract that reflects your form or feature structure. Your validations run in production, and they are framework agnostic - meaning Vest works well with React, Angular, Vue, or even without a framework at all.

**Example code:**

```js
// validation.js
import { validate, test, enforce } from "vest";
import vest, { test, enforce } from 'vest';

const validation = (data) =>
validate("NewUserForm", () => {
test("username", "Must be at least 3 chars", () => {
const validation = data => {
const validate = vest.create('NewUserForm', () => {
test('username', 'Must be at least 3 chars', () => {
enforce(data.username).longerThanOrEquals(3);
});

test("email", "Is not a valid email address", () => {
test('email', 'Is not a valid email address', () => {
enforce(data.email)
.isNotEmpty()
.matches(/[^@]+@[^\.]+\..+/g);
});
});

return validate();
};

export default validation;
```

```js
// myFeature.js
import validation from "./validation.js";
import validation from './validation.js';

const res = validation({
username: "example",
email: "email@example.com",
username: 'example',
email: 'email@example.com',
});

res.hasErrors(); // returns whether the form has errors
res.hasErrors("username"); // returns whether the 'username' field has errors
res.hasErrors('username'); // returns whether the 'username' field has errors
res.getErrors(); // returns an object with an array of errors per field
res.getErrors("username"); // returns an array of errors for the `username` field
res.getErrors('username'); // returns an array of errors for the `username` field
```

## Why Vest?
Expand Down
14 changes: 8 additions & 6 deletions config/babel/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module.exports = (api) => {
module.exports = api => {
if (api) {
api.cache(true);
}

const presets = ["@babel/preset-env"];
const presets = ['@babel/preset-env'];

const plugins = [
"babel-plugin-add-module-exports",
"@babel/plugin-transform-object-assign",
"@babel/plugin-proposal-optional-chaining",
'babel-plugin-add-module-exports',
'@babel/plugin-transform-object-assign',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
['@babel/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],
];

return {
include: [/src/, /node_modules/],
include: [/src/, /testUtils/, /node_modules/],
presets,
plugins,
};
Expand Down
32 changes: 16 additions & 16 deletions config/builds/vest.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import path from "path";
import compiler from "@ampproject/rollup-plugin-closure-compiler";
import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
import { terser } from "rollup-plugin-terser";
import path from 'path';
import compiler from '@ampproject/rollup-plugin-closure-compiler';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import replace from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';

const { BABEL_CONFIG_PATH } = require("..");
const { BABEL_CONFIG_PATH } = require('..');

const { PACKAGE_NAME_VEST } = require("../../scripts/constants");
const { packagePath, packageJson } = require("../../util");
const { PACKAGE_NAME_VEST } = require('../../scripts/constants');
const { packagePath, packageJson } = require('../../util');

const LIBRARY_NAME = PACKAGE_NAME_VEST;
const PACKAGE_PATH = path.resolve(packagePath(LIBRARY_NAME));

const DEFAULT_FORMAT = "umd";
const DEFAULT_FORMAT = 'umd';

const { version } = packageJson(PACKAGE_NAME_VEST);

Expand All @@ -34,16 +34,16 @@ const PLUGINS = [
];

const buildConfig = ({ format = DEFAULT_FORMAT, min = false } = {}) => ({
input: path.join(PACKAGE_PATH, "src/index.js"),
input: path.join(PACKAGE_PATH, 'src/index.js'),
output: {
file: [
path.join(PACKAGE_PATH, "dist", LIBRARY_NAME),
min && "min",
path.join(PACKAGE_PATH, 'dist', LIBRARY_NAME),
min && 'min',
format !== DEFAULT_FORMAT && format,
"js",
'js',
]
.filter(Boolean)
.join("."),
.join('.'),
name: LIBRARY_NAME,
format,
},
Expand Down
Loading

0 comments on commit 7872203

Please sign in to comment.