mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
chore: Update to use validator
This commit is contained in:
@@ -1,4 +1,13 @@
|
||||
export { Config } from "./config.ts";
|
||||
export { List } from "./list.ts";
|
||||
export { Value } from "./value.ts";
|
||||
export { Variants } from "./variants.ts";
|
||||
import { Config } from "./config.ts";
|
||||
import { List } from "./list.ts";
|
||||
import { Value } from "./value.ts";
|
||||
import { Variants } from "./variants.ts";
|
||||
|
||||
export {
|
||||
/** @typedef { import("./config.ts").Config } Pet
|
||||
*/
|
||||
Config,
|
||||
List,
|
||||
Value,
|
||||
Variants,
|
||||
};
|
||||
|
||||
@@ -17,17 +17,16 @@ const previousPath = /(.+?)\/([^/]*)$/;
|
||||
})
|
||||
const jsonFile = ConfigFile.json({
|
||||
path: 'data.json',
|
||||
data: someValidator,
|
||||
validator: someValidator,
|
||||
volume: 'main'
|
||||
})
|
||||
const tomlFile = ConfigFile.toml({
|
||||
path: 'data.toml',
|
||||
data: someValidator,
|
||||
validator: someValidator,
|
||||
volume: 'main'
|
||||
})
|
||||
const rawFile = ConfigFile.raw({
|
||||
path: 'data.amazingSettings',
|
||||
data: someValidator,
|
||||
volume: 'main'
|
||||
fromData(dataIn: Data): string {
|
||||
return `myDatais ///- ${dataIn.data}`
|
||||
@@ -56,12 +55,16 @@ export class ConfigFile<A> {
|
||||
volume: string;
|
||||
writeData(dataIn: A): string;
|
||||
readData(stringValue: string): A;
|
||||
}
|
||||
},
|
||||
) {}
|
||||
async write(data: A, effects: T.Effects) {
|
||||
let matched;
|
||||
if ((matched = previousPath.exec(this.options.path)))
|
||||
await effects.createDir({ volumeId: this.options.volume, path: matched[1] });
|
||||
if ((matched = previousPath.exec(this.options.path))) {
|
||||
await effects.createDir({
|
||||
volumeId: this.options.volume,
|
||||
path: matched[1],
|
||||
});
|
||||
}
|
||||
|
||||
await effects.writeFile({
|
||||
path: this.options.path,
|
||||
@@ -74,10 +77,17 @@ export class ConfigFile<A> {
|
||||
await effects.readFile({
|
||||
path: this.options.path,
|
||||
volumeId: this.options.volume,
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
static raw<A>(options: { path: string; volume: string; fromData(dataIn: A): string; toData(rawData: string): A }) {
|
||||
static raw<A>(
|
||||
options: {
|
||||
path: string;
|
||||
volume: string;
|
||||
fromData(dataIn: A): string;
|
||||
toData(rawData: string): A;
|
||||
},
|
||||
) {
|
||||
return new ConfigFile<A>({
|
||||
path: options.path,
|
||||
volume: options.volume,
|
||||
@@ -85,7 +95,13 @@ export class ConfigFile<A> {
|
||||
readData: options.toData,
|
||||
});
|
||||
}
|
||||
static json<A>(options: { path: string; volume: string; data: matches.Validator<unknown, A> }) {
|
||||
static json<A>(
|
||||
options: {
|
||||
path: string;
|
||||
volume: string;
|
||||
validator: matches.Validator<unknown, A>;
|
||||
},
|
||||
) {
|
||||
return new ConfigFile<A>({
|
||||
path: options.path,
|
||||
volume: options.volume,
|
||||
@@ -93,14 +109,14 @@ export class ConfigFile<A> {
|
||||
return JSON.stringify(inData, null, 2);
|
||||
},
|
||||
readData(inString) {
|
||||
return options.data.unsafeCast(JSON.parse(inString));
|
||||
return options.validator.unsafeCast(JSON.parse(inString));
|
||||
},
|
||||
});
|
||||
}
|
||||
static toml<A extends Record<string, unknown>>(options: {
|
||||
path: string;
|
||||
volume: string;
|
||||
data: matches.Validator<unknown, A>;
|
||||
validator: matches.Validator<unknown, A>;
|
||||
}) {
|
||||
return new ConfigFile<A>({
|
||||
path: options.path,
|
||||
@@ -109,14 +125,14 @@ export class ConfigFile<A> {
|
||||
return TOML.stringify(inData);
|
||||
},
|
||||
readData(inString) {
|
||||
return options.data.unsafeCast(TOML.parse(inString));
|
||||
return options.validator.unsafeCast(TOML.parse(inString));
|
||||
},
|
||||
});
|
||||
}
|
||||
static yaml<A extends Record<string, unknown>>(options: {
|
||||
path: string;
|
||||
volume: string;
|
||||
data: matches.Validator<unknown, A>;
|
||||
validator: matches.Validator<unknown, A>;
|
||||
}) {
|
||||
return new ConfigFile<A>({
|
||||
path: options.path,
|
||||
@@ -125,7 +141,7 @@ export class ConfigFile<A> {
|
||||
return YAML.stringify(inData);
|
||||
},
|
||||
readData(inString) {
|
||||
return options.data.unsafeCast(YAML.parse(inString));
|
||||
return options.validator.unsafeCast(YAML.parse(inString));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,9 +16,12 @@ console.log(`
|
||||
const data = JSON.parse(list.join("\n"));
|
||||
|
||||
const namedConsts = new Set(["Config", "Value", "List"]);
|
||||
const configName = newConst("config", convertConfigSpec(data));
|
||||
const configMatcherName = newConst("matchConfig", `${configName}.validator()`);
|
||||
console.log(`export type Config = typeof ${configMatcherName}._TYPE;`);
|
||||
const configName = newConst("configSpec", convertConfigSpec(data));
|
||||
const configMatcherName = newConst(
|
||||
"matchConfigSpec",
|
||||
`${configName}.validator()`,
|
||||
);
|
||||
console.log(`export type ConfigSpec = typeof ${configMatcherName}._TYPE;`);
|
||||
|
||||
function newConst(key: string, data: string) {
|
||||
const variableName = getNextConstName(camelCase(key));
|
||||
@@ -37,7 +40,8 @@ function convertConfigSpec(data: any) {
|
||||
function convertValueSpec(value: any): string {
|
||||
switch (value.type) {
|
||||
case "string": {
|
||||
return `Value.string(${JSON.stringify(
|
||||
return `Value.string(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
default: value.default || null,
|
||||
@@ -51,11 +55,13 @@ function convertValueSpec(value: any): string {
|
||||
textarea: value.textarea || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "number": {
|
||||
return `Value.number(${JSON.stringify(
|
||||
return `Value.number(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
default: value.default || null,
|
||||
@@ -68,11 +74,13 @@ function convertValueSpec(value: any): string {
|
||||
placeholder: value.placeholder || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "boolean": {
|
||||
return `Value.boolean(${JSON.stringify(
|
||||
return `Value.boolean(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
default: value.default || false,
|
||||
@@ -80,11 +88,13 @@ function convertValueSpec(value: any): string {
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "enum": {
|
||||
return `Value.enum(${JSON.stringify(
|
||||
return `Value.enum(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
description: value.description || null,
|
||||
@@ -94,11 +104,15 @@ function convertValueSpec(value: any): string {
|
||||
"value-names": value["value-names"] || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "object": {
|
||||
const specName = newConst(value.name + "_spec", convertConfigSpec(value.spec));
|
||||
const specName = newConst(
|
||||
value.name + "_spec",
|
||||
convertConfigSpec(value.spec),
|
||||
);
|
||||
return `Value.object({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
description: ${JSON.stringify(value.description || null)},
|
||||
@@ -111,23 +125,30 @@ function convertValueSpec(value: any): string {
|
||||
})`;
|
||||
}
|
||||
case "union": {
|
||||
const variants = newConst(value.name + "_variants", convertVariants(value.variants));
|
||||
const variants = newConst(
|
||||
value.name + "_variants",
|
||||
convertVariants(value.variants),
|
||||
);
|
||||
return `Value.union({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
description: ${JSON.stringify(value.description || null)},
|
||||
warning: ${JSON.stringify(value.warning || null)},
|
||||
default: ${JSON.stringify(value.default || null)},
|
||||
variants: ${variants},
|
||||
tag: ${JSON.stringify({
|
||||
tag: ${
|
||||
JSON.stringify({
|
||||
id: value?.tag?.["id"] || null,
|
||||
name: value?.tag?.["name"] || null,
|
||||
description: value?.tag?.["description"] || null,
|
||||
warning: value?.tag?.["warning"] || null,
|
||||
"variant-names": value?.tag?.["variant-names"] || {},
|
||||
})},
|
||||
})
|
||||
},
|
||||
"display-as": ${JSON.stringify(value["display-as"] || null)},
|
||||
"unique-by": ${JSON.stringify(value["unique-by"] || null)},
|
||||
"variant-names": ${JSON.stringify((value["variant-names"] as any) || null)},
|
||||
"variant-names": ${
|
||||
JSON.stringify((value["variant-names"] as any) || null)
|
||||
},
|
||||
})`;
|
||||
}
|
||||
case "list": {
|
||||
@@ -144,7 +165,8 @@ function convertValueSpec(value: any): string {
|
||||
function convertList(value: any) {
|
||||
switch (value.subtype) {
|
||||
case "string": {
|
||||
return `List.string(${JSON.stringify(
|
||||
return `List.string(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
range: value.range || null,
|
||||
@@ -152,7 +174,8 @@ function convertList(value: any) {
|
||||
masked: value?.spec?.["masked"] || null,
|
||||
placeholder: value?.spec?.["placeholder"] || null,
|
||||
pattern: value?.spec?.["pattern"] || null,
|
||||
"pattern-description": value?.spec?.["pattern-description"] || null,
|
||||
"pattern-description": value?.spec?.["pattern-description"] ||
|
||||
null,
|
||||
textarea: value?.spec?.["textarea"] || false,
|
||||
},
|
||||
default: value.default || null,
|
||||
@@ -160,11 +183,13 @@ function convertList(value: any) {
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "number": {
|
||||
return `List.number(${JSON.stringify(
|
||||
return `List.number(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
range: value.range || null,
|
||||
@@ -179,11 +204,13 @@ function convertList(value: any) {
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "enum": {
|
||||
return `List.enum(${JSON.stringify(
|
||||
return `List.enum(${
|
||||
JSON.stringify(
|
||||
{
|
||||
name: value.name || null,
|
||||
range: value.range || null,
|
||||
@@ -196,17 +223,23 @@ function convertList(value: any) {
|
||||
warning: value.warning || null,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)})`;
|
||||
2,
|
||||
)
|
||||
})`;
|
||||
}
|
||||
case "object": {
|
||||
const specName = newConst(value.name + "_spec", convertConfigSpec(value.spec.spec));
|
||||
const specName = newConst(
|
||||
value.name + "_spec",
|
||||
convertConfigSpec(value.spec.spec),
|
||||
);
|
||||
return `List.obj({
|
||||
name: ${JSON.stringify(value.name || null)},
|
||||
range: ${JSON.stringify(value.range || null)},
|
||||
spec: {
|
||||
spec: ${specName},
|
||||
"display-as": ${JSON.stringify(value?.spec?.["display-as"] || null)},
|
||||
"display-as": ${
|
||||
JSON.stringify(value?.spec?.["display-as"] || null)
|
||||
},
|
||||
"unique-by": ${JSON.stringify(value?.spec?.["unique-by"] || null)},
|
||||
},
|
||||
default: ${JSON.stringify(value.default || null)},
|
||||
@@ -215,7 +248,10 @@ function convertList(value: any) {
|
||||
})`;
|
||||
}
|
||||
case "union": {
|
||||
const variants = newConst(value.name + "_variants", convertConfigSpec(value.spec.variants));
|
||||
const variants = newConst(
|
||||
value.name + "_variants",
|
||||
convertConfigSpec(value.spec.variants),
|
||||
);
|
||||
return `List.union(
|
||||
{
|
||||
name:${JSON.stringify(value.name || null)},
|
||||
@@ -223,14 +259,26 @@ function convertList(value: any) {
|
||||
spec: {
|
||||
tag: {
|
||||
"id":${JSON.stringify(value?.spec?.tag?.["id"] || null)},
|
||||
"name": ${JSON.stringify(value?.spec?.tag?.["name"] || null)},
|
||||
"description": ${JSON.stringify(value?.spec?.tag?.["description"] || null)},
|
||||
"warning": ${JSON.stringify(value?.spec?.tag?.["warning"] || null)},
|
||||
"variant-names": ${JSON.stringify(value?.spec?.tag?.["variant-names"] || {})},
|
||||
"name": ${
|
||||
JSON.stringify(value?.spec?.tag?.["name"] || null)
|
||||
},
|
||||
"description": ${
|
||||
JSON.stringify(value?.spec?.tag?.["description"] || null)
|
||||
},
|
||||
"warning": ${
|
||||
JSON.stringify(value?.spec?.tag?.["warning"] || null)
|
||||
},
|
||||
"variant-names": ${
|
||||
JSON.stringify(value?.spec?.tag?.["variant-names"] || {})
|
||||
},
|
||||
},
|
||||
variants: ${variants},
|
||||
"display-as": ${JSON.stringify(value?.spec?.["display-as"] || null)},
|
||||
"unique-by": ${JSON.stringify(value?.spec?.["unique-by"] || null)},
|
||||
"display-as": ${
|
||||
JSON.stringify(value?.spec?.["display-as"] || null)
|
||||
},
|
||||
"unique-by": ${
|
||||
JSON.stringify(value?.spec?.["unique-by"] || null)
|
||||
},
|
||||
default: ${JSON.stringify(value?.spec?.["default"] || null)},
|
||||
},
|
||||
default: ${JSON.stringify(value.default || null)},
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as PM from "./propertiesMatcher.ts";
|
||||
import { expect } from "https://deno.land/x/expect@v0.2.9/mod.ts";
|
||||
import { matches } from "../dependencies.ts";
|
||||
import { config as bitcoinPropertiesConfig } from "./test/output.ts";
|
||||
import { configSpec as bitcoinPropertiesConfig } from "./test/output.ts";
|
||||
|
||||
const randWithSeed = (seed = 1) => {
|
||||
return function random() {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { configBuilder } from "../../mod.ts";
|
||||
const { Config, Value, List, Variants } = configBuilder;
|
||||
|
||||
export const enable = Value.boolean({
|
||||
export const enable = configBuilder.Value.boolean({
|
||||
"name": "Enable",
|
||||
"default": true,
|
||||
"description": "Allow remote RPC requests.",
|
||||
@@ -443,12 +443,12 @@ export const advanced1 = Value.object({
|
||||
spec: advancedSpec1,
|
||||
"value-names": {},
|
||||
});
|
||||
export const config = Config.of({
|
||||
export const configSpec = configBuilder.Config.of({
|
||||
"rpc": rpc,
|
||||
"zmq-enabled": zmqEnabled,
|
||||
"txindex": txindex,
|
||||
"wallet": wallet,
|
||||
"advanced": advanced1,
|
||||
});
|
||||
export const matchConfig = config.validator();
|
||||
export type Config = typeof matchConfig._TYPE;
|
||||
export const matchConfigSpec = configSpec.validator();
|
||||
export type ConfigSpec = typeof matchConfigSpec._TYPE;
|
||||
|
||||
Reference in New Issue
Block a user