@valbuild/core 0.100.0 → 0.101.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,7 @@ import { getValidationBasis, getValidationHash } from "./remote/validationBasis.
41
41
  import { getFileHash, hashToRemoteFileHash } from "./remote/fileHash.js";
42
42
  import { splitRemoteRef } from "./remote/splitRemoteRef.js";
43
43
  import { convertRemoteSource } from "./schema/remote.js";
44
+ import { colorToHex, convertColor, detectColorFormat, formatColor, parseColor } from "./schema/colorFormat.js";
44
45
  export { type SerializedArraySchema, ArraySchema } from "./schema/array.js";
45
46
  export { type SerializedObjectSchema, ObjectSchema } from "./schema/object.js";
46
47
  export { type SerializedRecordSchema, RecordSchema } from "./schema/record.js";
@@ -50,6 +51,8 @@ export { type SerializedBooleanSchema, BooleanSchema } from "./schema/boolean.js
50
51
  export { type SerializedImageSchema, ImageSchema } from "./schema/image.js";
51
52
  export { type SerializedFileSchema, FileSchema } from "./schema/file.js";
52
53
  export { type SerializedDateSchema, DateSchema } from "./schema/date.js";
54
+ export { type SerializedColorSchema, type ColorOptions, ColorSchema, } from "./schema/color.js";
55
+ export { type ColorFormat, type ParsedColor, COLOR_FORMATS, DEFAULT_COLOR_FORMAT, } from "./schema/colorFormat.js";
53
56
  export { type SerializedDateTimeSchema, DateTimeSchema, } from "./schema/datetime.js";
54
57
  export { type SerializedKeyOfSchema, KeyOfSchema } from "./schema/keyOf.js";
55
58
  export { type SerializedRouteSchema, RouteSchema } from "./schema/route.js";
@@ -79,6 +82,13 @@ declare const Internal: {
79
82
  joinModuleFilePathAndModulePath: typeof joinModuleFilePathAndModulePath;
80
83
  nextAppRouter: import("./router.js").ValRouter;
81
84
  externalPageRouter: import("./router.js").ValRouter;
85
+ color: {
86
+ parseColor: typeof parseColor;
87
+ formatColor: typeof formatColor;
88
+ convertColor: typeof convertColor;
89
+ detectColorFormat: typeof detectColorFormat;
90
+ colorToHex: typeof colorToHex;
91
+ };
82
92
  remote: {
83
93
  createRemoteRef: typeof createRemoteRef;
84
94
  getValidationBasis: typeof getValidationBasis;
@@ -13,6 +13,7 @@ import { file } from "./schema/file.js";
13
13
  import { files } from "./schema/files.js";
14
14
  import { date } from "./schema/date.js";
15
15
  import { datetime } from "./schema/datetime.js";
16
+ import { color } from "./schema/color.js";
16
17
  import { route } from "./schema/route.js";
17
18
  import { router } from "./schema/router.js";
18
19
  import { images } from "./schema/images.js";
@@ -173,6 +174,25 @@ export type InitSchema = {
173
174
  *
174
175
  */
175
176
  readonly datetime: typeof datetime;
177
+ /**
178
+ * Define a color.
179
+ *
180
+ * Stored as a CSS color string, so it can be used directly in `style`
181
+ * attributes or set as a CSS custom property.
182
+ *
183
+ * The notation is decided by the `format` option, which defaults to `"hsl"`.
184
+ * Set `alpha: true` to allow transparency.
185
+ *
186
+ * @example
187
+ * const schema = s.color();
188
+ * export default c.define("/example.val.ts", schema, "hsl(217.22 91.22% 59.8%)");
189
+ *
190
+ * @example
191
+ * const schema = s.color({ format: "hex" });
192
+ * export default c.define("/example.val.ts", schema, "#3b82f6");
193
+ *
194
+ */
195
+ readonly color: typeof color;
176
196
  /**
177
197
  * Define a string that references a route path in your application.
178
198
  *
@@ -283,6 +303,7 @@ export declare function initSchema(): {
283
303
  }>, import("./schema/index.js").Schema<string>, Record<string, import("./schema/files.js").FilesEntryMetadata>>;
284
304
  date: (options?: Record<string, never>) => import("./schema/date.js").DateSchema<import("./schema/string.js").RawString>;
285
305
  datetime: (options?: Record<string, never>) => import("./schema/datetime.js").DateTimeSchema<import("./schema/string.js").RawString>;
306
+ color: (options?: import("./schema/color.js").ColorOptions) => import("./schema/color.js").ColorSchema<import("./schema/string.js").RawString>;
286
307
  route: <T extends string>(options?: Record<string, never>) => import("./schema/route.js").RouteSchema<T>;
287
308
  router: typeof router;
288
309
  images: <Accept extends `image/${string}`>(options?: import("./schema/images.js").ImagesOptions<Accept>) => import("./schema/record.js").RecordSchema<import("./schema/object.js").ObjectSchema<{
@@ -0,0 +1,57 @@
1
+ import { CustomValidateFunction, Schema, SchemaAssertResult, SerializedSchema } from "./index.js";
2
+ import { ReifiedRender } from "../render.js";
3
+ import { SourcePath } from "../val/index.js";
4
+ import { ColorFormat } from "./colorFormat.js";
5
+ import { RawString } from "./string.js";
6
+ import { ValidationError, ValidationErrors } from "./validation/ValidationError.js";
7
+ export type { ColorFormat } from "./colorFormat.js";
8
+ export type ColorOptions = {
9
+ /**
10
+ * The CSS notation the color is stored in.
11
+ *
12
+ * Defaults to `"hsl"`.
13
+ *
14
+ * @example
15
+ * "hex" // #3b82f6
16
+ * "rgb" // rgb(59 130 246)
17
+ * "hsl" // hsl(217.22 91.22% 59.8%)
18
+ * "oklch" // oklch(0.6231 0.188 259.81)
19
+ */
20
+ format?: ColorFormat;
21
+ /**
22
+ * Allow an alpha channel (transparency).
23
+ *
24
+ * Defaults to `false`: a color with an alpha channel is a validation error
25
+ * unless this is enabled.
26
+ */
27
+ alpha?: boolean;
28
+ };
29
+ export type SerializedColorSchema = {
30
+ type: "color";
31
+ options?: ColorOptions;
32
+ opt: boolean;
33
+ customValidate?: boolean;
34
+ readonly?: boolean;
35
+ hidden?: boolean;
36
+ description?: string;
37
+ };
38
+ export declare class ColorSchema<Src extends string | null> extends Schema<Src> {
39
+ private readonly options?;
40
+ private readonly opt;
41
+ private readonly customValidateFunctions;
42
+ private readonly isReadonly;
43
+ private readonly isHidden;
44
+ private readonly description?;
45
+ constructor(options?: ColorOptions | undefined, opt?: boolean, customValidateFunctions?: CustomValidateFunction<Src>[], isReadonly?: boolean, isHidden?: boolean, description?: string | undefined);
46
+ describe(description: string | null): ColorSchema<Src>;
47
+ validate(validationFunction: (src: Src) => false | string): ColorSchema<Src>;
48
+ protected executeCustomValidateAt(path: SourcePath, src: Src): ValidationError[];
49
+ protected executeValidate(path: SourcePath, src: Src): ValidationErrors;
50
+ protected executeAssert(path: SourcePath, src: unknown): SchemaAssertResult<Src>;
51
+ nullable(): ColorSchema<Src | null>;
52
+ readonly(): ColorSchema<Src>;
53
+ hidden(): ColorSchema<Src>;
54
+ protected executeSerialize(): SerializedSchema;
55
+ protected executeRender(): ReifiedRender;
56
+ }
57
+ export declare const color: (options?: ColorOptions) => ColorSchema<RawString>;
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Parsing / formatting of the CSS color strings stored by `s.color()`.
3
+ *
4
+ * Colors are stored as plain CSS strings so that they can be used directly in
5
+ * `style` attributes and CSS custom properties. The schema decides which family
6
+ * (`hex`, `rgb`, `hsl` or `oklch`) the string is written in, this module does
7
+ * the conversion between them.
8
+ *
9
+ * All colors are sRGB: `oklch` is only used as an output notation, so any
10
+ * `oklch` value that falls outside the sRGB gamut is clipped when converted
11
+ * back to the other formats.
12
+ */
13
+ export declare const COLOR_FORMATS: readonly ["hex", "rgb", "hsl", "oklch"];
14
+ export type ColorFormat = (typeof COLOR_FORMATS)[number];
15
+ export declare const DEFAULT_COLOR_FORMAT: ColorFormat;
16
+ /**
17
+ * A color, normalized to sRGB.
18
+ *
19
+ * `r`, `g` and `b` are 0-255 (not rounded), `a` is 0-1.
20
+ */
21
+ export type ParsedColor = {
22
+ r: number;
23
+ g: number;
24
+ b: number;
25
+ a: number;
26
+ };
27
+ /**
28
+ * Which color notation a string uses, or `null` if it is not a notation we
29
+ * recognize. This looks at the syntax only: `detectColorFormat` says nothing
30
+ * about whether the value actually parses (`hsl(nope)` is still `"hsl"`).
31
+ */
32
+ export declare function detectColorFormat(value: string): ColorFormat | null;
33
+ /**
34
+ * Parses a CSS color string into sRGB channels.
35
+ *
36
+ * Accepts hex (3, 4, 6 and 8 digits), `rgb()` / `rgba()`, `hsl()` / `hsla()`
37
+ * and `oklch()`, in both the legacy comma and the modern space syntax. Returns
38
+ * `null` if the value is not a color we can parse - notably: named colors
39
+ * (`red`), `color()`, `lab()` and `color-mix()` are not supported.
40
+ */
41
+ export declare function parseColor(value: string): ParsedColor | null;
42
+ export declare function rgbToHsl(r: number, g: number, b: number): {
43
+ h: number;
44
+ s: number;
45
+ l: number;
46
+ };
47
+ export declare function hslToRgb(h: number, s: number, l: number): {
48
+ r: number;
49
+ g: number;
50
+ b: number;
51
+ };
52
+ /**
53
+ * sRGB to Oklch, using Björn Ottosson's Oklab matrices.
54
+ */
55
+ export declare function rgbToOklch(r: number, g: number, b: number): {
56
+ l: number;
57
+ c: number;
58
+ h: number;
59
+ };
60
+ /**
61
+ * Oklch to sRGB. Out of gamut colors are clipped per channel.
62
+ */
63
+ export declare function oklchToRgb(l: number, c: number, h: number): {
64
+ r: number;
65
+ g: number;
66
+ b: number;
67
+ };
68
+ /**
69
+ * Serializes a color to the canonical string of the given format.
70
+ *
71
+ * The modern, space separated CSS syntax is used for the color functions
72
+ * (`rgb(255 0 0 / 0.5)`). Alpha is only included when it is not fully opaque.
73
+ */
74
+ export declare function formatColor(color: ParsedColor, format: ColorFormat): string;
75
+ /**
76
+ * Converts a color string to the canonical string of the given format, or
77
+ * `null` if the input cannot be parsed.
78
+ */
79
+ export declare function convertColor(value: string, format: ColorFormat): string | null;
80
+ /**
81
+ * `#rrggbb`, which is what `<input type="color">` uses as its value.
82
+ */
83
+ export declare function colorToHex(color: ParsedColor): string;
@@ -12,6 +12,7 @@ import { SerializedRecordSchema } from "./record.js";
12
12
  import { SerializedRichTextSchema } from "./richtext.js";
13
13
  import { RawString, SerializedStringSchema } from "./string.js";
14
14
  import { SerializedUnionSchema } from "./union.js";
15
+ import { SerializedColorSchema } from "./color.js";
15
16
  import { SerializedDateSchema } from "./date.js";
16
17
  import { SerializedDateTimeSchema } from "./datetime.js";
17
18
  import { SerializedRouteSchema } from "./route.js";
@@ -19,7 +20,7 @@ import { ValidationError, ValidationErrors } from "./validation/ValidationError.
19
20
  import { FileSource } from "../source/file.js";
20
21
  import { GenericRichTextSourceNode, RichTextSource } from "../source/richtext.js";
21
22
  import { ReifiedRender } from "../render.js";
22
- export type SerializedSchema = SerializedStringSchema | SerializedLiteralSchema | SerializedBooleanSchema | SerializedNumberSchema | SerializedObjectSchema | SerializedArraySchema | SerializedUnionSchema | SerializedRichTextSchema | SerializedRecordSchema | SerializedKeyOfSchema | SerializedFileSchema | SerializedDateSchema | SerializedDateTimeSchema | SerializedRouteSchema | SerializedImageSchema;
23
+ export type SerializedSchema = SerializedStringSchema | SerializedLiteralSchema | SerializedBooleanSchema | SerializedNumberSchema | SerializedObjectSchema | SerializedArraySchema | SerializedUnionSchema | SerializedRichTextSchema | SerializedRecordSchema | SerializedKeyOfSchema | SerializedFileSchema | SerializedDateSchema | SerializedDateTimeSchema | SerializedColorSchema | SerializedRouteSchema | SerializedImageSchema;
23
24
  type Primitives = number | string | boolean | null | FileSource;
24
25
  export type AssertError = {
25
26
  message: string;