From 52b7140d5d9b7849ce5a4ba6c4936c5e896182e3 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sat, 28 Aug 2021 17:17:16 -0700 Subject: [PATCH] build: Switch to a monorepo. (#36) * Move files. * Update TS. * Polish. * Fix pathing. --- .gitignore | 1 + CHANGELOG.md | 293 +----------------- optimal/CHANGELOG.md | 292 +++++++++++++++++ optimal/LICENSE | 21 ++ optimal/README.md | 54 ++++ optimal/package.json | 55 ++++ {src => optimal/src}/ValidationError.ts | 0 {src => optimal/src}/createPredicate.ts | 0 {src => optimal/src}/createSchema.ts | 0 {src => optimal/src}/criteria/arrays.ts | 0 {src => optimal/src}/criteria/booleans.ts | 0 {src => optimal/src}/criteria/classes.ts | 0 {src => optimal/src}/criteria/common.ts | 0 {src => optimal/src}/criteria/dates.ts | 0 {src => optimal/src}/criteria/index.ts | 0 {src => optimal/src}/criteria/numbers.ts | 0 {src => optimal/src}/criteria/objects.ts | 0 {src => optimal/src}/criteria/shapes.ts | 0 {src => optimal/src}/criteria/strings.ts | 0 {src => optimal/src}/criteria/tuples.ts | 0 {src => optimal/src}/criteria/unions.ts | 0 {src => optimal/src}/helpers.ts | 15 +- {src => optimal/src}/index.ts | 0 {src => optimal/src}/optimal.ts | 0 {src => optimal/src}/schemas/array.ts | 0 {src => optimal/src}/schemas/blueprint.ts | 0 {src => optimal/src}/schemas/bool.ts | 0 {src => optimal/src}/schemas/custom.ts | 0 {src => optimal/src}/schemas/date.ts | 0 {src => optimal/src}/schemas/func.ts | 0 {src => optimal/src}/schemas/index.ts | 0 {src => optimal/src}/schemas/instance.ts | 0 {src => optimal/src}/schemas/number.ts | 0 {src => optimal/src}/schemas/object.ts | 0 {src => optimal/src}/schemas/regex.ts | 0 {src => optimal/src}/schemas/schema.ts | 0 {src => optimal/src}/schemas/shape.ts | 0 {src => optimal/src}/schemas/string.ts | 0 {src => optimal/src}/schemas/tuple.ts | 0 {src => optimal/src}/schemas/union.ts | 0 {src => optimal/src}/types.ts | 0 .../tests}/createPredicate.test.ts | 0 {tests => optimal/tests}/helpers.ts | 0 {tests => optimal/tests}/optimal.test.ts | 0 .../tests}/schemas/array.test.ts | 0 .../tests}/schemas/blueprint.test.ts | 0 {tests => optimal/tests}/schemas/bool.test.ts | 0 .../tests}/schemas/custom.test.ts | 0 {tests => optimal/tests}/schemas/date.test.ts | 0 {tests => optimal/tests}/schemas/func.test.ts | 0 .../tests}/schemas/instance.test.ts | 0 .../tests}/schemas/number.test.ts | 0 .../tests}/schemas/object.test.ts | 0 .../tests}/schemas/regex.test.ts | 0 .../tests}/schemas/runCommonTests.ts | 0 .../tests}/schemas/schema.test.ts | 0 .../tests}/schemas/shape.test.ts | 0 .../tests}/schemas/string.test.ts | 0 .../tests}/schemas/tuple.test.ts | 0 .../tests}/schemas/union.test.ts | 0 optimal/tests/tsconfig.json | 19 ++ {tests => optimal/tests}/typings.ts | 0 optimal/tsconfig.json | 19 ++ package.json | 57 +--- tsconfig.json | 34 +- tsconfig.options.json | 29 ++ yarn.lock | 22 +- 67 files changed, 532 insertions(+), 379 deletions(-) create mode 100644 optimal/CHANGELOG.md create mode 100644 optimal/LICENSE create mode 100644 optimal/README.md create mode 100644 optimal/package.json rename {src => optimal/src}/ValidationError.ts (100%) rename {src => optimal/src}/createPredicate.ts (100%) rename {src => optimal/src}/createSchema.ts (100%) rename {src => optimal/src}/criteria/arrays.ts (100%) rename {src => optimal/src}/criteria/booleans.ts (100%) rename {src => optimal/src}/criteria/classes.ts (100%) rename {src => optimal/src}/criteria/common.ts (100%) rename {src => optimal/src}/criteria/dates.ts (100%) rename {src => optimal/src}/criteria/index.ts (100%) rename {src => optimal/src}/criteria/numbers.ts (100%) rename {src => optimal/src}/criteria/objects.ts (100%) rename {src => optimal/src}/criteria/shapes.ts (100%) rename {src => optimal/src}/criteria/strings.ts (100%) rename {src => optimal/src}/criteria/tuples.ts (100%) rename {src => optimal/src}/criteria/unions.ts (100%) rename {src => optimal/src}/helpers.ts (87%) rename {src => optimal/src}/index.ts (100%) rename {src => optimal/src}/optimal.ts (100%) rename {src => optimal/src}/schemas/array.ts (100%) rename {src => optimal/src}/schemas/blueprint.ts (100%) rename {src => optimal/src}/schemas/bool.ts (100%) rename {src => optimal/src}/schemas/custom.ts (100%) rename {src => optimal/src}/schemas/date.ts (100%) rename {src => optimal/src}/schemas/func.ts (100%) rename {src => optimal/src}/schemas/index.ts (100%) rename {src => optimal/src}/schemas/instance.ts (100%) rename {src => optimal/src}/schemas/number.ts (100%) rename {src => optimal/src}/schemas/object.ts (100%) rename {src => optimal/src}/schemas/regex.ts (100%) rename {src => optimal/src}/schemas/schema.ts (100%) rename {src => optimal/src}/schemas/shape.ts (100%) rename {src => optimal/src}/schemas/string.ts (100%) rename {src => optimal/src}/schemas/tuple.ts (100%) rename {src => optimal/src}/schemas/union.ts (100%) rename {src => optimal/src}/types.ts (100%) rename {tests => optimal/tests}/createPredicate.test.ts (100%) rename {tests => optimal/tests}/helpers.ts (100%) rename {tests => optimal/tests}/optimal.test.ts (100%) rename {tests => optimal/tests}/schemas/array.test.ts (100%) rename {tests => optimal/tests}/schemas/blueprint.test.ts (100%) rename {tests => optimal/tests}/schemas/bool.test.ts (100%) rename {tests => optimal/tests}/schemas/custom.test.ts (100%) rename {tests => optimal/tests}/schemas/date.test.ts (100%) rename {tests => optimal/tests}/schemas/func.test.ts (100%) rename {tests => optimal/tests}/schemas/instance.test.ts (100%) rename {tests => optimal/tests}/schemas/number.test.ts (100%) rename {tests => optimal/tests}/schemas/object.test.ts (100%) rename {tests => optimal/tests}/schemas/regex.test.ts (100%) rename {tests => optimal/tests}/schemas/runCommonTests.ts (100%) rename {tests => optimal/tests}/schemas/schema.test.ts (100%) rename {tests => optimal/tests}/schemas/shape.test.ts (100%) rename {tests => optimal/tests}/schemas/string.test.ts (100%) rename {tests => optimal/tests}/schemas/tuple.test.ts (100%) rename {tests => optimal/tests}/schemas/union.test.ts (100%) create mode 100644 optimal/tests/tsconfig.json rename {tests => optimal/tests}/typings.ts (100%) create mode 100644 optimal/tsconfig.json create mode 100644 tsconfig.options.json diff --git a/.gitignore b/.gitignore index 26915e1..b792757 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ node_modules/ jest.config.js prettier.config.js webpack.config.js +*.tsbuildinfo # Yarn .yarn/* diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cb7b9b..acc3fbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,292 +1,3 @@ -# 5.0.0 +# Changelog -Ground-up rewrite that migrates to a more composable API. Under the hood, classes were refactored -into functions to reduce file size and overhead. - -#### 💥 Breaking - -The biggest change is that predicates are now known as schemas. For the purpose of this release, the -changelog will use the new verbiage, but may affect previous APIs. - -- Updated `array()` to no longer accept a schema as an argument, use `array().of()` instead. -- Updated `func()` to not be nullable by default. Instead uses `undefined`. -- Updated `instance()` to no longer accept a schema as an argument, use `instance().of()` instead. -- Updated `object()` to no longer accept a schema as an argument, use `object().of()` instead. -- Renamed `Schema#nonNullable()` method to `notNullable()`. -- Removed `Schema#key()` method. -- Removed `Schema#message()` method. -- Removed the `predicate()` predicate (use `schema()` instead). -- Removed the state boolean argument from `Predicate#required()`. -- Dropped Node.js v10 support. Minimum requirement v12.17. - -#### 🚀 Updates - -- Added `Schema#notRequired()` method. -- Added `date()`, `schema()` schemas. -- Added `createSchema()` function for creating custom schemas. -- Added `createPredicate()` function for creating boolean return checkers. - -#### 🛠 Internals - -- A `ValidationError` is now thrown instead of an `Error`. -- Migrated to Packemon for package building. -- Improved the handling of `null` and `undefined` values. -- Increased code coverage, especially around edge cases. -- Renamed predicates to schemas. -- Renamed validation checks to criteria. - -### 4.3.0 - 2020-02-20 - -#### 🚀 Updates - -- Improved error messages by including the currently invalid value. - -#### 🐞 Fixes - -- Fixed an issue where arrays and tuples were used at the same time in a union. - -#### 🛠 Internals - -- Updated union error messages to be more readable. -- Updated tuple type aliases to use "tuple". - -### 4.2.1 - 2020-02-11 - -#### 🐞 Fixes - -- Fixed some object comparison checks that would fail cross-realm. - -## 4.2.0 - 2020-01-26 - -#### 🚀 Updates - -- Reworked default values to handle undefined and null by default, and in a much better fashion. - -### 4.1.2 - 2020-01-26 - -#### 🐞 Fixes - -- **[shape]** Reset schema state after validating children. -- **[array,object,string]** Updated empty checks to take nullable into account. - -### 4.1.1 - 2020-01-24 - -#### 🐞 Fixes - -- **[func]** Removed the generic constraint as it is too restrictive. - -## 4.1.0 - 2020-01-20 - -#### 🚀 Updates - -- Added `Schema#currentPath`, `currentValue`, `initialStruct`, `parentPath`, and `parentStruct` - properties. Should make custom tree checks much easier. -- Added `Predicate#doRun`, so that sub-classes can hook into the run process. - -#### 🐞 Fixes - -- **[shape,tuple]** Fixed an issue where common predicate checks were not always running. - -### 4.0.1 - 2020-01-19 - -#### 🐞 Fixes - -- **[tuple]** Fixed some array type check failures. -- **[tuple]** Fixed tuples failing to validate when in a `union`. - -# 4.0.0 - 2020-01-07 - -#### 💥 Breaking - -- Updated Node.js requirement to v10. -- Values are now type cast once all checks and validations have ran. This may cause unexpected - results in production, but is not exactly breaking. -- Updated `custom()` callbacks to receive a `Schema` as the 2nd argument instead of a struct object. -- Removed and inlined all `check*` methods as we don't want them publicly chainable. -- Renamed `Builder` to `Predicate` for all classes. -- Renamed `Builder#runChecks()` method to `Predicate#run()` and reworked the arguments. -- Renamed `builder()` to `predicate()`. -- **[ts]** Added visibility modifiers to many internal properties and methods. - -#### 🚀 Updates - -- Add new `Schema` class for handling the building and validation of structs. -- Add new `tuple()` predicate . -- Add support for default values via factory functions. -- Add `Predicate#default()` to return the default value. -- Add `Predicate#validate()` to run stand-alone validation with a predicate. -- **[array,object,string]** Add `sizeOf()` method. -- **[number]** Add `float()`, `int()`, `negative()`, and `positive()` methods. -- **[string]** Add `lowerCase()` and `upperCase()` methods. - -#### ⚙️ Types - -- Updated `object()` and `ObjectOf` to support mapped types through a keys generic. - -#### 🛠 Internals - -- Package is now built with Rollup to reduce filesize. - -## 3.4.0 - 2019-12-28 - -#### 🚀 Updates - -- Add `never()` for all predicates. -- **[bool]** Add `onlyFalse()` and `onlyTrue()` methods. -- **[string]** Add `camelCase()`, `kebabCase()`, `pascalCase()`, and `snakeCase()` methods. - -#### ⚙️ Types - -- Update `CustomCallback` with a struct generic. - -#### 📦 Dependencies - -- Update to latest. - -## 3.3.0 - 2019-12-02 - -#### ⚙️ Types - -- Refined types and replaced `any` with `unknown`. - -## 3.2.1 - 2019-04-22 - -#### 🐞 Fixes - -- Fixed some build issues. - -## 3.2.0 - 2019-04-20 - -#### 🚀 Updates - -- Added a `prefix` option to `optimal`, which customizes the initial object path prefix for errors. -- Builder methods can now return a value to mutate the value being checked. - -#### 🐞 Fixes - -- Shapes within arrays, objects, or unions are now returned with the full object. - -#### 🛠 Internals - -- Updated `ShapeBuilder` to call `optimal` internally, as the APIs are very similar. -- Removed `@babel/runtime-corejs3` as it wasn't saving much space. - -### 3.1.1 - 2019-04-13 - -#### 🐞 Fixes - -- Updated `StringBuilder#contains` and `StringBuilder#match` to only check when the passed value is - not the default value _or_ the field is required. - -## 3.1.0 - 2019-04-07 - -#### 🚀 Updates - -- Added `ShapeBuilder#exact`, which throws on unknown fields and requires an exact shape. -- Updated `UnionBuilder` to support builders of the same type in parallel. For example, can now use - 2 instances of `object()`. -- Updated `UnionBuilder` to support object and shapes in parallel. -- TS: Updated `instance()` to allow abstract classes. - -# 3.0.0 - 2019-04-03 - -#### 🚀 Updates - -- Added a `builder()` predicate, which checks that a property value is a `Builder` instance. -- Added a `blueprint()` predicate, which checks that a property is an object of `Builder`s. - -#### 🛠 Internals - -- Updated to `@babel/runtime-corejs3`, which uses Babel v7.4 and CoreJS v3. -- TS: Updated `optimal` to return the shape wrapped in `Required`. -- TS: Updated `Blueprint` to remove optional properties (acts like `Required`). -- TS: Updated `Builder#custom` to infer the value argument based on the builder type. -- TS: Replaced some usage of `any` with `unknown`. - -### 2.1.1 - 2019-02-07 - -#### 🐞 Fixes - -- Fixed some inheritance issues for instance of checks in loose mode. - -## 2.1.0 - 2019-02-06 - -#### 🚀 Updates - -- Removed docblocks from source files to reduce bundle size. - -# 2.0.0 - 2019-01-30 - -#### 💥 Breaking - -- TypeScript minimum version requirement is now 3.0. -- TypeScript has been rewritten to infer builder types and structures as best it can. This may cause - unexpected inferrence for `optimal()` usage and may require explicit generic types to be passed. -- Nested blueprints must now use `shape()` instead of a plain object. -- Collections have split into `ArrayBuilder` and `ObjectBuilder`. `array()` and `object()` are still - the same. -- `Builder#nullable` no longer accepts an argument and instead enables nulls. -- `custom()` - - Default value is now required at all times. - - TS: Type will be inferred by the default value. Can be explicitly typed using generics. -- `shape()` - - Default value has been removed (since it's inferred by nested blueprint structure). -- `string()` - - String logic has been reversed, as they are now empty by default, instead of not empty. To - enable the old logic, use the `notEmpty()` method. - - The `empty()` method has been removed. -- `union()` - - Default value is now required at all times. - - TS: Type is `any` as unions are not inferrable. Can be explicitly typed using generics. - -#### 🚀 Updates - -- Added documentation. -- Added a `file` option to `optimal()` to include in error messages. -- Added a `predicates` export from the index, which is an object of all builder factory functions. - - TS: Also added a `Predicates` type representing this shape. -- Added a `loose` option to `instance()`, so cross-realm instance checks work (compares constructor - name). -- Added `Builder#notNullable` to disable null values. -- Added `StringBuilder#notEmpty` to require strings to not be empty. - -#### 🛠 Internals - -- TS: Removed `Struct` type. - -## 1.2.0 - 2018-12-29 - -#### 🚀 Updates - -- Added and ES `module` target. - -### 1.1.3 - 2018-10-15 - -#### 🐞 Fixes - -- Fixed an issue with built files. - -### 1.1.2 - 2018-10-15 - -#### 🐞 Fixes - -- Updated `shape()` to return an object with inherited or default values, instead of an empty - object. - -### 1.1.1 - 2018-09-02 - -#### 🐞 Fixes - -- Fixed an issue with built declaration files. - -## 1.1.0 - 2018-09-02 - -#### 🚀 Updates - -- Switched to Babel 7 as the transpiler. Bumped IE requirement to v11, and Node requirement to v8.9. - -# 1.0.0 - 2018-06-07 - -#### 🎉 Release - -- Initial release. +[View changelog in package](./optimal/CHANGELOG.md) diff --git a/optimal/CHANGELOG.md b/optimal/CHANGELOG.md new file mode 100644 index 0000000..1cb7b9b --- /dev/null +++ b/optimal/CHANGELOG.md @@ -0,0 +1,292 @@ +# 5.0.0 + +Ground-up rewrite that migrates to a more composable API. Under the hood, classes were refactored +into functions to reduce file size and overhead. + +#### 💥 Breaking + +The biggest change is that predicates are now known as schemas. For the purpose of this release, the +changelog will use the new verbiage, but may affect previous APIs. + +- Updated `array()` to no longer accept a schema as an argument, use `array().of()` instead. +- Updated `func()` to not be nullable by default. Instead uses `undefined`. +- Updated `instance()` to no longer accept a schema as an argument, use `instance().of()` instead. +- Updated `object()` to no longer accept a schema as an argument, use `object().of()` instead. +- Renamed `Schema#nonNullable()` method to `notNullable()`. +- Removed `Schema#key()` method. +- Removed `Schema#message()` method. +- Removed the `predicate()` predicate (use `schema()` instead). +- Removed the state boolean argument from `Predicate#required()`. +- Dropped Node.js v10 support. Minimum requirement v12.17. + +#### 🚀 Updates + +- Added `Schema#notRequired()` method. +- Added `date()`, `schema()` schemas. +- Added `createSchema()` function for creating custom schemas. +- Added `createPredicate()` function for creating boolean return checkers. + +#### 🛠 Internals + +- A `ValidationError` is now thrown instead of an `Error`. +- Migrated to Packemon for package building. +- Improved the handling of `null` and `undefined` values. +- Increased code coverage, especially around edge cases. +- Renamed predicates to schemas. +- Renamed validation checks to criteria. + +### 4.3.0 - 2020-02-20 + +#### 🚀 Updates + +- Improved error messages by including the currently invalid value. + +#### 🐞 Fixes + +- Fixed an issue where arrays and tuples were used at the same time in a union. + +#### 🛠 Internals + +- Updated union error messages to be more readable. +- Updated tuple type aliases to use "tuple". + +### 4.2.1 - 2020-02-11 + +#### 🐞 Fixes + +- Fixed some object comparison checks that would fail cross-realm. + +## 4.2.0 - 2020-01-26 + +#### 🚀 Updates + +- Reworked default values to handle undefined and null by default, and in a much better fashion. + +### 4.1.2 - 2020-01-26 + +#### 🐞 Fixes + +- **[shape]** Reset schema state after validating children. +- **[array,object,string]** Updated empty checks to take nullable into account. + +### 4.1.1 - 2020-01-24 + +#### 🐞 Fixes + +- **[func]** Removed the generic constraint as it is too restrictive. + +## 4.1.0 - 2020-01-20 + +#### 🚀 Updates + +- Added `Schema#currentPath`, `currentValue`, `initialStruct`, `parentPath`, and `parentStruct` + properties. Should make custom tree checks much easier. +- Added `Predicate#doRun`, so that sub-classes can hook into the run process. + +#### 🐞 Fixes + +- **[shape,tuple]** Fixed an issue where common predicate checks were not always running. + +### 4.0.1 - 2020-01-19 + +#### 🐞 Fixes + +- **[tuple]** Fixed some array type check failures. +- **[tuple]** Fixed tuples failing to validate when in a `union`. + +# 4.0.0 - 2020-01-07 + +#### 💥 Breaking + +- Updated Node.js requirement to v10. +- Values are now type cast once all checks and validations have ran. This may cause unexpected + results in production, but is not exactly breaking. +- Updated `custom()` callbacks to receive a `Schema` as the 2nd argument instead of a struct object. +- Removed and inlined all `check*` methods as we don't want them publicly chainable. +- Renamed `Builder` to `Predicate` for all classes. +- Renamed `Builder#runChecks()` method to `Predicate#run()` and reworked the arguments. +- Renamed `builder()` to `predicate()`. +- **[ts]** Added visibility modifiers to many internal properties and methods. + +#### 🚀 Updates + +- Add new `Schema` class for handling the building and validation of structs. +- Add new `tuple()` predicate . +- Add support for default values via factory functions. +- Add `Predicate#default()` to return the default value. +- Add `Predicate#validate()` to run stand-alone validation with a predicate. +- **[array,object,string]** Add `sizeOf()` method. +- **[number]** Add `float()`, `int()`, `negative()`, and `positive()` methods. +- **[string]** Add `lowerCase()` and `upperCase()` methods. + +#### ⚙️ Types + +- Updated `object()` and `ObjectOf` to support mapped types through a keys generic. + +#### 🛠 Internals + +- Package is now built with Rollup to reduce filesize. + +## 3.4.0 - 2019-12-28 + +#### 🚀 Updates + +- Add `never()` for all predicates. +- **[bool]** Add `onlyFalse()` and `onlyTrue()` methods. +- **[string]** Add `camelCase()`, `kebabCase()`, `pascalCase()`, and `snakeCase()` methods. + +#### ⚙️ Types + +- Update `CustomCallback` with a struct generic. + +#### 📦 Dependencies + +- Update to latest. + +## 3.3.0 - 2019-12-02 + +#### ⚙️ Types + +- Refined types and replaced `any` with `unknown`. + +## 3.2.1 - 2019-04-22 + +#### 🐞 Fixes + +- Fixed some build issues. + +## 3.2.0 - 2019-04-20 + +#### 🚀 Updates + +- Added a `prefix` option to `optimal`, which customizes the initial object path prefix for errors. +- Builder methods can now return a value to mutate the value being checked. + +#### 🐞 Fixes + +- Shapes within arrays, objects, or unions are now returned with the full object. + +#### 🛠 Internals + +- Updated `ShapeBuilder` to call `optimal` internally, as the APIs are very similar. +- Removed `@babel/runtime-corejs3` as it wasn't saving much space. + +### 3.1.1 - 2019-04-13 + +#### 🐞 Fixes + +- Updated `StringBuilder#contains` and `StringBuilder#match` to only check when the passed value is + not the default value _or_ the field is required. + +## 3.1.0 - 2019-04-07 + +#### 🚀 Updates + +- Added `ShapeBuilder#exact`, which throws on unknown fields and requires an exact shape. +- Updated `UnionBuilder` to support builders of the same type in parallel. For example, can now use + 2 instances of `object()`. +- Updated `UnionBuilder` to support object and shapes in parallel. +- TS: Updated `instance()` to allow abstract classes. + +# 3.0.0 - 2019-04-03 + +#### 🚀 Updates + +- Added a `builder()` predicate, which checks that a property value is a `Builder` instance. +- Added a `blueprint()` predicate, which checks that a property is an object of `Builder`s. + +#### 🛠 Internals + +- Updated to `@babel/runtime-corejs3`, which uses Babel v7.4 and CoreJS v3. +- TS: Updated `optimal` to return the shape wrapped in `Required`. +- TS: Updated `Blueprint` to remove optional properties (acts like `Required`). +- TS: Updated `Builder#custom` to infer the value argument based on the builder type. +- TS: Replaced some usage of `any` with `unknown`. + +### 2.1.1 - 2019-02-07 + +#### 🐞 Fixes + +- Fixed some inheritance issues for instance of checks in loose mode. + +## 2.1.0 - 2019-02-06 + +#### 🚀 Updates + +- Removed docblocks from source files to reduce bundle size. + +# 2.0.0 - 2019-01-30 + +#### 💥 Breaking + +- TypeScript minimum version requirement is now 3.0. +- TypeScript has been rewritten to infer builder types and structures as best it can. This may cause + unexpected inferrence for `optimal()` usage and may require explicit generic types to be passed. +- Nested blueprints must now use `shape()` instead of a plain object. +- Collections have split into `ArrayBuilder` and `ObjectBuilder`. `array()` and `object()` are still + the same. +- `Builder#nullable` no longer accepts an argument and instead enables nulls. +- `custom()` + - Default value is now required at all times. + - TS: Type will be inferred by the default value. Can be explicitly typed using generics. +- `shape()` + - Default value has been removed (since it's inferred by nested blueprint structure). +- `string()` + - String logic has been reversed, as they are now empty by default, instead of not empty. To + enable the old logic, use the `notEmpty()` method. + - The `empty()` method has been removed. +- `union()` + - Default value is now required at all times. + - TS: Type is `any` as unions are not inferrable. Can be explicitly typed using generics. + +#### 🚀 Updates + +- Added documentation. +- Added a `file` option to `optimal()` to include in error messages. +- Added a `predicates` export from the index, which is an object of all builder factory functions. + - TS: Also added a `Predicates` type representing this shape. +- Added a `loose` option to `instance()`, so cross-realm instance checks work (compares constructor + name). +- Added `Builder#notNullable` to disable null values. +- Added `StringBuilder#notEmpty` to require strings to not be empty. + +#### 🛠 Internals + +- TS: Removed `Struct` type. + +## 1.2.0 - 2018-12-29 + +#### 🚀 Updates + +- Added and ES `module` target. + +### 1.1.3 - 2018-10-15 + +#### 🐞 Fixes + +- Fixed an issue with built files. + +### 1.1.2 - 2018-10-15 + +#### 🐞 Fixes + +- Updated `shape()` to return an object with inherited or default values, instead of an empty + object. + +### 1.1.1 - 2018-09-02 + +#### 🐞 Fixes + +- Fixed an issue with built declaration files. + +## 1.1.0 - 2018-09-02 + +#### 🚀 Updates + +- Switched to Babel 7 as the transpiler. Bumped IE requirement to v11, and Node requirement to v8.9. + +# 1.0.0 - 2018-06-07 + +#### 🎉 Release + +- Initial release. diff --git a/optimal/LICENSE b/optimal/LICENSE new file mode 100644 index 0000000..6d54162 --- /dev/null +++ b/optimal/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Miles Johnson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/optimal/README.md b/optimal/README.md new file mode 100644 index 0000000..1cb035a --- /dev/null +++ b/optimal/README.md @@ -0,0 +1,54 @@ +# Optimal + +[![Build Status](https://github.com/milesj/optimal/workflows/Build/badge.svg)](https://github.com/milesj/optimal/actions?query=branch%3Amaster) +[![npm version](https://badge.fury.io/js/optimal.svg)](https://www.npmjs.com/package/optimal) +[![npm deps](https://david-dm.org/milesj/optimal.svg)](https://www.npmjs.com/package/optimal) + +A system for building and validating defined object structures, like argument options, configuration +files, data bags, validation fields, and more! Runs checks in development and strips checks in +production using dead code elimination (should not be used on user input!). + +- Recursively builds and validates nested structures. +- Supports common data types. +- Autofills missing fields with default values. +- Allows or restricts unknown fields. +- Mark fields as nullable or required. +- Handles logical operators AND, OR, and XOR. + +```ts +import optimal, { array, string, number } from 'optimal'; + +// Pass a partial object and define an explicit blueprint +optimal( + { name: 'Optimal' }, + { + name: string().notEmpty(), + include: array().of(string()), + exclude: array().of(string()), + maxSize: number(10000).gte(0), + }, +); + +// Which validates, builds, and returns the following object +{ + name: 'Optimal', + include: [], + exclude: [], + maxSize: 10000, +} +``` + +## Requirements + +- Node 12.17+ +- Edge / Modern browsers + +## Installation + +``` +yarn add optimal +``` + +## Documentation + +[https://milesj.gitbook.io/optimal](https://milesj.gitbook.io/optimal) diff --git a/optimal/package.json b/optimal/package.json new file mode 100644 index 0000000..347b806 --- /dev/null +++ b/optimal/package.json @@ -0,0 +1,55 @@ +{ + "name": "optimal", + "version": "5.0.0-alpha.0", + "description": "A system for building and validating defined object structures with schemas.", + "main": "./lib/node/index.js", + "browser": "./lib/browser/index.js", + "module": "./esm/index.js", + "types": "./dts/index.d.ts", + "keywords": [ + "opts", + "options", + "predicate", + "validator", + "config", + "defaults", + "object", + "params", + "struct" + ], + "repository": { + "type": "git", + "url": "git@github.com:milesj/optimal.git", + "directory": "optimal" + }, + "author": { + "name": "Miles Johnson", + "url": "http://milesj.me" + }, + "license": "MIT", + "files": [ + "dts/**/*.d.ts", + "esm/**/*.{js,map}", + "lib/**/*.{js,map}", + "src/**/*.{ts,tsx,json}" + ], + "engines": { + "node": ">=12.17.0", + "npm": ">=6.13.0" + }, + "funding": { + "type": "ko-fi", + "url": "https://ko-fi.com/milesjohnson" + }, + "packemon": { + "bundle": true, + "format": [ + "lib", + "esm" + ], + "platform": [ + "browser", + "node" + ] + } +} diff --git a/src/ValidationError.ts b/optimal/src/ValidationError.ts similarity index 100% rename from src/ValidationError.ts rename to optimal/src/ValidationError.ts diff --git a/src/createPredicate.ts b/optimal/src/createPredicate.ts similarity index 100% rename from src/createPredicate.ts rename to optimal/src/createPredicate.ts diff --git a/src/createSchema.ts b/optimal/src/createSchema.ts similarity index 100% rename from src/createSchema.ts rename to optimal/src/createSchema.ts diff --git a/src/criteria/arrays.ts b/optimal/src/criteria/arrays.ts similarity index 100% rename from src/criteria/arrays.ts rename to optimal/src/criteria/arrays.ts diff --git a/src/criteria/booleans.ts b/optimal/src/criteria/booleans.ts similarity index 100% rename from src/criteria/booleans.ts rename to optimal/src/criteria/booleans.ts diff --git a/src/criteria/classes.ts b/optimal/src/criteria/classes.ts similarity index 100% rename from src/criteria/classes.ts rename to optimal/src/criteria/classes.ts diff --git a/src/criteria/common.ts b/optimal/src/criteria/common.ts similarity index 100% rename from src/criteria/common.ts rename to optimal/src/criteria/common.ts diff --git a/src/criteria/dates.ts b/optimal/src/criteria/dates.ts similarity index 100% rename from src/criteria/dates.ts rename to optimal/src/criteria/dates.ts diff --git a/src/criteria/index.ts b/optimal/src/criteria/index.ts similarity index 100% rename from src/criteria/index.ts rename to optimal/src/criteria/index.ts diff --git a/src/criteria/numbers.ts b/optimal/src/criteria/numbers.ts similarity index 100% rename from src/criteria/numbers.ts rename to optimal/src/criteria/numbers.ts diff --git a/src/criteria/objects.ts b/optimal/src/criteria/objects.ts similarity index 100% rename from src/criteria/objects.ts rename to optimal/src/criteria/objects.ts diff --git a/src/criteria/shapes.ts b/optimal/src/criteria/shapes.ts similarity index 100% rename from src/criteria/shapes.ts rename to optimal/src/criteria/shapes.ts diff --git a/src/criteria/strings.ts b/optimal/src/criteria/strings.ts similarity index 100% rename from src/criteria/strings.ts rename to optimal/src/criteria/strings.ts diff --git a/src/criteria/tuples.ts b/optimal/src/criteria/tuples.ts similarity index 100% rename from src/criteria/tuples.ts rename to optimal/src/criteria/tuples.ts diff --git a/src/criteria/unions.ts b/optimal/src/criteria/unions.ts similarity index 100% rename from src/criteria/unions.ts rename to optimal/src/criteria/unions.ts diff --git a/src/helpers.ts b/optimal/src/helpers.ts similarity index 87% rename from src/helpers.ts rename to optimal/src/helpers.ts index aaeeea6..5b7e90b 100644 --- a/src/helpers.ts +++ b/optimal/src/helpers.ts @@ -7,8 +7,9 @@ export function isObject(value: unknown): value is object { export function isSchema(value: unknown): value is Schema { return ( isObject(value) && - typeof (value as UnknownObject).validate === 'function' && - typeof (value as UnknownObject).type === 'function' + typeof (value as UnknownObject).schema === 'function' && + typeof (value as UnknownObject).type === 'function' && + typeof (value as UnknownObject).validate === 'function' ); } @@ -49,7 +50,7 @@ export function createDate(value: unknown): Date { } export function createObject(value: unknown) { - return ((isObject(value) ? value : {}) as unknown) as T; + return (isObject(value) ? value : {}) as unknown as T; } /** @@ -101,9 +102,11 @@ export function logUnknown(unknownFields: object, pathPrefix?: string) { const unknownKeys = Object.keys(unknownFields); if (unknownKeys.length > 0) { - const message = pathPrefix ? `Unknown "${pathPrefix}" fields` : 'Unknown fields'; - - throw new Error(`${message}: ${unknownKeys.join(', ')}.`); + throw new Error( + `${pathPrefix ? `Unknown "${pathPrefix}" fields` : 'Unknown fields'}: ${unknownKeys.join( + ', ', + )}.`, + ); } } diff --git a/src/index.ts b/optimal/src/index.ts similarity index 100% rename from src/index.ts rename to optimal/src/index.ts diff --git a/src/optimal.ts b/optimal/src/optimal.ts similarity index 100% rename from src/optimal.ts rename to optimal/src/optimal.ts diff --git a/src/schemas/array.ts b/optimal/src/schemas/array.ts similarity index 100% rename from src/schemas/array.ts rename to optimal/src/schemas/array.ts diff --git a/src/schemas/blueprint.ts b/optimal/src/schemas/blueprint.ts similarity index 100% rename from src/schemas/blueprint.ts rename to optimal/src/schemas/blueprint.ts diff --git a/src/schemas/bool.ts b/optimal/src/schemas/bool.ts similarity index 100% rename from src/schemas/bool.ts rename to optimal/src/schemas/bool.ts diff --git a/src/schemas/custom.ts b/optimal/src/schemas/custom.ts similarity index 100% rename from src/schemas/custom.ts rename to optimal/src/schemas/custom.ts diff --git a/src/schemas/date.ts b/optimal/src/schemas/date.ts similarity index 100% rename from src/schemas/date.ts rename to optimal/src/schemas/date.ts diff --git a/src/schemas/func.ts b/optimal/src/schemas/func.ts similarity index 100% rename from src/schemas/func.ts rename to optimal/src/schemas/func.ts diff --git a/src/schemas/index.ts b/optimal/src/schemas/index.ts similarity index 100% rename from src/schemas/index.ts rename to optimal/src/schemas/index.ts diff --git a/src/schemas/instance.ts b/optimal/src/schemas/instance.ts similarity index 100% rename from src/schemas/instance.ts rename to optimal/src/schemas/instance.ts diff --git a/src/schemas/number.ts b/optimal/src/schemas/number.ts similarity index 100% rename from src/schemas/number.ts rename to optimal/src/schemas/number.ts diff --git a/src/schemas/object.ts b/optimal/src/schemas/object.ts similarity index 100% rename from src/schemas/object.ts rename to optimal/src/schemas/object.ts diff --git a/src/schemas/regex.ts b/optimal/src/schemas/regex.ts similarity index 100% rename from src/schemas/regex.ts rename to optimal/src/schemas/regex.ts diff --git a/src/schemas/schema.ts b/optimal/src/schemas/schema.ts similarity index 100% rename from src/schemas/schema.ts rename to optimal/src/schemas/schema.ts diff --git a/src/schemas/shape.ts b/optimal/src/schemas/shape.ts similarity index 100% rename from src/schemas/shape.ts rename to optimal/src/schemas/shape.ts diff --git a/src/schemas/string.ts b/optimal/src/schemas/string.ts similarity index 100% rename from src/schemas/string.ts rename to optimal/src/schemas/string.ts diff --git a/src/schemas/tuple.ts b/optimal/src/schemas/tuple.ts similarity index 100% rename from src/schemas/tuple.ts rename to optimal/src/schemas/tuple.ts diff --git a/src/schemas/union.ts b/optimal/src/schemas/union.ts similarity index 100% rename from src/schemas/union.ts rename to optimal/src/schemas/union.ts diff --git a/src/types.ts b/optimal/src/types.ts similarity index 100% rename from src/types.ts rename to optimal/src/types.ts diff --git a/tests/createPredicate.test.ts b/optimal/tests/createPredicate.test.ts similarity index 100% rename from tests/createPredicate.test.ts rename to optimal/tests/createPredicate.test.ts diff --git a/tests/helpers.ts b/optimal/tests/helpers.ts similarity index 100% rename from tests/helpers.ts rename to optimal/tests/helpers.ts diff --git a/tests/optimal.test.ts b/optimal/tests/optimal.test.ts similarity index 100% rename from tests/optimal.test.ts rename to optimal/tests/optimal.test.ts diff --git a/tests/schemas/array.test.ts b/optimal/tests/schemas/array.test.ts similarity index 100% rename from tests/schemas/array.test.ts rename to optimal/tests/schemas/array.test.ts diff --git a/tests/schemas/blueprint.test.ts b/optimal/tests/schemas/blueprint.test.ts similarity index 100% rename from tests/schemas/blueprint.test.ts rename to optimal/tests/schemas/blueprint.test.ts diff --git a/tests/schemas/bool.test.ts b/optimal/tests/schemas/bool.test.ts similarity index 100% rename from tests/schemas/bool.test.ts rename to optimal/tests/schemas/bool.test.ts diff --git a/tests/schemas/custom.test.ts b/optimal/tests/schemas/custom.test.ts similarity index 100% rename from tests/schemas/custom.test.ts rename to optimal/tests/schemas/custom.test.ts diff --git a/tests/schemas/date.test.ts b/optimal/tests/schemas/date.test.ts similarity index 100% rename from tests/schemas/date.test.ts rename to optimal/tests/schemas/date.test.ts diff --git a/tests/schemas/func.test.ts b/optimal/tests/schemas/func.test.ts similarity index 100% rename from tests/schemas/func.test.ts rename to optimal/tests/schemas/func.test.ts diff --git a/tests/schemas/instance.test.ts b/optimal/tests/schemas/instance.test.ts similarity index 100% rename from tests/schemas/instance.test.ts rename to optimal/tests/schemas/instance.test.ts diff --git a/tests/schemas/number.test.ts b/optimal/tests/schemas/number.test.ts similarity index 100% rename from tests/schemas/number.test.ts rename to optimal/tests/schemas/number.test.ts diff --git a/tests/schemas/object.test.ts b/optimal/tests/schemas/object.test.ts similarity index 100% rename from tests/schemas/object.test.ts rename to optimal/tests/schemas/object.test.ts diff --git a/tests/schemas/regex.test.ts b/optimal/tests/schemas/regex.test.ts similarity index 100% rename from tests/schemas/regex.test.ts rename to optimal/tests/schemas/regex.test.ts diff --git a/tests/schemas/runCommonTests.ts b/optimal/tests/schemas/runCommonTests.ts similarity index 100% rename from tests/schemas/runCommonTests.ts rename to optimal/tests/schemas/runCommonTests.ts diff --git a/tests/schemas/schema.test.ts b/optimal/tests/schemas/schema.test.ts similarity index 100% rename from tests/schemas/schema.test.ts rename to optimal/tests/schemas/schema.test.ts diff --git a/tests/schemas/shape.test.ts b/optimal/tests/schemas/shape.test.ts similarity index 100% rename from tests/schemas/shape.test.ts rename to optimal/tests/schemas/shape.test.ts diff --git a/tests/schemas/string.test.ts b/optimal/tests/schemas/string.test.ts similarity index 100% rename from tests/schemas/string.test.ts rename to optimal/tests/schemas/string.test.ts diff --git a/tests/schemas/tuple.test.ts b/optimal/tests/schemas/tuple.test.ts similarity index 100% rename from tests/schemas/tuple.test.ts rename to optimal/tests/schemas/tuple.test.ts diff --git a/tests/schemas/union.test.ts b/optimal/tests/schemas/union.test.ts similarity index 100% rename from tests/schemas/union.test.ts rename to optimal/tests/schemas/union.test.ts diff --git a/optimal/tests/tsconfig.json b/optimal/tests/tsconfig.json new file mode 100644 index 0000000..70dd35a --- /dev/null +++ b/optimal/tests/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "composite": false, + "emitDeclarationOnly": false, + "noEmit": true, + "rootDir": "." + }, + "extends": "../../tsconfig.options.json", + "include": [ + "**/*", + "../types/**/*", + "../../types/**/*" + ], + "references": [ + { + "path": ".." + } + ] +} diff --git a/tests/typings.ts b/optimal/tests/typings.ts similarity index 100% rename from tests/typings.ts rename to optimal/tests/typings.ts diff --git a/optimal/tsconfig.json b/optimal/tsconfig.json new file mode 100644 index 0000000..d3867c3 --- /dev/null +++ b/optimal/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "declarationDir": "dts", + "outDir": "dts", + "rootDir": "src", + "emitDeclarationOnly": true + }, + "exclude": [ + "dts", + "tests" + ], + "extends": "../tsconfig.options.json", + "include": [ + "src/**/*", + "types/**/*", + "../types/**/*" + ], + "references": [] +} diff --git a/package.json b/package.json index 56bbfd4..9bad7b4 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,6 @@ { - "name": "optimal", - "version": "5.0.0-alpha.0", - "description": "A system for building and validating defined object structures with schemas.", - "main": "./lib/node/index.js", - "browser": "./lib/browser/index.js", - "module": "./esm/index.js", - "types": "./dts/index.d.ts", + "private": true, + "name": "optimal-root", "scripts": { "prepare": "beemo create-config", "build": "packemon build --addEngines", @@ -15,36 +10,10 @@ "coverage": "yarn run test --coverage", "lint": "beemo eslint", "format": "beemo prettier", - "release": "yarn run pack && yarn run ci && npx np --yolo --no-yarn", + "release": "yarn run pack && yarn run check && npx np --yolo --no-yarn", "test": "beemo jest", - "type": "beemo typescript --noEmit" + "type": "beemo typescript --build" }, - "repository": { - "type": "git", - "url": "git+https://github.com/milesj/optimal.git" - }, - "keywords": [ - "opts", - "options", - "predicate", - "validator", - "config", - "defaults", - "object", - "params", - "struct" - ], - "author": { - "name": "Miles Johnson", - "url": "http://milesj.me" - }, - "license": "MIT", - "files": [ - "dts/**/*.d.ts", - "esm/**/*.{js,map}", - "lib/**/*.{js,map}", - "src/**/*.{ts,tsx,json}" - ], "engines": { "node": ">=12.17.0", "npm": ">=6.13.0" @@ -56,19 +25,7 @@ "@types/node": "^15.6.1", "packemon": "^1.4.1" }, - "funding": { - "type": "ko-fi", - "url": "https://ko-fi.com/milesjohnson" - }, - "packemon": { - "bundle": true, - "format": [ - "lib", - "esm" - ], - "platform": [ - "browser", - "node" - ] - } + "workspaces": [ + "optimal/" + ] } diff --git a/tsconfig.json b/tsconfig.json index 9e9a457..4d856c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,28 +1,14 @@ { "$schema": "https://json.schemastore.org/tsconfig", "display": "Beemo", - "compilerOptions": { - "allowJs": false, - "allowSyntheticDefaultImports": true, - "declaration": false, - "esModuleInterop": true, - "experimentalDecorators": false, - "forceConsistentCasingInFileNames": true, - "isolatedModules": true, - "lib": [ - "esnext" - ], - "module": "esnext", - "moduleResolution": "node", - "noEmitOnError": true, - "noImplicitOverride": true, - "noImplicitReturns": true, - "pretty": true, - "removeComments": false, - "resolveJsonModule": false, - "skipLibCheck": true, - "sourceMap": false, - "strict": true, - "target": "es2020" - } + "extends": "./tsconfig.options.json", + "files": [], + "references": [ + { + "path": "optimal" + }, + { + "path": "optimal/tests" + } + ] } \ No newline at end of file diff --git a/tsconfig.options.json b/tsconfig.options.json new file mode 100644 index 0000000..29c0b47 --- /dev/null +++ b/tsconfig.options.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "allowJs": false, + "allowSyntheticDefaultImports": true, + "declaration": true, + "esModuleInterop": true, + "experimentalDecorators": false, + "forceConsistentCasingInFileNames": true, + "isolatedModules": true, + "lib": [ + "esnext" + ], + "module": "esnext", + "moduleResolution": "node", + "noEmitOnError": true, + "noImplicitOverride": true, + "noImplicitReturns": true, + "pretty": true, + "removeComments": false, + "resolveJsonModule": false, + "skipLibCheck": true, + "sourceMap": false, + "strict": true, + "target": "es2020", + "composite": true, + "declarationMap": true, + "emitDeclarationOnly": true + } +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index d02699d..521bedb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7723,6 +7723,18 @@ __metadata: languageName: node linkType: hard +"optimal-root@workspace:.": + version: 0.0.0-use.local + resolution: "optimal-root@workspace:." + dependencies: + "@beemo/cli": ^2.0.1 + "@beemo/core": ^2.0.2 + "@beemo/dev": ^1.5.0 + "@types/node": ^15.6.1 + packemon: ^1.4.1 + languageName: unknown + linkType: soft + "optimal@npm:^4.3.0": version: 4.3.0 resolution: "optimal@npm:4.3.0" @@ -7730,15 +7742,9 @@ __metadata: languageName: node linkType: hard -"optimal@workspace:.": +"optimal@workspace:optimal": version: 0.0.0-use.local - resolution: "optimal@workspace:." - dependencies: - "@beemo/cli": ^2.0.1 - "@beemo/core": ^2.0.2 - "@beemo/dev": ^1.5.0 - "@types/node": ^15.6.1 - packemon: ^1.4.1 + resolution: "optimal@workspace:optimal" languageName: unknown linkType: soft