Skip to content

Commit

Permalink
Merge pull request #481 from shimataro/develop
Browse files Browse the repository at this point in the history
version 3.0.0-rc.9
  • Loading branch information
shimataro committed Jun 17, 2020
2 parents eb435f0 + 64d85c7 commit 540577d
Show file tree
Hide file tree
Showing 26 changed files with 463 additions and 413 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [3.0.0-rc.9] - 2020-06-18

### Changed

* make `converter` non-nullable

### Fixed

* support nullable cases
* `{ifUndefined: null}`
* `{ifNull: null}`
* `{ifEmptyString: null}`

## [3.0.0-rc.8] - 2020-06-13

### Changed
Expand Down Expand Up @@ -483,7 +496,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

* First release.

[Unreleased]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.8...HEAD
[Unreleased]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.9...HEAD
[3.0.0-rc.9]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.8...v3.0.0-rc.9
[3.0.0-rc.8]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.7...v3.0.0-rc.8
[3.0.0-rc.7]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.6...v3.0.0-rc.7
[3.0.0-rc.6]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.5...v3.0.0-rc.6
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ type OptionsForNumber = {
minValue?: number | {value: number, adjusts: boolean};
maxValue?: number | {value: number, adjusts: boolean};

converter?: (value: number, fail: () => never) => number | null;
converter?: (value: number, fail: () => never) => number;
}
type ErrorHandler = (err: ValueSchemaError) => number | null | never;
interface NumberSchema {
Expand Down Expand Up @@ -1184,7 +1184,7 @@ type OptionsForString = {
maxLength?: number | {length: number, trims: boolean};
pattern?: RegExp;

converter?: (value: string, fail: () => never) => string | null;
converter?: (value: string, fail: () => never) => string;
}
type ErrorHandler = (err: ValueSchemaError) => string | null | never;
interface StringSchema {
Expand Down Expand Up @@ -1436,7 +1436,7 @@ type OptionsForNumericString = {
pattern?: RegExp;
checksum?: NUMERIC_STRING.CHECKSUM_ALGORITHM;

converter?: (value: string, fail: () => never) => string | null;
converter?: (value: string, fail: () => never) => string;
}
type ErrorHandler = (err: ValueSchemaError) => string | null | never;
interface NumericStringSchema {
Expand Down Expand Up @@ -1856,7 +1856,7 @@ type OptionsForArray<T> = {
maxLength?: number | {length: number, trims: boolean};
each?: BaseSchema<T> | {schema: BaseSchema<T>, ignoresErrors: boolean};

converter?: (values: T[], fail: () => never) => T[] | null;
converter?: (values: T[], fail: () => never) => T[];
}
type ErrorHandler<T> = (err: ValueSchemaError) => T[] | null | never;
interface ArraySchema<T> {
Expand Down Expand Up @@ -2071,7 +2071,7 @@ type OptionsForObject = {

schemaObject?: Record<string, BaseSchema>;

converter?: (values: object, fail: () => never) => object | null;
converter?: (values: object, fail: () => never) => object;
}
type ErrorHandler = (err: ValueSchemaError) => object | null | never;
interface ObjectSchema {
Expand Down
110 changes: 49 additions & 61 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "value-schema",
"description": "simple, easy-to-use, and declarative schema validator",
"version": "3.0.0-rc.8",
"version": "3.0.0-rc.9",
"author": "shimataro",
"license": "MIT",
"repository": {
Expand Down Expand Up @@ -57,8 +57,8 @@
"@babel/core": "7.10.2",
"@types/jest": "26.0.0",
"@types/node": "14.0.13",
"@typescript-eslint/eslint-plugin": "3.2.0",
"@typescript-eslint/parser": "3.2.0",
"@typescript-eslint/eslint-plugin": "3.3.0",
"@typescript-eslint/parser": "3.3.0",
"babel-plugin-add-module-exports": "1.0.2",
"babel-plugin-module-extension-resolver": "1.0.0-rc.1",
"case": "1.6.3",
Expand Down
33 changes: 19 additions & 14 deletions src/appliers/array/each.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Key, Values, isArray} from "../../libs/types";
import {BaseSchema} from "../../libs/BaseSchema";
import {ValueSchemaError} from "../../libs/ValueSchemaError";
import {BaseSchema} from "../../schemaClasses/BaseSchema";

type Each<T> = {
schema: BaseSchema<T>;
Expand Down Expand Up @@ -34,29 +34,34 @@ export function applyTo<T>(values: Values, options: Options<T>, keyStack: Key[])
return false;
}

const adjustedValues: (T | null)[] = [];
const adjustedValues: T[] = [];
for(let idx = 0; idx < values.output.length; idx++)
{
let ignored = false;
const element = values.output[idx];

// A trick in order to call _applyTo() private method from the outside (like "friend")
const adjustedValue = each.schema["_applyTo"](element, (err) =>
try
{
if(each.ignoresErrors)
const adjustedValue = each.schema["_applyTo"](element, (err) =>
{
ignored = true;
return null;
}

ValueSchemaError.raise(err.cause, values, err.keyStack);
}, [...keyStack, idx]);
if(each.ignoresErrors)
{
throw Error("!IGNORE!");
}

if(ignored)
ValueSchemaError.raise(err.cause, values, err.keyStack);
}, [...keyStack, idx]);
adjustedValues.push(adjustedValue);
}
catch(err)
{
continue;
if(err.message === "!IGNORE!")
{
// ignore
continue;
}
throw err;
}
adjustedValues.push(adjustedValue);
}

// replace with adjusted values
Expand Down
2 changes: 1 addition & 1 deletion src/appliers/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {CAUSE, ValueSchemaError} from "../libs/ValueSchemaError";

export interface Options<T>
{
converter?: (value: T, fail: () => never) => T | null;
converter?: (value: T, fail: () => never) => T;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/appliers/object/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Key, ObjectTypeOf, SchemaObject, Values} from "../../libs/types";
import {applySchemaObjectCore} from "../../libs/applySchemaObjectCore";
import {onErrorDefault, onFinishedDefault} from "../../libs/BaseSchema";
import {onErrorDefault, onFinishedDefault} from "../../schemaClasses/BaseSchema";

export interface Options<S>
{
Expand Down
6 changes: 3 additions & 3 deletions src/libs/applySchemaObject.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {applySchemaObjectCore} from "./applySchemaObjectCore";
import {ErrorHandler, FinishHandler, onErrorDefault, onFinishedDefault} from "./BaseSchema";

import {ObjectTypeOf, SchemaObject} from "./types";

export {ErrorHandler} from "./BaseSchema";
import {ErrorHandler, FinishHandler, onErrorDefault, onFinishedDefault} from "../schemaClasses/BaseSchema";

export {ErrorHandler} from "../schemaClasses/BaseSchema";

/**
* apply schema object to data
Expand Down

0 comments on commit 540577d

Please sign in to comment.