@valbuild/core 0.72.2 → 0.72.4

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.
@@ -94,7 +94,7 @@ export declare function initSchema(): {
94
94
  a: boolean;
95
95
  img: boolean;
96
96
  }>;
97
- }>>(options?: O | undefined) => import("./schema/index.js").Schema<import("./index.js").RichTextSource<O>>;
97
+ }>>(options?: O | undefined) => import("./schema/richtext.js").RichTextSchema<O, import("./index.js").RichTextSource<O>>;
98
98
  image: (options?: import("./schema/image.js").ImageOptions | undefined) => import("./schema/index.js").Schema<import("./index.js").ImageSource>;
99
99
  literal: <T_2 extends string>(value: T_2) => import("./schema/literal.js").LiteralSchema<T_2>;
100
100
  keyOf: <Src extends import("./selector/index.js").GenericSelector<import("./source/index.js").SourceObject, undefined> & import("./module.js").ValModuleBrand>(valModule: Src) => import("./schema/index.js").Schema<Src extends import("./selector/index.js").GenericSelector<infer S_2 extends import("./source/index.js").Source, undefined> ? S_2 extends readonly import("./source/index.js").Source[] ? number : S_2 extends import("./source/index.js").SourceObject ? string extends keyof S_2 ? import("./schema/string.js").RawString : keyof S_2 : S_2 extends Record<string, import("./source/index.js").Source> ? import("./schema/string.js").RawString : never : never>;
@@ -2,17 +2,23 @@ import { Schema, SchemaAssertResult, SerializedSchema } from "./index.js";
2
2
  import { RichTextSource, RichTextOptions } from "../source/richtext.js";
3
3
  import { SourcePath } from "../val/index.js";
4
4
  import { ValidationErrors } from "./validation/ValidationError.js";
5
+ type ValidationOptions = {
6
+ maxLength?: number;
7
+ minLength?: number;
8
+ };
5
9
  export type SerializedRichTextSchema = {
6
10
  type: "richtext";
7
11
  opt: boolean;
8
- options?: RichTextOptions;
12
+ options?: RichTextOptions & ValidationOptions;
9
13
  };
10
14
  export declare class RichTextSchema<O extends RichTextOptions, Src extends RichTextSource<O> | null> extends Schema<Src> {
11
- readonly options: O;
15
+ readonly options: O & ValidationOptions;
12
16
  readonly opt: boolean;
13
- constructor(options: O, opt?: boolean);
17
+ constructor(options: O & ValidationOptions, opt?: boolean);
18
+ maxLength(max: number): RichTextSchema<O, Src>;
19
+ minLength(min: number): RichTextSchema<O, Src>;
14
20
  validate(path: SourcePath, src: Src): ValidationErrors;
15
- private recursiveValidate;
21
+ private internalValidate;
16
22
  assert(path: SourcePath, src: unknown): SchemaAssertResult<Src>;
17
23
  private recursiveAssert;
18
24
  nullable(): Schema<Src | null>;
@@ -38,4 +44,5 @@ export declare const richtext: <O extends Partial<{
38
44
  a: boolean;
39
45
  img: boolean;
40
46
  }>;
41
- }>>(options?: O | undefined) => Schema<RichTextSource<O>>;
47
+ }>>(options?: O | undefined) => RichTextSchema<O, RichTextSource<O>>;
48
+ export {};
@@ -28,8 +28,16 @@ export declare class StringSchema<Src extends string | null> extends Schema<Src>
28
28
  readonly opt: boolean;
29
29
  private readonly isRaw;
30
30
  constructor(options?: StringOptions | undefined, opt?: boolean, isRaw?: boolean);
31
+ /**
32
+ * @deprecated Use `minLength` instead
33
+ */
31
34
  min(minLength: number): StringSchema<Src>;
35
+ minLength(minLength: number): StringSchema<Src>;
36
+ /**
37
+ * @deprecated Use `maxLength` instead
38
+ */
32
39
  max(maxLength: number): StringSchema<Src>;
40
+ maxLength(maxLength: number): StringSchema<Src>;
33
41
  regexp(regexp: RegExp): StringSchema<Src>;
34
42
  validate(path: SourcePath, src: Src): ValidationErrors;
35
43
  assert(path: SourcePath, src: unknown): SchemaAssertResult<Src>;
@@ -2101,6 +2101,20 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2101
2101
  }
2102
2102
  _inherits(RichTextSchema, _Schema);
2103
2103
  return _createClass(RichTextSchema, [{
2104
+ key: "maxLength",
2105
+ value: function maxLength(max) {
2106
+ return new RichTextSchema(_objectSpread2(_objectSpread2({}, this.options), {}, {
2107
+ maxLength: max
2108
+ }), this.opt);
2109
+ }
2110
+ }, {
2111
+ key: "minLength",
2112
+ value: function minLength(min) {
2113
+ return new RichTextSchema(_objectSpread2(_objectSpread2({}, this.options), {}, {
2114
+ minLength: min
2115
+ }), this.opt);
2116
+ }
2117
+ }, {
2104
2118
  key: "validate",
2105
2119
  value: function validate(path, src) {
2106
2120
  var assertRes = this.assert(path, src);
@@ -2118,7 +2132,7 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2118
2132
  }]);
2119
2133
  }
2120
2134
  var current = {};
2121
- var typeErrorRes = this.recursiveValidate(path, nodes, current);
2135
+ var typeErrorRes = this.internalValidate(path, nodes, current);
2122
2136
  if (typeErrorRes) {
2123
2137
  return typeErrorRes;
2124
2138
  }
@@ -2128,148 +2142,194 @@ var RichTextSchema = /*#__PURE__*/function (_Schema) {
2128
2142
  return false;
2129
2143
  }
2130
2144
  }, {
2131
- key: "recursiveValidate",
2132
- value: function recursiveValidate(rootPath, nodes, current) {
2133
- var addError = function addError(path, message, typeError) {
2134
- if (!current[path]) {
2135
- current[path] = [];
2136
- }
2137
- current[path].push({
2138
- message: message,
2139
- typeError: typeError
2140
- });
2141
- };
2142
- var _iterator = result._createForOfIteratorHelper(nodes),
2143
- _step;
2144
- try {
2145
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
2146
- var _this$options$block, _this$options$block2, _this$options$block3, _this$options$block4, _this$options$block5, _this$options$block6, _this$options$block7, _this$options$block8, _this$options$block9, _this$options$block10, _this$options$inline, _this$options$inline2;
2147
- var node = _step.value;
2148
- var path = unsafeCreateSourcePath(rootPath, nodes.indexOf(node));
2149
- if (typeof node === "string") {
2150
- continue;
2151
- }
2152
- if (_typeof(node) !== "object") {
2153
- return _defineProperty({}, path, [{
2154
- message: "Expected nodes of type 'object' or 'string', got '".concat(_typeof(node), "'"),
2155
- typeError: true
2156
- }]);
2157
- }
2158
- if (node === null) {
2159
- return _defineProperty({}, path, [{
2160
- message: "Expected nodes of type 'object' or 'string', got 'null'",
2161
- typeError: true
2162
- }]);
2163
- }
2164
- if (!("tag" in node)) {
2165
- return _defineProperty({}, path, [{
2166
- message: "Expected node to either have 'tag' or be of type 'string'",
2167
- typeError: true
2168
- }]);
2169
- }
2170
- if (typeof node.tag !== "string") {
2171
- return _defineProperty({}, path, [{
2172
- message: "Expected 'string', got '".concat(_typeof(node.tag), "'"),
2173
- typeError: true
2174
- }]);
2175
- }
2176
- if (node.tag === "h1" && !((_this$options$block = this.options.block) !== null && _this$options$block !== void 0 && _this$options$block.h1)) {
2177
- addError(path, "'h' block is not valid", false);
2178
- }
2179
- if (node.tag === "h2" && !((_this$options$block2 = this.options.block) !== null && _this$options$block2 !== void 0 && _this$options$block2.h2)) {
2180
- addError(path, "'h2' block is not valid", false);
2181
- }
2182
- if (node.tag === "h3" && !((_this$options$block3 = this.options.block) !== null && _this$options$block3 !== void 0 && _this$options$block3.h3)) {
2183
- addError(path, "'h3' block is not valid", false);
2184
- }
2185
- if (node.tag === "h4" && !((_this$options$block4 = this.options.block) !== null && _this$options$block4 !== void 0 && _this$options$block4.h4)) {
2186
- addError(path, "'h4' block is not valid", false);
2187
- }
2188
- if (node.tag === "h5" && !((_this$options$block5 = this.options.block) !== null && _this$options$block5 !== void 0 && _this$options$block5.h5)) {
2189
- addError(path, "'h5' block is not valid", false);
2190
- }
2191
- if (node.tag === "h6" && !((_this$options$block6 = this.options.block) !== null && _this$options$block6 !== void 0 && _this$options$block6.h6)) {
2192
- addError(path, "'h6' block is not valid", false);
2193
- }
2194
- if (node.tag === "ol" && !((_this$options$block7 = this.options.block) !== null && _this$options$block7 !== void 0 && _this$options$block7.ol)) {
2195
- addError(path, "'ol' block is not valid", false);
2196
- }
2197
- if (node.tag === "ul" && !((_this$options$block8 = this.options.block) !== null && _this$options$block8 !== void 0 && _this$options$block8.ul)) {
2198
- addError(path, "'ul' block is not valid", false);
2199
- }
2200
- if (node.tag === "li" && !((_this$options$block9 = this.options.block) !== null && _this$options$block9 !== void 0 && _this$options$block9.ul) && !((_this$options$block10 = this.options.block) !== null && _this$options$block10 !== void 0 && _this$options$block10.ol)) {
2201
- addError(path, "'li' tag is invalid since neither 'ul' nor 'ol' block is not valid", false);
2202
- }
2203
- if (node.tag === "a" && !((_this$options$inline = this.options.inline) !== null && _this$options$inline !== void 0 && _this$options$inline.a)) {
2204
- addError(path, "'a' inline is not valid", false);
2205
- }
2206
- if (node.tag === "img" && !((_this$options$inline2 = this.options.inline) !== null && _this$options$inline2 !== void 0 && _this$options$inline2.img)) {
2207
- addError(path, "'img' inline is not valid", false);
2208
- }
2209
- if ("styles" in node && node.tag !== "span") {
2210
- return _defineProperty({}, path, [{
2211
- message: "Cannot have styles on '".concat(node.tag, "'. This is only allowed on 'span'"),
2212
- typeError: true
2213
- }]);
2145
+ key: "internalValidate",
2146
+ value: function internalValidate(rootPath, nodes, current) {
2147
+ var _this2 = this;
2148
+ var length = 0;
2149
+ var _recurse = function recurse(rootPath, nodes, current) {
2150
+ var addError = function addError(path, message, typeError) {
2151
+ if (!current[path]) {
2152
+ current[path] = [];
2214
2153
  }
2215
- if ("styles" in node) {
2216
- if (!Array.isArray(node.styles)) {
2154
+ current[path].push({
2155
+ message: message,
2156
+ typeError: typeError
2157
+ });
2158
+ };
2159
+ var _iterator = result._createForOfIteratorHelper(nodes),
2160
+ _step;
2161
+ try {
2162
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
2163
+ var _this2$options$block, _this2$options$block2, _this2$options$block3, _this2$options$block4, _this2$options$block5, _this2$options$block6, _this2$options$block7, _this2$options$block8, _this2$options$block9, _this2$options$block10, _this2$options$inline, _this2$options$inline2;
2164
+ var node = _step.value;
2165
+ var path = unsafeCreateSourcePath(rootPath, nodes.indexOf(node));
2166
+ if (typeof node === "string") {
2167
+ length += node.length;
2168
+ continue;
2169
+ }
2170
+ if (_typeof(node) !== "object") {
2217
2171
  return _defineProperty({}, path, [{
2218
- message: "Expected 'array', got '".concat(_typeof(node.styles), "'"),
2172
+ message: "Expected nodes of type 'object' or 'string', got '".concat(_typeof(node), "'"),
2219
2173
  typeError: true
2220
2174
  }]);
2221
2175
  }
2222
- var stylesPath = unsafeCreateSourcePath(path, "styles");
2223
- for (var i = 0; i < node.styles.length; i++) {
2224
- var _this$options$style, _this$options$style2, _this$options$style3;
2225
- var style = node.styles[i];
2226
- var currentStylePath = unsafeCreateSourcePath(stylesPath, i);
2227
- if (typeof style !== "string") {
2228
- return _defineProperty({}, currentStylePath, [{
2229
- message: "Expected 'string', got '".concat(_typeof(style), "'"),
2230
- typeError: true
2231
- }]);
2232
- }
2233
- if (style === "bold" && !((_this$options$style = this.options.style) !== null && _this$options$style !== void 0 && _this$options$style.bold)) {
2234
- addError(currentStylePath, "Style 'bold' is not valid", false);
2235
- }
2236
- if (style === "italic" && !((_this$options$style2 = this.options.style) !== null && _this$options$style2 !== void 0 && _this$options$style2.italic)) {
2237
- addError(currentStylePath, "Style 'italic' is not valid", false);
2238
- }
2239
- if (style === "lineThrough" && !((_this$options$style3 = this.options.style) !== null && _this$options$style3 !== void 0 && _this$options$style3.lineThrough)) {
2240
- addError(currentStylePath, "Style 'lineThrough' is not valid", false);
2241
- }
2176
+ if (node === null) {
2177
+ return _defineProperty({}, path, [{
2178
+ message: "Expected nodes of type 'object' or 'string', got 'null'",
2179
+ typeError: true
2180
+ }]);
2242
2181
  }
2243
- }
2244
- if ("children" in node) {
2245
- if (!Array.isArray(node.children)) {
2182
+ if (!("tag" in node)) {
2183
+ return _defineProperty({}, path, [{
2184
+ message: "Expected node to either have 'tag' or be of type 'string'",
2185
+ typeError: true
2186
+ }]);
2187
+ }
2188
+ if (typeof node.tag !== "string") {
2189
+ return _defineProperty({}, path, [{
2190
+ message: "Expected 'string', got '".concat(_typeof(node.tag), "'"),
2191
+ typeError: true
2192
+ }]);
2193
+ }
2194
+ if (node.tag === "h1" && !((_this2$options$block = _this2.options.block) !== null && _this2$options$block !== void 0 && _this2$options$block.h1)) {
2195
+ addError(path, "'h' block is not valid", false);
2196
+ }
2197
+ if (node.tag === "h2" && !((_this2$options$block2 = _this2.options.block) !== null && _this2$options$block2 !== void 0 && _this2$options$block2.h2)) {
2198
+ addError(path, "'h2' block is not valid", false);
2199
+ }
2200
+ if (node.tag === "h3" && !((_this2$options$block3 = _this2.options.block) !== null && _this2$options$block3 !== void 0 && _this2$options$block3.h3)) {
2201
+ addError(path, "'h3' block is not valid", false);
2202
+ }
2203
+ if (node.tag === "h4" && !((_this2$options$block4 = _this2.options.block) !== null && _this2$options$block4 !== void 0 && _this2$options$block4.h4)) {
2204
+ addError(path, "'h4' block is not valid", false);
2205
+ }
2206
+ if (node.tag === "h5" && !((_this2$options$block5 = _this2.options.block) !== null && _this2$options$block5 !== void 0 && _this2$options$block5.h5)) {
2207
+ addError(path, "'h5' block is not valid", false);
2208
+ }
2209
+ if (node.tag === "h6" && !((_this2$options$block6 = _this2.options.block) !== null && _this2$options$block6 !== void 0 && _this2$options$block6.h6)) {
2210
+ addError(path, "'h6' block is not valid", false);
2211
+ }
2212
+ if (node.tag === "ol" && !((_this2$options$block7 = _this2.options.block) !== null && _this2$options$block7 !== void 0 && _this2$options$block7.ol)) {
2213
+ addError(path, "'ol' block is not valid", false);
2214
+ }
2215
+ if (node.tag === "ul" && !((_this2$options$block8 = _this2.options.block) !== null && _this2$options$block8 !== void 0 && _this2$options$block8.ul)) {
2216
+ addError(path, "'ul' block is not valid", false);
2217
+ }
2218
+ if (node.tag === "li" && !((_this2$options$block9 = _this2.options.block) !== null && _this2$options$block9 !== void 0 && _this2$options$block9.ul) && !((_this2$options$block10 = _this2.options.block) !== null && _this2$options$block10 !== void 0 && _this2$options$block10.ol)) {
2219
+ addError(path, "'li' tag is invalid since neither 'ul' nor 'ol' block is not valid", false);
2220
+ }
2221
+ if (node.tag === "a" && !((_this2$options$inline = _this2.options.inline) !== null && _this2$options$inline !== void 0 && _this2$options$inline.a)) {
2222
+ addError(path, "'a' inline is not valid", false);
2223
+ }
2224
+ if (node.tag === "img" && !((_this2$options$inline2 = _this2.options.inline) !== null && _this2$options$inline2 !== void 0 && _this2$options$inline2.img)) {
2225
+ addError(path, "'img' inline is not valid", false);
2226
+ }
2227
+ if ("styles" in node && node.tag !== "span") {
2246
2228
  return _defineProperty({}, path, [{
2247
- message: "Expected 'array', got '".concat(_typeof(node.children), "'"),
2229
+ message: "Cannot have styles on '".concat(node.tag, "'. This is only allowed on 'span'"),
2248
2230
  typeError: true
2249
2231
  }]);
2250
2232
  }
2251
- var children = node.children;
2252
- for (var _i = 0; _i < children.length; _i++) {
2253
- var child = children[_i];
2254
- if (_typeof(child) === "object") {
2255
- var childPath = unsafeCreateSourcePath(path, "children");
2256
- var res = this.recursiveValidate(childPath, [child], current);
2257
- if (res) {
2258
- return res;
2233
+ if ("styles" in node) {
2234
+ if (!Array.isArray(node.styles)) {
2235
+ return _defineProperty({}, path, [{
2236
+ message: "Expected 'array', got '".concat(_typeof(node.styles), "'"),
2237
+ typeError: true
2238
+ }]);
2239
+ }
2240
+ var stylesPath = unsafeCreateSourcePath(path, "styles");
2241
+ for (var i = 0; i < node.styles.length; i++) {
2242
+ var _this2$options$style, _this2$options$style2, _this2$options$style3;
2243
+ var style = node.styles[i];
2244
+ var currentStylePath = unsafeCreateSourcePath(stylesPath, i);
2245
+ if (typeof style !== "string") {
2246
+ return _defineProperty({}, currentStylePath, [{
2247
+ message: "Expected 'string', got '".concat(_typeof(style), "'"),
2248
+ typeError: true
2249
+ }]);
2250
+ }
2251
+ if (style === "bold" && !((_this2$options$style = _this2.options.style) !== null && _this2$options$style !== void 0 && _this2$options$style.bold)) {
2252
+ addError(currentStylePath, "Style 'bold' is not valid", false);
2253
+ }
2254
+ if (style === "italic" && !((_this2$options$style2 = _this2.options.style) !== null && _this2$options$style2 !== void 0 && _this2$options$style2.italic)) {
2255
+ addError(currentStylePath, "Style 'italic' is not valid", false);
2259
2256
  }
2260
- } else if (typeof child !== "string") {
2257
+ if (style === "lineThrough" && !((_this2$options$style3 = _this2.options.style) !== null && _this2$options$style3 !== void 0 && _this2$options$style3.lineThrough)) {
2258
+ addError(currentStylePath, "Style 'lineThrough' is not valid", false);
2259
+ }
2260
+ }
2261
+ }
2262
+ if ("children" in node) {
2263
+ if (!Array.isArray(node.children)) {
2261
2264
  return _defineProperty({}, path, [{
2262
- message: "Expected 'object' or 'string', got '".concat(_typeof(child), "'"),
2265
+ message: "Expected 'array', got '".concat(_typeof(node.children), "'"),
2263
2266
  typeError: true
2264
2267
  }]);
2265
2268
  }
2269
+ var children = node.children;
2270
+ for (var _i = 0; _i < children.length; _i++) {
2271
+ var child = children[_i];
2272
+ if (_typeof(child) === "object") {
2273
+ var childPath = unsafeCreateSourcePath(path, "children");
2274
+ var res = _recurse(childPath, [child], current);
2275
+ if (res) {
2276
+ return res;
2277
+ }
2278
+ } else if (typeof child === "string") {
2279
+ length += child.length;
2280
+ continue;
2281
+ } else {
2282
+ return _defineProperty({}, path, [{
2283
+ message: "Expected 'object' or 'string', got '".concat(_typeof(child), "'"),
2284
+ typeError: true
2285
+ }]);
2286
+ }
2287
+ }
2266
2288
  }
2267
2289
  }
2290
+ } catch (err) {
2291
+ _iterator.e(err);
2292
+ } finally {
2293
+ _iterator.f();
2268
2294
  }
2269
- } catch (err) {
2270
- _iterator.e(err);
2271
- } finally {
2272
- _iterator.f();
2295
+ return false;
2296
+ };
2297
+ var results = _recurse(rootPath, nodes, current);
2298
+ var lengthErrors = [];
2299
+ if (this.options.maxLength && length > this.options.maxLength) {
2300
+ lengthErrors.push({
2301
+ message: "Maximum length of ".concat(this.options.maxLength, " exceeded (current length: ").concat(length, ")"),
2302
+ typeError: false
2303
+ });
2304
+ }
2305
+ if (this.options.minLength && length < this.options.minLength) {
2306
+ lengthErrors.push({
2307
+ message: "Minimum length of ".concat(this.options.minLength, " not met (current length: ").concat(length, ")"),
2308
+ typeError: false
2309
+ });
2310
+ }
2311
+ if (results) {
2312
+ if (lengthErrors.length > 0) {
2313
+ var _results$rootPath;
2314
+ if (!results[rootPath]) {
2315
+ results[rootPath] = [];
2316
+ }
2317
+ (_results$rootPath = results[rootPath]).push.apply(_results$rootPath, lengthErrors);
2318
+ }
2319
+ return results;
2320
+ }
2321
+ if (Object.keys(current).length > 0) {
2322
+ if (lengthErrors.length > 0) {
2323
+ var _current$rootPath;
2324
+ if (!current[rootPath]) {
2325
+ current[rootPath] = [];
2326
+ }
2327
+ (_current$rootPath = current[rootPath]).push.apply(_current$rootPath, lengthErrors);
2328
+ }
2329
+ return current;
2330
+ }
2331
+ if (lengthErrors.length > 0) {
2332
+ return _defineProperty({}, rootPath, lengthErrors);
2273
2333
  }
2274
2334
  return false;
2275
2335
  }
@@ -3085,19 +3145,37 @@ var StringSchema = /*#__PURE__*/function (_Schema) {
3085
3145
  _this.isRaw = isRaw;
3086
3146
  return _this;
3087
3147
  }
3148
+
3149
+ /**
3150
+ * @deprecated Use `minLength` instead
3151
+ */
3088
3152
  _inherits(StringSchema, _Schema);
3089
3153
  return _createClass(StringSchema, [{
3090
3154
  key: "min",
3091
3155
  value: function min(minLength) {
3156
+ return this.minLength(minLength);
3157
+ }
3158
+ }, {
3159
+ key: "minLength",
3160
+ value: function minLength(_minLength) {
3092
3161
  return new StringSchema(_objectSpread2(_objectSpread2({}, this.options), {}, {
3093
- minLength: minLength
3162
+ minLength: _minLength
3094
3163
  }), this.opt, this.isRaw);
3095
3164
  }
3165
+
3166
+ /**
3167
+ * @deprecated Use `maxLength` instead
3168
+ */
3096
3169
  }, {
3097
3170
  key: "max",
3098
3171
  value: function max(maxLength) {
3172
+ return this.maxLength(maxLength);
3173
+ }
3174
+ }, {
3175
+ key: "maxLength",
3176
+ value: function maxLength(_maxLength) {
3099
3177
  return new StringSchema(_objectSpread2(_objectSpread2({}, this.options), {}, {
3100
- maxLength: maxLength
3178
+ maxLength: _maxLength
3101
3179
  }), this.opt, this.isRaw);
3102
3180
  }
3103
3181
  }, {