Merge pull request #3 from Start9Labs/feat/remove-subtype

remove subtype and also inputmode for numbers
This commit is contained in:
J H
2023-04-03 09:28:19 -06:00
committed by GitHub
6 changed files with 20 additions and 54 deletions

View File

@@ -1,15 +1,15 @@
describe("test", () => {
test("test", () => {
expect(true).toEqual(true);
});
});
import { values } from "lodash";
import { Validator } from "ts-matches";
import { testOutput } from "../../test/output.test"; import { testOutput } from "../../test/output.test";
import { Config } from "./config"; import { Config } from "./config";
import { List } from "./list"; import { List } from "./list";
import { Value } from "./value"; import { Value } from "./value";
import { Variants } from "./variants"; import { Variants } from "./variants";
describe("test", () => {
test("test", () => {
expect(true).toEqual(true);
});
});
describe("builder tests", () => { describe("builder tests", () => {
test("String", () => { test("String", () => {
console.log("BLUJ->"); console.log("BLUJ->");

View File

@@ -1,31 +1,8 @@
import { BuilderExtract, IBuilder } from "./builder"; import { BuilderExtract, IBuilder } from "./builder";
import { Config } from "./config"; import { Config } from "./config";
import { InputSpec, ListValueSpecNumber, ListValueSpecString, UniqueBy, ValueSpecList } from "../config-types"; import { InputSpec, ListValueSpecString, UniqueBy, ValueSpecList } from "../config-types";
import { guardAll } from "../../util"; import { guardAll } from "../../util";
import { range } from "lodash";
/**
* Used as a subtype of Value.list
```ts
export const authorizationList = List.string({
"name": "Authorization",
"range": "[0,*)",
"spec": {
"masked": null,
"placeholder": null,
"pattern": "^[a-zA-Z0-9_-]+:([0-9a-fA-F]{2})+\\$([0-9a-fA-F]{2})+$",
"patternDescription":
'Each item must be of the form "<USERNAME>:<SALT>$<HASH>".',
"textarea": false,
},
"default": [],
"description":
"Username and hashed password for JSON-RPC connections. RPC clients connect using the usual http basic authentication.",
"warning": null,
});
```
*/
export class List<A extends ValueSpecList> extends IBuilder<A> { export class List<A extends ValueSpecList> extends IBuilder<A> {
static string( static string(
a: { a: {
@@ -48,6 +25,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
} }
) { ) {
const spec = { const spec = {
type: 'string' as const,
placeholder: null, placeholder: null,
pattern: null, pattern: null,
patternDescription: null, patternDescription: null,
@@ -60,7 +38,6 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
warning: null, warning: null,
default: [], default: [],
type: "list" as const, type: "list" as const,
subtype: "string" as const,
range: "(*,*)", range: "(*,*)",
...a, ...a,
spec, spec,
@@ -82,13 +59,11 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
range?: string; range?: string;
units?: string | null; units?: string | null;
placeholder?: string | null; placeholder?: string | null;
/** Default = "decimal" */
inputmode?: ListValueSpecNumber["inputmode"];
} }
) { ) {
const spec = { const spec = {
type: "number" as const,
placeholder: null, placeholder: null,
inputmode: "decimal" as const,
range: "(*,*)", range: "(*,*)",
units: null, units: null,
...aSpec, ...aSpec,
@@ -100,7 +75,6 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
range: "(*,*)", range: "(*,*)",
default: [], default: [],
type: "list" as const, type: "list" as const,
subtype: "number" as const,
...a, ...a,
spec, spec,
}); });
@@ -123,6 +97,7 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
const { spec: previousSpecSpec, ...restSpec } = aSpec; const { spec: previousSpecSpec, ...restSpec } = aSpec;
const specSpec = previousSpecSpec.build() as BuilderExtract<Spec>; const specSpec = previousSpecSpec.build() as BuilderExtract<Spec>;
const spec = { const spec = {
type: "object" as const,
displayAs: null, displayAs: null,
uniqueBy: null, uniqueBy: null,
...restSpec, ...restSpec,
@@ -137,7 +112,6 @@ export class List<A extends ValueSpecList> extends IBuilder<A> {
warning: null, warning: null,
range: "(*,*)", range: "(*,*)",
type: "list" as const, type: "list" as const,
subtype: "object" as const,
...value, ...value,
}); });
} }

View File

@@ -4,12 +4,10 @@ import { List } from "./list";
import { Variants } from "./variants"; import { Variants } from "./variants";
import { import {
InputSpec, InputSpec,
ListValueSpecNumber,
ListValueSpecString, ListValueSpecString,
ValueSpec, ValueSpec,
ValueSpecList, ValueSpecList,
ValueSpecNumber, ValueSpecNumber,
ValueSpecString,
ValueSpecTextarea, ValueSpecTextarea,
} from "../config-types"; } from "../config-types";
import { guardAll } from "../../util"; import { guardAll } from "../../util";
@@ -100,12 +98,9 @@ export class Value<A extends ValueSpec> extends IBuilder<A> {
integral: boolean; integral: boolean;
units?: string | null; units?: string | null;
placeholder?: string | null; placeholder?: string | null;
/** Default = 'decimal' */
inputmode?: ListValueSpecNumber["inputmode"];
}) { }) {
return new Value({ return new Value({
type: "number" as const, type: "number" as const,
inputmode: "decimal",
description: null, description: null,
warning: null, warning: null,
default: null, default: null,

View File

@@ -37,7 +37,6 @@ export type ValueSpecOf<T extends ValueType> = T extends "string"
: never; : never;
export interface ValueSpecString extends ListValueSpecString, WithStandalone { export interface ValueSpecString extends ListValueSpecString, WithStandalone {
type: "string";
required: boolean; required: boolean;
default: DefaultString | null; default: DefaultString | null;
} }
@@ -49,7 +48,6 @@ export interface ValueSpecTextarea extends WithStandalone {
} }
export interface ValueSpecNumber extends ListValueSpecNumber, WithStandalone { export interface ValueSpecNumber extends ListValueSpecNumber, WithStandalone {
type: "number";
required: boolean; required: boolean;
default: number | null; default: number | null;
} }
@@ -116,7 +114,6 @@ export type ValueSpecList = ValueSpecListOf<ListValueSpecType>;
export interface ValueSpecListOf<T extends ListValueSpecType> export interface ValueSpecListOf<T extends ListValueSpecType>
extends WithStandalone { extends WithStandalone {
type: "list"; type: "list";
subtype: T;
spec: ListValueSpecOf<T>; spec: ListValueSpecOf<T>;
range: string; // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules range: string; // '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules
default: default:
@@ -135,27 +132,29 @@ export function isValueSpecListOf<S extends ListValueSpecType>(
t: ValueSpecList, t: ValueSpecList,
s: S s: S
): t is ValueSpecListOf<S> { ): t is ValueSpecListOf<S> {
return t.subtype === s; return t.spec.type === s;
} }
export interface ListValueSpecString { export interface ListValueSpecString {
type: 'string'
pattern: string | null; pattern: string | null;
patternDescription: string | null; patternDescription: string | null;
masked: boolean; // default = false masked: boolean; // default = false
inputmode: 'text' | 'email' | 'tel' | 'url' // default = 'text' inputmode: 'text' | 'email' | 'tel' | 'url'; // default = 'text'
placeholder: string | null; placeholder: string | null;
} }
export interface ListValueSpecNumber { export interface ListValueSpecNumber {
type: 'number';
/** '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules */ /** '[0,1]' (inclusive) OR '[0,*)' (right unbounded), normal math rules */
range: string; range: string;
integral: boolean; // default = false integral: boolean; // default = false
units: string | null; units: string | null;
inputmode: 'numeric' | 'decimal' // default = 'decimal'
placeholder: string | null; placeholder: string | null;
} }
export interface ListValueSpecObject { export interface ListValueSpecObject {
type: 'object';
/** this is a mapped type of the config object at this level, replacing the object's values with specs on those values */ /** this is a mapped type of the config object at this level, replacing the object's values with specs on those values */
spec: InputSpec; spec: InputSpec;
/** indicates whether duplicates can be permitted in the list */ /** indicates whether duplicates can be permitted in the list */

View File

@@ -1,5 +1,5 @@
import { UnionSelectKey, unionSelectKey } from "../config/config-types"; import { UnionSelectKey, unionSelectKey } from "../config/config-types";
import { InputSpec, matchInputSpec, threads } from "./output"; import { InputSpec, matchInputSpec } from "./output";
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2 export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends U ? 1 : 2
? Y ? Y

View File

@@ -49,8 +49,8 @@ type GuardObject<A> =
unknown unknown
// prettier-ignore // prettier-ignore
export type GuardList<A> = export type GuardList<A> =
A extends { type: TypeList, subtype: infer B, spec?: { spec?: infer C } } ? Array<GuardAll<Omit<A, "type" | "subtype" | "spec"> & ({ type: B, spec: C })>> : A extends { type: TypeList, spec?: { type: infer B, spec?: infer C } } ? Array<GuardAll<Omit<A, "type" | "spec"> & ({ type: B, spec: C })>> :
A extends { type: TypeList, subtype: infer B, spec?: {} } ? Array<GuardAll<Omit<A, "type"> & ({ type: B })>> : A extends { type: TypeList, spec?: { type: infer B } } ? Array<GuardAll<Omit<A, "type"> & ({ type: B })>> :
unknown unknown
// prettier-ignore // prettier-ignore
type GuardSelect<A> = type GuardSelect<A> =
@@ -102,7 +102,6 @@ const rangeRegex = /(\[|\()(\*|(\d|\.)+),(\*|(\d|\.)+)(\]|\))/;
const matchRange = object({ range: string }); const matchRange = object({ range: string });
const matchIntegral = object({ integral: literals(true) }); const matchIntegral = object({ integral: literals(true) });
const matchSpec = object({ spec: recordString }); const matchSpec = object({ spec: recordString });
const matchSubType = object({ subtype: string });
const matchUnion = object({ const matchUnion = object({
variants: dictionary([string, matchVariant]), variants: dictionary([string, matchVariant]),
}); });
@@ -230,10 +229,9 @@ export function guardAll<A extends ValueSpecAny>(value: A): Parser<unknown, Guar
const spec = (matchSpec.test(value) && value.spec) || {}; const spec = (matchSpec.test(value) && value.spec) || {};
const rangeValidate = (matchRange.test(value) && matchNumberWithRange(value.range).test) || (() => true); const rangeValidate = (matchRange.test(value) && matchNumberWithRange(value.range).test) || (() => true);
const subtype = matchSubType.unsafeCast(value).subtype;
return defaultRequired( return defaultRequired(
matches matches
.arrayOf(guardAll({ type: subtype, ...spec } as any)) .arrayOf(guardAll(spec as any))
.validate((x) => rangeValidate(x.length), "valid length"), .validate((x) => rangeValidate(x.length), "valid length"),
value value
) as any; ) as any;