-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from shimataro/develop
version 3.0.0-rc.12
- Loading branch information
Showing
60 changed files
with
1,825 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ node_modules/ | |
|
||
# auto-generated files | ||
/dist/ | ||
/dist-deno/ | ||
/coverage/ | ||
|
||
# IDE files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/.github/ | ||
/coverage/ | ||
/dist-deno/ | ||
/examples/ | ||
/node_modules/ | ||
/scripts/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Key, Values, isArray } from "../../libs/types.ts"; | ||
import { ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
import { BaseSchema } from "../../schemaClasses/BaseSchema.ts"; | ||
type Each<T> = { | ||
schema: BaseSchema<T>; | ||
ignoresErrors: boolean; | ||
}; | ||
type EachLike<T> = BaseSchema<T> | Each<T>; | ||
export interface Options<T> { | ||
each?: EachLike<T>; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options<T>, keyStack: Key[]): values is Values<T[]> { | ||
const each = normalizeOptions(options.each); | ||
if (each === undefined) { | ||
return false; | ||
} | ||
// istanbul ignore next | ||
if (!isArray(values.output)) { | ||
return false; | ||
} | ||
const adjustedValues: T[] = []; | ||
for (let idx = 0; idx < values.output.length; idx++) { | ||
const element = values.output[idx]; | ||
// A trick in order to call _applyTo() private method from the outside (like "friend") | ||
try { | ||
const adjustedValue = each.schema["_applyTo"](element, (err) => { | ||
if (each.ignoresErrors) { | ||
throw Error("!IGNORE!"); | ||
} | ||
return ValueSchemaError.raise(err.cause, values, err.keyStack); | ||
}, [...keyStack, idx]); | ||
adjustedValues.push(adjustedValue); | ||
} | ||
catch (err) { | ||
if (err.message === "!IGNORE!") { | ||
// ignore | ||
continue; | ||
} | ||
throw err; | ||
} | ||
} | ||
// replace with adjusted values | ||
values.output = adjustedValues; | ||
return false; | ||
} | ||
/** | ||
* normalize options | ||
* @param each each | ||
* @returns normalized options | ||
*/ | ||
function normalizeOptions<T>(each?: EachLike<T>): Each<T> | void { | ||
if (each === undefined) { | ||
return; | ||
} | ||
if (each instanceof BaseSchema) { | ||
return { | ||
schema: each as BaseSchema<T>, | ||
ignoresErrors: false | ||
}; | ||
} | ||
return each; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Key, Values, isArray, isNumber } from "../../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
type MaxLength = { | ||
length: number; | ||
trims: boolean; | ||
}; | ||
type MaxLengthLike = number | MaxLength; | ||
export interface Options { | ||
maxLength?: MaxLengthLike; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options, keyStack: Key[]): values is Values<T> { | ||
const maxLength = normalizeOptions(options.maxLength); | ||
// istanbul ignore next | ||
if (!isArray(values.output)) { | ||
return false; | ||
} | ||
if (values.output.length <= maxLength.length) { | ||
return false; | ||
} | ||
if (maxLength.trims) { | ||
values.output.splice(maxLength.length); | ||
return false; | ||
} | ||
return ValueSchemaError.raise(CAUSE.MAX_LENGTH, values, keyStack); | ||
} | ||
/** | ||
* normalize options | ||
* @param maxLength options | ||
* @returns normalized options | ||
*/ | ||
function normalizeOptions(maxLength?: MaxLengthLike): MaxLength { | ||
const defaultOptions: MaxLength = { | ||
length: Number.MAX_SAFE_INTEGER, | ||
trims: false | ||
}; | ||
if (maxLength === undefined) { | ||
return defaultOptions; | ||
} | ||
if (isNumber(maxLength)) { | ||
return { | ||
...defaultOptions, | ||
length: maxLength | ||
}; | ||
} | ||
return { | ||
...defaultOptions, | ||
...maxLength | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Key, Values, isArray } from "../../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
export interface Options { | ||
minLength?: number; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options, keyStack: Key[]): values is Values<T> { | ||
const normalizedOptions: Required<Options> = { | ||
minLength: 0, | ||
...options | ||
}; | ||
// istanbul ignore next | ||
if (!isArray(values.output)) { | ||
return false; | ||
} | ||
if (values.output.length >= normalizedOptions.minLength) { | ||
return false; | ||
} | ||
return ValueSchemaError.raise(CAUSE.MIN_LENGTH, values, keyStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Key, Values, isArray, isString } from "../../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
export interface Options { | ||
separatedBy?: string | RegExp; | ||
toArray?: boolean; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options, keyStack: Key[]): values is Values<T> { | ||
if (isArray(values.output)) { | ||
// already array | ||
return false; | ||
} | ||
if (isString(values.output) && options.separatedBy !== undefined) { | ||
values.output = values.output.split(options.separatedBy); | ||
return false; | ||
} | ||
if (options.toArray !== undefined && options.toArray) { | ||
values.output = [values.output]; | ||
return false; | ||
} | ||
return ValueSchemaError.raise(CAUSE.TYPE, values, keyStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Key, Values, isBoolean, isNumber, isString } from "../../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
const REGEXP_TRUE = /^\s*(true|yes|on)\s*$/i; | ||
const REGEXP_FALSE = /^\s*(false|no|off)\s*$/i; | ||
export interface Options { | ||
strictType?: boolean; | ||
acceptsAllNumbers?: boolean; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo(values: Values, options: Options, keyStack: Key[]): values is Values<boolean> { | ||
const normalizedOptions: Required<Options> = { | ||
strictType: false, | ||
acceptsAllNumbers: false, | ||
...options | ||
}; | ||
if (isBoolean(values.output)) { | ||
// already boolean | ||
return true; | ||
} | ||
// not boolean | ||
if (normalizedOptions.strictType) { | ||
// strict type check | ||
ValueSchemaError.raise(CAUSE.TYPE, values, keyStack); | ||
} | ||
if (isString(values.output)) { | ||
// "true" is true, "false" is false | ||
if (REGEXP_TRUE.test(values.output)) { | ||
values.output = true; | ||
return true; | ||
} | ||
if (REGEXP_FALSE.test(values.output)) { | ||
values.output = false; | ||
return true; | ||
} | ||
// convert to number | ||
values.output = Number(values.output); | ||
} | ||
if (isNumber(values.output)) { | ||
if (values.output === 0 || values.output === 1 || normalizedOptions.acceptsAllNumbers) { | ||
values.output = Boolean(values.output); | ||
return true; | ||
} | ||
} | ||
return ValueSchemaError.raise(CAUSE.TYPE, values, keyStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Key, Values } from "../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../libs/ValueSchemaError.ts"; | ||
export interface Options<T> { | ||
converter?: (value: T, fail: () => never) => T; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options<T>, keyStack: Key[]): values is Values<T> { | ||
if (options.converter === undefined) { | ||
return false; | ||
} | ||
values.output = options.converter(values.output as T, () => { | ||
return ValueSchemaError.raise(CAUSE.CONVERTER, values, keyStack); | ||
}); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Key, Values, isString } from "../../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../../libs/ValueSchemaError.ts"; | ||
const MAX_LENGTH_LOCAL = 64; | ||
const MAX_LENGTH_DOMAIN = 255; | ||
const MAX_LENGTH = MAX_LENGTH_LOCAL + 1 + MAX_LENGTH_DOMAIN; // local-part + "@" + domain-part | ||
export interface Options { | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo(values: Values, options: Options, keyStack: Key[]): values is Values<string> // eslint-disable-line @typescript-eslint/no-unused-vars | ||
{ | ||
// istanbul ignore next | ||
if (!isString(values.output)) { | ||
return false; | ||
} | ||
if (values.output.length > MAX_LENGTH) { | ||
ValueSchemaError.raise(CAUSE.MAX_LENGTH, values, keyStack); | ||
} | ||
const atPosition = values.output.lastIndexOf("@"); | ||
if (atPosition > MAX_LENGTH_LOCAL) { | ||
// local-part length error | ||
ValueSchemaError.raise(CAUSE.MAX_LENGTH, values, keyStack); | ||
} | ||
if (values.output.length - atPosition - 1 > MAX_LENGTH_DOMAIN) { | ||
// domain-part length error | ||
ValueSchemaError.raise(CAUSE.MAX_LENGTH, values, keyStack); | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { REGEXP_EMAIL } from "../../libs/regexp/email.ts"; | ||
import { Key, Values } from "../../libs/types.ts"; | ||
import * as pattern from "../string/pattern.ts"; | ||
export interface Options { | ||
pattern?: RegExp; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo(values: Values, options: Options, keyStack: Key[]): values is Values<string> { | ||
const normalizedOptions: Required<Options> = { | ||
pattern: REGEXP_EMAIL, | ||
...options | ||
}; | ||
return pattern.applyTo(values, normalizedOptions, keyStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Key, Values } from "../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../libs/ValueSchemaError.ts"; | ||
export interface Options<T> { | ||
ifEmptyString?: T | null; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options<T>, keyStack: Key[]): values is Values<T> { | ||
if (values.output !== "") { | ||
return false; | ||
} | ||
if (options.ifEmptyString !== undefined) { | ||
values.output = options.ifEmptyString; | ||
return true; | ||
} | ||
return ValueSchemaError.raise(CAUSE.EMPTY_STRING, values, keyStack); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Key, Values } from "../libs/types.ts"; | ||
import { CAUSE, ValueSchemaError } from "../libs/ValueSchemaError.ts"; | ||
export interface Options<T> { | ||
ifNull?: T | null; | ||
} | ||
/** | ||
* apply schema | ||
* @param values input/output values | ||
* @param options options | ||
* @param keyStack key stack for error handling | ||
* @returns applied value | ||
*/ | ||
export function applyTo<T>(values: Values, options: Options<T>, keyStack: Key[]): values is Values<T> { | ||
if (values.output !== null) { | ||
return false; | ||
} | ||
if (options.ifNull !== undefined) { | ||
values.output = options.ifNull; | ||
return true; | ||
} | ||
return ValueSchemaError.raise(CAUSE.NULL, values, keyStack); | ||
} |
Oops, something went wrong.