Skip to content

Commit

Permalink
4.0.2
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
syuilo committed Apr 4, 2018
1 parent 0795fec commit 3a19c0c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
4.0.2 / 2018-04-04
------------------
* より良い型定義

4.0.1 / 2018-04-04
------------------
* READMEの更新
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cafy",
"version": "4.0.1",
"version": "4.0.2",
"description": "Simple, fun, flexible validator",
"keywords": [
"validator"
Expand Down
15 changes: 5 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@ import ObjectQuery from './types/object';
import StringQuery from './types/string';
import Query from './query';

export function createArrayQuery(): ArrayQuery<any, AnyQuery>;
export function createArrayQuery(q: ArrayQuery<any, Query<any>>): ArrayQuery<any[], ArrayQuery<any, Query<any>>>;
export function createArrayQuery(q: BooleanQuery): ArrayQuery<boolean, BooleanQuery>;
export function createArrayQuery(q: NumberQuery): ArrayQuery<number, NumberQuery>;
export function createArrayQuery(q: ObjectQuery): ArrayQuery<{ [x: string]: any }, ObjectQuery>;
export function createArrayQuery(q: StringQuery): ArrayQuery<string, StringQuery>;
export function createArrayQuery(q: AnyQuery): ArrayQuery<any, AnyQuery>;
export function createArrayQuery(q?: Query<any>): any {
export function createArrayQuery(): ArrayQuery<any>;
export function createArrayQuery<T extends Query<any>>(q: T): ArrayQuery<T>;
export function createArrayQuery(q?: Query<any>): ArrayQuery<any> {
const lazy = this.lazy;
const value = this.value;
const optional = this.optional;
const nullable = this.nullable;

return q == null
? new ArrayQuery<any, AnyQuery>(optional, nullable, lazy, value)
: new ArrayQuery<any, AnyQuery>(optional, nullable, lazy, value, q);
? new ArrayQuery(optional, nullable, lazy, value)
: new ArrayQuery(optional, nullable, lazy, value, q);
}

export type Types = {
Expand Down
20 changes: 17 additions & 3 deletions src/types/array.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import Query from '../query';
import $ from '../index';
import StringQuery from './string';
import NumberQuery from './number';
import AnyQuery from './any';
import BooleanQuery from './boolean';
import ObjectQuery from './object';

export const isAnArray = x => Array.isArray(x);
export const isNotAnArray = x => !isAnArray(x);
const hasDuplicates = (array: any[]) => (new Set(array)).size !== array.length;

export type TypeOf<Q> =
Q extends StringQuery ? string :
Q extends NumberQuery ? number :
Q extends BooleanQuery ? boolean :
Q extends ObjectQuery ? { [x: string]: any } :
Q extends ArrayQuery<Query<infer R>> ? R[] :
Q extends AnyQuery ? any :
any;

/**
* Array
*/
export default class ArrayQuery<T, Q extends Query<any>> extends Query<T[]> {
export default class ArrayQuery<Q extends Query<any>> extends Query<TypeOf<Q>[]> {
constructor(optional: boolean, nullable: boolean, lazy: boolean, value?: any, q?: Query<any>) {
super(optional, nullable, lazy, value);

Expand Down Expand Up @@ -91,7 +105,7 @@ export default class ArrayQuery<T, Q extends Query<any>> extends Query<T[]> {
* @param index インデックス
* @param validator バリデータ
*/
public item(index: number, validator: ((element: T) => boolean | Error) | Query<any>) {
public item(index: number, validator: ((element: TypeOf<Q>) => boolean | Error) | Query<any>) {
const validate = validator instanceof Query ? validator.test : validator;
this.pushValidator(v => {
const result = validate(v[index]);
Expand All @@ -111,7 +125,7 @@ export default class ArrayQuery<T, Q extends Query<any>> extends Query<T[]> {
* バリデータが false またはエラーを返した場合エラーにします
* @param validator バリデータ
*/
public each(validator: ((element: T, index: number, array: T[]) => boolean | Error) | Query<any>) {
public each(validator: ((element: TypeOf<Q>, index: number, array: TypeOf<Q>[]) => boolean | Error) | Query<any>) {
const validate = validator instanceof Query ? validator.test : validator;
this.pushValidator(v => {
let err: Error;
Expand Down

0 comments on commit 3a19c0c

Please sign in to comment.