Skip to content

Commit

Permalink
Merge pull request #476 from shimataro/develop
Browse files Browse the repository at this point in the history
version 3.0.0-rc.8
  • Loading branch information
shimataro committed Jun 12, 2020
2 parents 94ce449 + c9cff9a commit eb435f0
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 77 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

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

### Changed

* enabled property completion and type inference, without explicit type specification

## [3.0.0-rc.7] - 2020-06-11

### Fixed
Expand Down Expand Up @@ -477,7 +483,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.7...HEAD
[Unreleased]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.8...HEAD
[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
[3.0.0-rc.5]: https://github.com/shimataro/value-schema/compare/v3.0.0-rc.4...v3.0.0-rc.5
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ interface Parameters {
bar: string
}

const schemaObject: vs.SchemaObject = {
const schemaObject = {
foo: vs.number(),
bar: vs.string(),
};
Expand Down Expand Up @@ -2069,7 +2069,7 @@ type OptionsForObject = {
ifEmptyString?: object | null;
ifNull?: object | null;

schemaObject?: SchemaObject;
schemaObject?: Record<string, BaseSchema>;

converter?: (values: object, fail: () => never) => object | null;
}
Expand Down
19 changes: 2 additions & 17 deletions examples/example-deno.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import * as assert from "https://deno.land/std/testing/asserts.ts";
import vs from "../mod.ts";

interface Input {
id: number;
name: string;
age: number;
email: string;
state: string;
classes: number[];
skills: string[];
creditCard: string;
remoteAddr: string;
remoteAddrIpv6: string;
limit: number,
offset: number,
};

const schemaObject: vs.SchemaObject = { // schema for input
const schemaObject = { // schema for input
id: vs.number({ // number, >=1
minValue: 1,
}),
Expand Down Expand Up @@ -108,7 +93,7 @@ const expected = { // should be converted to this
};

// Let's apply!
const actual = vs.applySchemaObject<Input>(schemaObject, input);
const actual = vs.applySchemaObject(schemaObject, input);

// verification
assert.assertEquals(actual, expected);
Expand Down
19 changes: 2 additions & 17 deletions examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import assert from "assert";
import vs from "value-schema";

interface Input {
id: number;
name: string;
age: number;
email: string;
state: string;
classes: number[];
skills: string[];
creditCard: string;
remoteAddr: string;
remoteAddrIpv6: string;
limit: number,
offset: number,
};

const schemaObject: vs.SchemaObject = { // schema for input
const schemaObject = { // schema for input
id: vs.number({ // number, >=1
minValue: 1,
}),
Expand Down Expand Up @@ -108,7 +93,7 @@ const expected = { // should be converted to this
};

// Let's apply!
const actual = vs.applySchemaObject<Input>(schemaObject, input);
const actual = vs.applySchemaObject(schemaObject, input);

// verification
assert.deepStrictEqual(actual, expected);
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

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

2 changes: 1 addition & 1 deletion 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.7",
"version": "3.0.0-rc.8",
"author": "shimataro",
"license": "MIT",
"repository": {
Expand Down
10 changes: 5 additions & 5 deletions src/appliers/object/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Key, Values} from "../../libs/types";
import {SchemaObject, applySchemaObjectCore} from "../../libs/applySchemaObjectCore";
import {Key, ObjectTypeOf, SchemaObject, Values} from "../../libs/types";
import {applySchemaObjectCore} from "../../libs/applySchemaObjectCore";
import {onErrorDefault, onFinishedDefault} from "../../libs/BaseSchema";

export interface Options
export interface Options<S>
{
schemaObject?: SchemaObject;
schemaObject?: S;
}

/**
Expand All @@ -15,7 +15,7 @@ export interface Options
* @returns ends applying
* @throws {ValueSchemaError}
*/
export function applyTo<T>(values: Values, options: Options, keyStack: Key[]): values is Values<T>
export function applyTo<S extends SchemaObject>(values: Values, options: Options<S>, keyStack: Key[]): values is Values<ObjectTypeOf<S>>
{
if(options.schemaObject === undefined)
{
Expand Down
7 changes: 3 additions & 4 deletions src/libs/applySchemaObject.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {SchemaObject, applySchemaObjectCore} from "./applySchemaObjectCore";
import {applySchemaObjectCore} from "./applySchemaObjectCore";
import {ErrorHandler, FinishHandler, onErrorDefault, onFinishedDefault} from "./BaseSchema";

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

export {SchemaObject} from "./applySchemaObjectCore";
export {ErrorHandler} from "./BaseSchema";

/**
Expand All @@ -14,7 +13,7 @@ export {ErrorHandler} from "./BaseSchema";
* @param onFinished finish handler
* @returns applied data
*/
export function applySchemaObject<T extends Empty>(schemaObject: SchemaObject, data: unknown, onError: ErrorHandler = onErrorDefault, onFinished: FinishHandler = onFinishedDefault): T
export function applySchemaObject<S extends SchemaObject>(schemaObject: S, data: unknown, onError: ErrorHandler = onErrorDefault, onFinished: FinishHandler = onFinishedDefault): ObjectTypeOf<S>
{
return applySchemaObjectCore(schemaObject, data, onError, onFinished, []);
}
12 changes: 5 additions & 7 deletions src/libs/applySchemaObjectCore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {AnyObject, Empty, Key, isObject} from "./types";
import {BaseSchema, ErrorHandler, FinishHandler} from "./BaseSchema";
import {AnyObject, Key, ObjectTypeOf, SchemaObject, isObject} from "./types";
import {ErrorHandler, FinishHandler} from "./BaseSchema";
import {CAUSE, ValueSchemaError} from "./ValueSchemaError";

export type SchemaObject = Record<string, BaseSchema>

/**
* apply schema object to data
* @param schemaObject schema object
Expand All @@ -13,7 +11,7 @@ export type SchemaObject = Record<string, BaseSchema>
* @param keyStack path to key that caused error
* @returns applied data
*/
export function applySchemaObjectCore<T extends Empty>(schemaObject: SchemaObject, data: unknown, onError: ErrorHandler, onFinished: FinishHandler, keyStack: Key[]): T
export function applySchemaObjectCore<S extends SchemaObject>(schemaObject: S, data: unknown, onError: ErrorHandler, onFinished: FinishHandler, keyStack: Key[]): ObjectTypeOf<S>
{
if(!isObject(data))
{
Expand All @@ -24,7 +22,7 @@ export function applySchemaObjectCore<T extends Empty>(schemaObject: SchemaObjec
throw err;
}

return result as T;
return result as ObjectTypeOf<S>;
}

const appliedObject: AnyObject = {};
Expand All @@ -39,7 +37,7 @@ export function applySchemaObjectCore<T extends Empty>(schemaObject: SchemaObjec
{
onFinished();
}
return appliedObject as T;
return appliedObject as ObjectTypeOf<S>;

/**
* error handler (to avoid "no-loop-func" error on eslint)
Expand Down
23 changes: 18 additions & 5 deletions src/libs/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import {BaseSchema} from "./BaseSchema";

class AS<T> extends BaseSchema<T[]>
{}
class OS<S extends SchemaObject> extends BaseSchema<ObjectTypeOf<S>>
{}
export type ObjectTypeOf<S extends SchemaObject> = {
[K in keyof S]:
S[K] extends AS<infer T> ? T[] :
S[K] extends OS<infer S2> ? ObjectTypeOf<S2> :
S[K] extends BaseSchema<boolean> ? boolean :
S[K] extends BaseSchema<number> ? number :
S[K] extends BaseSchema<string> ? string :
never
}

type Scalar = boolean | number | string;

export type AnyObject = Record<string, unknown>;

export interface Empty
{
}
export type SchemaObject = Record<string, BaseSchema>

export type Key = string | number;

Expand Down Expand Up @@ -102,7 +115,7 @@ export function isArray(value: unknown): value is unknown[]
* @param value value to check
* @returns Yes/No
*/
export function isObject(value: unknown): value is Record<string, unknown>
export function isObject(value: unknown): value is AnyObject
{
if(value === null)
{
Expand Down
12 changes: 6 additions & 6 deletions src/schemas/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import * as schema from "../appliers/object/schema";
import * as type from "../appliers/object/type";

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

type OptionsForObject =
type OptionsForObject<S> =
converter.Options<AnyObject> |
ifUndefined.Options<AnyObject> |
ifEmptyString.Options<AnyObject> |
ifNull.Options<AnyObject> |
schema.Options |
schema.Options<S> |
type.Options;

class ObjectSchema extends BaseSchema<AnyObject>
class ObjectSchema<S extends SchemaObject> extends BaseSchema<ObjectTypeOf<S>>
{
constructor(options: OptionsForObject)
constructor(options: OptionsForObject<S>)
{
super(options, [
ifUndefined.applyTo,
Expand All @@ -36,7 +36,7 @@ class ObjectSchema extends BaseSchema<AnyObject>
* @param options Options
* @returns schema
*/
export function object(options: OptionsForObject = {}): ObjectSchema
export function object<S extends SchemaObject>(options: OptionsForObject<S> = {}): ObjectSchema<S>
{
return new ObjectSchema(options);
}

0 comments on commit eb435f0

Please sign in to comment.