Skip to content

Commit

Permalink
Merge branch 'v4' into feature/ValueSchemaError
Browse files Browse the repository at this point in the history
  • Loading branch information
shimataro committed Jan 4, 2025
2 parents 1f89a74 + 3febbdc commit 99e6605
Show file tree
Hide file tree
Showing 51 changed files with 5,128 additions and 17,232 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ rules: # https://eslint.org/docs/rules/
rest-spread-spacing:
- error
- never
semi: off
semi: error
semi-spacing: error
semi-style:
- error
Expand Down Expand Up @@ -318,7 +318,6 @@ rules: # https://eslint.org/docs/rules/
"@typescript-eslint/no-use-before-define":
- error
- functions: false
"@typescript-eslint/semi": error
"@typescript-eslint/strict-boolean-expressions":
- error
- allowString: false
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/build-package/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inputs:
node-version:
description: Node.js version for building package
required: false
default: "16"
default: "18"
registry-url:
description: Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN.
required: false
Expand Down
105 changes: 87 additions & 18 deletions .github/workflows/examples-macos.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# https://help.github.com/en/articles/workflow-syntax-for-github-actions
# NOTE: Node.js has supported Apple Silicon (macOS 14) as of v16
# NOTE: Deno has supported Apple Silicon (macOS 14) as of v1.6
name: macOS

on:
Expand All @@ -14,8 +16,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
# - macos-14
nodejs:
- "13.2.0"
fail-fast: false
Expand All @@ -30,8 +32,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
nodejs:
- "4.0.0"
- "6.0.0"
Expand All @@ -41,6 +43,19 @@ jobs:
- "14.0.0"
- "16.0.0"
- "18.0.0"
exclude:
- os: macos-14
nodejs: "4.0.0"
- os: macos-14
nodejs: "6.0.0"
- os: macos-14
nodejs: "8.0.0"
- os: macos-14
nodejs: "10.0.0"
- os: macos-14
nodejs: "12.0.0"
- os: macos-14
nodejs: "14.0.0"
fail-fast: false
uses: "./.github/workflows/reusable-cjs.yml"
with:
Expand All @@ -53,8 +68,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
# - macos-14
nodejs:
- "8.5.0"
- "10.0.0"
Expand All @@ -72,12 +87,15 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
nodejs:
- "14.0.0"
- "16.0.0"
- "18.0.0"
exclude:
- os: macos-14
nodejs: "14.0.0"
fail-fast: false
uses: "./.github/workflows/reusable-esm.yml"
with:
Expand All @@ -90,8 +108,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
nodejs:
- "4.0.0"
- "6.0.0"
Expand All @@ -107,6 +125,19 @@ jobs:
- "3.7"
- "3.8"
- "3.9"
exclude:
- os: macos-14
nodejs: "4.0.0"
- os: macos-14
nodejs: "6.0.0"
- os: macos-14
nodejs: "8.0.0"
- os: macos-14
nodejs: "10.0.0"
- os: macos-14
nodejs: "12.0.0"
- os: macos-14
nodejs: "14.0.0"
fail-fast: false
uses: "./.github/workflows/reusable-typescript.yml"
with:
Expand All @@ -120,8 +151,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
nodejs:
- "4.0.0"
- "6.0.0"
Expand All @@ -147,6 +178,18 @@ jobs:
typescript: "4.0"
- nodejs: "6.0.0"
typescript: "4.0"
- os: macos-14
nodejs: "4.0.0"
- os: macos-14
nodejs: "6.0.0"
- os: macos-14
nodejs: "8.0.0"
- os: macos-14
nodejs: "10.0.0"
- os: macos-14
nodejs: "12.0.0"
- os: macos-14
nodejs: "14.0.0"
fail-fast: false
uses: "./.github/workflows/reusable-typescript.yml"
with:
Expand All @@ -160,8 +203,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
nodejs:
- "8.3.0" # TypeScript 5 uses "spread syntax in object literals" internally, that is supported as of Node.js v8.3.0; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntaxhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
- "10.0.0"
Expand All @@ -180,6 +223,14 @@ jobs:
nodejs: "10.0.0"
- typescript: "5.1"
nodejs: "12.0.0"
- os: macos-14
nodejs: "8.3.0"
- os: macos-14
nodejs: "10.0.0"
- os: macos-14
nodejs: "12.0.0"
- os: macos-14
nodejs: "14.0.0"
fail-fast: false
uses: "./.github/workflows/reusable-typescript.yml"
with:
Expand All @@ -192,8 +243,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
deno:
- "1.0.0"
- "1.1.0"
Expand Down Expand Up @@ -231,6 +282,24 @@ jobs:
- "1.33.0"
- "1.34.0"
- "1.35.0"
exclude:
- os: macos-14
deno: "1.0.0"
- os: macos-14
deno: "1.1.0"
- os: macos-14
deno: "1.2.0"
- os: macos-14
deno: "1.3.0"
- os: macos-14
deno: "1.4.0"
- os: macos-14
deno: "1.5.0"
# Deno v1.23.0 and v1.24.0 on Apple Silicon causes fatal error; "Check failed: RwxMemoryWriteScope::IsAllowed()."
- os: macos-14
deno: "1.23.0"
- os: macos-14
deno: "1.24.0"
fail-fast: false
uses: "./.github/workflows/reusable-deno.yml"
with:
Expand All @@ -242,8 +311,8 @@ jobs:
strategy:
matrix:
os:
- macos-11
- macos-12
- macos-13
- macos-14
bun:
- "1.0.0"
fail-fast: false
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

* `map` rule

### Others

* support Bun>=1.0.0
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ const schemaObject = { // schema for input
separatedBy: "-",
checksum: vs.NUMERIC_STRING.CHECKSUM_ALGORITHM.CREDIT_CARD,
}),
remoteAddr: vs.string({ // IPv4
remoteAddr: vs.string({ // IPv4, mapped to "remote_addr" property
pattern: vs.STRING.PATTERN.IPV4,
map: "remote_addr",
}),
remoteAddrIpv6: vs.string({ // IPv6
remoteAddrIpv6: vs.string({ // IPv6, mapped to "remote-addr-ipv6" property
pattern: vs.STRING.PATTERN.IPV6,
map: "remote-addr-ipv6",
}),
limit: vs.number({ // number, integer, omittable (sets 10 if omitted), >=1 (sets 1 if less), <=100 (sets 100 if greater)
ifUndefined: 10,
Expand Down Expand Up @@ -122,8 +124,8 @@ const input = { // input values
classes: "1,3,abc,4",
skills: "c,c++,javascript,python,,swift,kotlin",
creditCard: "4111-1111-1111-1111",
remoteAddr: "127.0.0.1",
remoteAddrIpv6: "::1",
remote_ddr: "127.0.0.1",
"remote-addr-ipv6": "::1",
limit: "0",
};
const expected = { // should be transformed to this
Expand Down
3 changes: 1 addition & 2 deletions dist-deno/appliers/date/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isDate, isValidDate, Key, Values } from "../../libs/types.ts";
import { RULE, ValueSchemaError } from "../../libs/ValueSchemaError.ts";
export interface Rules {
}
export type Rules = object;
/**
* apply schema
* @param values input/output values
Expand Down
3 changes: 1 addition & 2 deletions dist-deno/appliers/email/maxLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { RULE, 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 Rules {
}
export type Rules = object;
/**
* apply schema
* @param values input/output values
Expand Down
16 changes: 16 additions & 0 deletions dist-deno/appliers/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isObject } from "../libs/types.ts";
export interface Rules {
/** map to input property (only available in object) */
map?: string;
}
/**
* check the input has rules of map or not
* @param rules unknown rules
* @returns Yes/No
*/
export function hasMapRules(rules: unknown): rules is Required<Rules> {
if (!isObject(rules)) {
return false;
}
return typeof rules.map === "string";
}
3 changes: 1 addition & 2 deletions dist-deno/appliers/numericString/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { isString, Key, Values } from "../../libs/types.ts";
import { RULE, ValueSchemaError } from "../../libs/ValueSchemaError.ts";
const REGEXP = /^\d+$/;
export interface Rules {
}
export type Rules = object;
/**
* apply schema
* @param values input/output values
Expand Down
3 changes: 1 addition & 2 deletions dist-deno/appliers/object/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isObject, Key, Values } from "../../libs/types.ts";
import { RULE, ValueSchemaError } from "../../libs/ValueSchemaError.ts";
export interface Rules {
}
export type Rules = object;
/**
* apply schema
* @param values input/output values
Expand Down
6 changes: 4 additions & 2 deletions dist-deno/libs/applySchemaObjectCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function applySchemaObjectCore<S extends SchemaObject>(schemaObject: S, d
const appliedObject: AnyObject = {};
let hasError = false;
for (const key of Object.keys(schemaObject)) {
// A trick in order to call _applyTo() private method from the outside (like "friend")
appliedObject[key] = schemaObject[key]["_applyTo"](data[key], errorHandler, [...keyStack, key]);
const schema = schemaObject[key];
// A trick to call non-public properties/methods from the outside (like "friend" in C++)
const prop = schema["_getPropertyName"](key);
appliedObject[key] = schema["_applyTo"](data[prop], errorHandler, [...keyStack, prop]);
}
if (hasError) {
onFinishWithError();
Expand Down
3 changes: 2 additions & 1 deletion dist-deno/schemaClasses/ArraySchema.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as ifEmptyString from "../appliers/ifEmptyString.ts";
import * as ifNull from "../appliers/ifNull.ts";
import * as ifUndefined from "../appliers/ifUndefined.ts";
import * as map from "../appliers/map.ts";
import * as transform from "../appliers/transform.ts";
import * as each from "../appliers/array/each.ts";
import * as maxLength from "../appliers/array/maxLength.ts";
import * as minLength from "../appliers/array/minLength.ts";
import * as type from "../appliers/array/type.ts";
import { BaseSchema } from "./BaseSchema.ts";
export type RulesForArray<T> = transform.Rules<T[]> & ifUndefined.Rules<T[]> & ifEmptyString.Rules<T[]> & ifNull.Rules<T[]> & each.Rules<T> & minLength.Rules & maxLength.Rules & type.Rules;
export type RulesForArray<T> = transform.Rules<T[]> & map.Rules & ifUndefined.Rules<T[]> & ifEmptyString.Rules<T[]> & ifNull.Rules<T[]> & each.Rules<T> & minLength.Rules & maxLength.Rules & type.Rules;
export class ArraySchema<T, Tx = never> extends BaseSchema<T[] | Tx> {
constructor(rules: RulesForArray<T>) {
super(rules, [
Expand Down
17 changes: 15 additions & 2 deletions dist-deno/schemaClasses/BaseSchema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { hasMapRules } from "../appliers/map.ts";
import { Key, makeValues, Values } from "../libs/types.ts";
import { ErrorHandler } from "../libs/publicTypes.ts";
import { ValueSchemaError } from "../libs/ValueSchemaError.ts";
interface Rules {
}
type Rules = object;
type ApplyTo<T> = (values: Values, rules: Rules, keyStack: Key[]) => values is Values<T>;
/**
* Base Schema Class
Expand Down Expand Up @@ -42,6 +42,19 @@ export class BaseSchema<T = unknown> {
return onError(err as ValueSchemaError);
}
}
/**
* get a "mapped" property
* @param defaultProperty default property
* @returns mapped property
* @see Rules (appliers/map.ts)
* @protected in order to repress TS6133 error
*/
protected _getPropertyName(defaultProperty: string): string {
if (!hasMapRules(this.rules)) {
return defaultProperty;
}
return this.rules.map;
}
}
/**
* default error handler
Expand Down
Loading

0 comments on commit 99e6605

Please sign in to comment.