Skip to content

Commit 3f527c1

Browse files
Structure 2 (#130)
* Bump major to alpha * Move Node version to 10 * Replace Object.assign with spread * Replace every var with let or const * Migrate tests that run on Node to use Jest * Update Node version on Travis * Base on Node 8 instead of 10 * Change linter to accept jest tests * Update joi without considering cases where lazy schema is needed * Handle dynamic types validations with link + shared API * Begin to refactor to a new internal structure * Rename descriptors concept to definitions * Implement initialization * Implement coercion for non-array types * Adjust tests to coercion without considering nested * Implement coercion for array types * Allow to subclass structures and inherit attributes * Implement strict mode (missing validation) * Implement validation to new core (#102) * Implement validation * Implement serialization (#101) * Implement validation for nested types * Implement clone to the new core (#103) * Implement clone to the new core * Make linter happy * Increase coverage * Refactors Descriptors to be a function instead of a class * Improve function naming for better debugging * Ensure that when reusing an attribute, it uses the new schema * Reformat according to prettier * Upgrade browser build (#105) * Webpack setup and begin to setup tests * Add electron to dev dependencies * Improve Webpack output during tests * Add browser tests to Travis * Remove unecessary dependencies * Update readme about browser and support * Separate travis lifecycles * Attempt to avoid hanging electron test * Update coveralls * Move to lerna (#106) * Move current code to packages * Share prettier config * Add coveralls to root package * Fix Coveralls script * Add coverage to root package * Create jest structure (#107) * Begin to implement assertions * Migrate boolean validation assertions to use jest-structure * Assert no expected for valid/invalid matchers * Assert attribute path is passed * Add package to automatically add assertions to jest * Begin to write tests * Add tests for validating error messages * Reorganize and begin to write toHaveInvalidAttributes * Implement toHaveInvalidAttributes and cover with tests * Rename auto.js to extend-expect.js * Refactor checking of path presence and usage hint * Refactor and remove hint from validity check of toHaveInvalidAttributes * Remove hint from validity check of toHaveInvalidAttribute * Refactor checking of attribute validity * Refactor usage error and remove hint for non-usage-related errors * Replace usage of assertValid with expect().toBeValid() * Replace validitationMatchers with jest-structure in array specs * Migrate whole structure spec to use jest-structure * Rename validity assertions to contain 'structure' in the name * Fix coverage script * Add tests for when using arrayContaining with jest-structure * Add readme * Allow defaulting when null and not nullable (#108) * Fallback to default when value is null and not nullable * Update changelog * Update docs about nullable attributes * Refactor name of dynamic default instance variable * Update changelog (#109) * Allow to pass custom identifier for dynamic types (#111) * Allow to pass custom identifier for dynamic types * Improve documentation wording * Improve changelog wording * Apply suggestions from code review Co-Authored-By: Leonardo Negreiros de Oliveira <[email protected]> * Update docs/schema-concept/circular-references-and-dynamic-types.md Co-Authored-By: Leonardo Negreiros de Oliveira <[email protected]> * Allow to declare custom setter and getter (#112) * Allow to use custom setters and getters to attributes * Add docs about custom getters and setters * Allow to inherit custom accessors * reduce function complexity * Add docs about custom accessors and inheritance * update joi (#114) * Add alpha patch (#115) * Make versioning consistent (#116) * Update dependencies node and generate build (#117) * Update dependencies and Node * Generate build * Remove Node 8 from Travis build * Make linter happy * Add missing symlinks (#118) * Invert symlinks (#119) * Invert symlinks * Bump alpha patch * Gix Gitbook refs (#120) * Gix Gitbook refs * New organization * Remove introduction duplicate link * Prepare to release new alpha * Gitbook testing docs (#124) * Add testing to docs and prettify md files * Fix formatting * Publish only src folder for jest-structure (#125) * Update Joi links (#126) * Allow to disable coercion (#127) * Update paths and messages of errors in docs (#128) * Improve docs (#129) * Improve docs * Improve verbiage * Update build * Bump version to 2.0.0 * Point links to master docs * Update changelog and add migration guide * Point CodeClimate to use eslint 6 * Use normal quotes where needed Co-authored-by: Leonardo Negreiros de Oliveira <[email protected]>
1 parent b48d1ba commit 3f527c1

File tree

178 files changed

+15349
-7322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+15349
-7322
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.codeclimate.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
engines:
22
eslint:
33
enabled: true
4-
channel: "eslint-3"
4+
channel: 'eslint-6'
55

66
ratings:
77
paths:
8-
- src/**
8+
- packages/structure/src/**
99

1010
exclude_paths:
11-
- benchmark/**/*
12-
- dist/**/*
13-
- test/**/*
11+
- benchmark/**/*
12+
- packages/structure/dist/**/*
13+
- packages/structure/test/**/*

.eslintrc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
"env": {
33
"node": true,
44
"es6": true,
5-
"mocha": true
5+
"jest/globals": true
6+
},
7+
"extends": ["eslint:recommended", "plugin:jest/recommended", "prettier"],
8+
"plugins": ["jest"],
9+
"parserOptions": {
10+
"ecmaVersion": 2018
611
},
7-
"extends": ["eslint:recommended", "prettier"],
8-
"plugins": ["mocha"],
912
"rules": {
1013
"comma-spacing": ["error", { "before": false, "after": true }],
1114
"complexity": ["error", 4],
@@ -16,8 +19,13 @@
1619
"always",
1720
{ "avoidQuotes": true, "avoidExplicitReturnArrows": true }
1821
],
19-
"mocha/no-exclusive-tests": "error",
20-
"mocha/no-skipped-tests": "error",
22+
"jest/no-disabled-tests": "warn",
23+
"jest/no-focused-tests": "error",
24+
"jest/no-identical-title": "error",
25+
"jest/prefer-to-have-length": "warn",
26+
"jest/valid-expect": "error",
27+
"jest/no-test-callback": "off",
28+
"jest/no-try-expect": "off",
2129
"quotes": ["error", "single", { "avoidEscape": true }],
2230
"semi": ["error", "always"]
2331
}

.gitbook.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
structure:
2-
readme: README.md
2+
readme: packages/structure/README.md
33
summary: docs/SUMMARY.md

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10.13.0

.prettierignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
/dist
2-
/README.md
3-
/CHANGELOG.md
1+
dist
File renamed without changes.

.travis.yml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
language: node_js
22
node_js:
3-
- "node"
4-
- "6.1"
5-
- "6.0"
6-
- "8.0"
3+
- '10'
4+
- '11'
5+
- '12'
6+
- 'node'
7+
8+
addons:
9+
apt:
10+
packages:
11+
- xvfb
12+
13+
install:
14+
- yarn
15+
- export DISPLAY=':99.0'
16+
- Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
17+
18+
before_script:
19+
- yarn test:browser:build
20+
721
script:
8-
- npm run lint
9-
- npm test
10-
- npm run coveralls
22+
- yarn lint
23+
- yarn test
24+
- yarn test:browser:run
25+
- yarn coveralls

CHANGELOG.md

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,129 @@
1+
## 2.0.0 - 2020-03-31
2+
3+
Refactors:
4+
5+
- The whole part of schemas and attribute definitions was refactored
6+
- Tests are now run by Jest (and Electron for browser tests)
7+
- Prettier was added
8+
- Move to mono-repo
9+
10+
Enhancements
11+
12+
- Implement jest-structure assertions
13+
- It's possible to set custom getters e setters directly in the structure class
14+
- Allows to disable coercion
15+
16+
Breaking changes:
17+
18+
- Joi is updated to v16
19+
- Attribute path in validation _errors_ is an array instead of a string
20+
- Attribute path in validation _messages_ contains the whole path joined by '.'
21+
- The name used for the dynamic import should aways be the same as the name of its type or else a custom identifier must be used
22+
- Non-nullable attributes with value null will use default value the same way undefined does
23+
- Structure classes now have two methods to generically set and get the value of the attributes, `.get(attributeName)` and `.set(attributeName, attributeValue)`
24+
- Minimum Node version is now 10
25+
26+
Docs:
27+
28+
- Rename the term `type descriptor` to `attribute definition` in the docs and in the code
29+
- Reorganize and add more specific pages to docs
30+
31+
## 2.0.0-alpha.4 - 2020-03-21
32+
33+
- Publish only src folder for jest-structure
34+
35+
## 2.0.0-alpha.3 - 2020-03-20
36+
37+
- Reorganize md files
38+
39+
## 2.0.0-alpha.2 - 2020-03-19
40+
41+
- Invert symlinks
42+
43+
## 2.0.0-alpha.1 - 2020-03-19
44+
45+
- Add symlinks to md files to packages/structure
46+
47+
## 2.0.0-alpha.0 - 2020-03-19
48+
49+
Refactors:
50+
51+
- The whole part of schemas and attribute definitions was refactored
52+
- Tests are now run by Jest (and Electron for browser tests)
53+
- Prettier was added
54+
- Move to mono-repo
55+
56+
Enhancements
57+
58+
- Implement jest-structure assertions
59+
- It's possible to set custom getters e setters directly in the structure class
60+
61+
Breaking changes:
62+
63+
- Joi is updated to v16
64+
- Attribute path in validation _errors_ is an array instead of a string
65+
- Attribute path in validation _messages_ contains the whole path joined by '.'
66+
- The name used for the dynamic import should aways be the same as the name of its type or else a custom identifier must be used
67+
- Non-nullable attributes with value null will use default value the same way undefined does
68+
- Structure classes now have two methods to generically set and get the value of the attributes, `.get(attributeName)` and `.set(attributeName, attributeValue)`
69+
- Minimum Node version is now 10
70+
171
## 1.8.0 - 2019-09-16
72+
273
Enhancements:
3-
* Add `unique` validation to arrays
74+
75+
- Add `unique` validation to arrays
476

577
## 1.7.0 - 2019-09-14
78+
679
Enhancements:
7-
* Add method to clone structures
80+
81+
- Add method to clone structures
882

983
## 1.6.0 - 2019-08-27
84+
1085
Enhancements:
11-
* Allow custom error class to static mode
86+
87+
- Allow custom error class to static mode
1288

1389
## 1.5.0 - 2019-07-08
90+
1491
Enhancements:
15-
* Add `buildStrict` static method
92+
93+
- Add `buildStrict` static method
1694

1795
## 1.4.0 - 2019-03-26
96+
1897
Enhancements:
19-
* Add `nullable` option
98+
99+
- Add `nullable` option
20100

21101
## 1.3.2 - 2019-03-22
102+
22103
Fix:
23-
* The actual instance is passed to the dynamic defaults
104+
105+
- The actual instance is passed to the dynamic defaults
24106

25107
## 1.3.0 - 2018-03-23
108+
26109
Enhancements:
27-
* When using default function to initialize attributes you can now refer to another attribute values to compose value
110+
111+
- When using default function to initialize attributes you can now refer to another attribute values to compose value
28112

29113
## 1.2.0 - 2017-02-01
114+
30115
Features:
31-
* Allow circular reference on type definitions ([@talyssonoc](https://github.com/talyssonoc/structure/pull/30))
116+
117+
- Allow circular reference on type definitions ([@talyssonoc](https://github.com/talyssonoc/structure/pull/30))
32118

33119
Enhancements:
34-
* Make validation faster ([@talyssonoc](https://github.com/talyssonoc/structure/pull/28))
120+
121+
- Make validation faster ([@talyssonoc](https://github.com/talyssonoc/structure/pull/28))
35122

36123
Dependencies update:
37-
* Update joi from 9.2.0 to 10.2.0 ([@talyssonoc](https://github.com/talyssonoc/structure/pull/26))
124+
125+
- Update joi from 9.2.0 to 10.2.0 ([@talyssonoc](https://github.com/talyssonoc/structure/pull/26))
38126

39127
## 1.1.0 - 2017-01-17
40-
* Added static method `validate()` to structures ([@talyssonoc](https://github.com/talyssonoc/structure/pull/25))
128+
129+
- Added static method `validate()` to structures ([@talyssonoc](https://github.com/talyssonoc/structure/pull/25))

README.md

Lines changed: 15 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,16 @@
22

33
## A simple schema/attributes library built on top of modern JavaScript
44

5-
[![npm](https://img.shields.io/npm/v/structure.svg?style=flat)](https://www.npmjs.com/package/structure) [![Build Status](https://travis-ci.org/talyssonoc/structure.svg?branch=master)](https://travis-ci.org/talyssonoc/structure) [![Coverage Status](https://coveralls.io/repos/github/talyssonoc/structure/badge.svg?branch=master)](https://coveralls.io/github/talyssonoc/structure?branch=master) [![Code Climate](https://codeclimate.com/github/talyssonoc/structure/badges/gpa.svg)](https://codeclimate.com/github/talyssonoc/structure) [![JS.ORG](https://img.shields.io/badge/js.org-structure-ffb400.svg?style=flat)](https://js.org/)
6-
---
7-
Structure provides a simple interface which allows you to add attributes to your ES6 classes based on a schema, with validations and type coercion.
5+
Structure provides a simple interface which allows you to add attributes to your classes based on a schema, with validations and type coercion.
86

9-
## Use cases
7+
## Packages
108

11-
You can use Structure for a lot of different cases, including:
9+
- [Structure](packages/structure)
10+
- [jest-structure](packages/jest-structure)
1211

13-
- Domain entities and value objects
14-
- Model business rules
15-
- Validation and coercion of request data
16-
- Map pure objects and JSON to your application classes
17-
- Add attributes to classes that you can't change the class hierarchy
12+
## [Documentation](https://structure.js.org/)
1813

19-
Structure was inspired by Ruby's [Virtus](https://github.com/solnic/virtus).
20-
21-
What Structure is __not__:
22-
23-
- It's not a database abstraction
24-
- It's not a MVC framework (but it can be used to domain entities)
25-
- It's not an attempt to simulate classic inheritance in JavaScript
26-
27-
## Getting started
28-
29-
`npm install --save structure`
30-
31-
## Usage
14+
## Example Structure usage
3215

3316
For each attribute on your schema, a getter and a setter will be created into the given class. It'll also auto-assign those attributes passed to the constructor.
3417

@@ -39,53 +22,25 @@ const User = attributes({
3922
name: String,
4023
age: {
4124
type: Number,
42-
default: 18
25+
default: 18,
4326
},
44-
birthday: Date
45-
})(class User {
46-
greet() {
47-
return `Hello ${this.name}`;
27+
birthday: Date,
28+
})(
29+
class User {
30+
greet() {
31+
return `Hello ${this.name}`;
32+
}
4833
}
49-
});
50-
51-
/* The attributes "wraps" the Class, still providing access to its methods: */
34+
);
5235

5336
const user = new User({
54-
name: 'John Foo'
37+
name: 'John Foo',
5538
});
5639

5740
user.name; // 'John Foo'
5841
user.greet(); // 'Hello John Foo'
5942
```
6043

61-
## Support and compatibility
62-
63-
Structure is built on top of modern JavaScript, using new features like [Proxy](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy), [Reflect](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Reflect) and [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol). That being so, there are some things regarding compatibility you should consider when using Structure.
64-
65-
### Node
66-
67-
Node only implemented all the used features on version 6, so for using Structure for a backend application you'll need Node 6 or later.
68-
69-
### Browser
70-
71-
Not all major browsers implemented the used features so you'll need to transpile the code for using it. For browser usage we have the [UMD version](https://github.com/talyssonoc/structure/blob/master/dist/structure.js) bundled with Webpack. We don't bundle Structure with its dependencies so you'll have to provide it with your module bundler. It's recommended to replace joi with [joi-browser](https://www.npmjs.com/package/joi-browser) when using it on the front-end, here's how we run our test suite on the browser regarding [bundling](https://github.com/talyssonoc/structure/blob/master/test/karma.conf.js#L3-L11) and [polyfill of features](https://github.com/talyssonoc/structure/blob/master/test/browserSetup.js#L1-L2).
72-
73-
Be aware that not the whole test suite will pass on browsers, there are some cases that can't be simulated through polyfilling, like extending Array or having a non-structure class extending a structure class. You can setup the project on your computer and run npm run test:browser to see how it'll work.
74-
75-
Right now 95.5% of the tests will pass on Chrome 55, and 95% will pass on Firefox 45. We intend to make it support older versions using polyfills in the next releases.
76-
77-
## BattleCry generators
78-
79-
There are configurable [BattleCry](https://github.com/pedsmoreira/battlecry) generators ready to be downloaded and help scaffolding schema:
80-
81-
```sh
82-
npm install -g battlecry
83-
cry download generator talyssonoc/structure
84-
cry g schema user name age:int:required cars:string[] favoriteBook:book friends:user[]:default :updateAge
85-
```
86-
87-
Run `cry --help` to check more info about the generators available;
88-
8944
## [Contributing](contributing.md)
9045

9146
## [LICENSE](license.md)

0 commit comments

Comments
 (0)