@venizia/ignis-filter 0.4.7-1 → 0.4.7-3
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.
- package/README.md +1 -0
- package/dist/esm/common/index.d.ts +1 -0
- package/dist/esm/common/index.js +1 -0
- package/dist/esm/common/index.js.map +1 -1
- package/dist/esm/common/keys.d.ts +22 -0
- package/dist/esm/common/keys.js +21 -0
- package/dist/esm/common/keys.js.map +1 -0
- package/dist/esm/schemas/builder.d.ts +7 -3
- package/dist/esm/schemas/builder.js +19 -2
- package/dist/esm/schemas/builder.js.map +1 -1
- package/dist/esm/schemas/index.d.ts +3 -3
- package/dist/esm/schemas/where-depth.d.ts +15 -0
- package/dist/esm/schemas/where-depth.js +33 -0
- package/dist/esm/schemas/where-depth.js.map +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -72,6 +72,7 @@ nested filter.
|
|
|
72
72
|
| Pattern | `like`, `nlike`, `ilike`, `nilike`, `regexp`, `iregexp` |
|
|
73
73
|
| Null | `is`, `isn` |
|
|
74
74
|
| List and range | `in`, `inq`, `nin`, `between`, `notBetween` |
|
|
75
|
+
| Subquery | `inSql`, `ninSql` - the operand is a Drizzle SQL object (`{ getSQL() }`) built in code, never a value from a request; the relational connectors run it |
|
|
75
76
|
| Array columns | `contains`, `containedBy`, `overlaps` |
|
|
76
77
|
| Other | `exists`, `notExists`, `not`, `and`, `or` |
|
|
77
78
|
|
package/dist/esm/common/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC","sourcesContent":["export * from './operators';\nexport * from './types';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC","sourcesContent":["export * from './operators';\nexport * from './types';\nexport * from './keys';\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a key of a `where`, or the field of an `order` entry, names a column. One reader for the query that builds the SQL and for
|
|
3
|
+
* everything that must judge the same key (a request guard), so they cannot disagree about which column a key reaches.
|
|
4
|
+
*/
|
|
5
|
+
export declare class FilterKeys {
|
|
6
|
+
/** A key with a `.` or a `[` is a JSON path into a column (`meta.tags[0]`), not a column name. */
|
|
7
|
+
static isJsonPath(opts: {
|
|
8
|
+
key: string;
|
|
9
|
+
}): boolean;
|
|
10
|
+
/** Splits a JSON path on dots and brackets, dropping the empty parts: `[meta].pin`, `.meta.pin` and `meta.pin` all read `meta`, then `pin`. */
|
|
11
|
+
static parseJsonPath(opts: {
|
|
12
|
+
key: string;
|
|
13
|
+
}): {
|
|
14
|
+
columnName: string;
|
|
15
|
+
path: string[];
|
|
16
|
+
};
|
|
17
|
+
/** The column a key reaches: the first part of a JSON path, or the key itself. */
|
|
18
|
+
static columnOf(opts: {
|
|
19
|
+
key: string;
|
|
20
|
+
}): string;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a key of a `where`, or the field of an `order` entry, names a column. One reader for the query that builds the SQL and for
|
|
3
|
+
* everything that must judge the same key (a request guard), so they cannot disagree about which column a key reaches.
|
|
4
|
+
*/
|
|
5
|
+
export class FilterKeys {
|
|
6
|
+
/** A key with a `.` or a `[` is a JSON path into a column (`meta.tags[0]`), not a column name. */
|
|
7
|
+
static isJsonPath(opts) {
|
|
8
|
+
return opts.key.includes('.') || opts.key.includes('[');
|
|
9
|
+
}
|
|
10
|
+
/** Splits a JSON path on dots and brackets, dropping the empty parts: `[meta].pin`, `.meta.pin` and `meta.pin` all read `meta`, then `pin`. */
|
|
11
|
+
static parseJsonPath(opts) {
|
|
12
|
+
const parts = opts.key.split(/[.[\]]+/).filter(Boolean);
|
|
13
|
+
const [columnName = opts.key, ...path] = parts;
|
|
14
|
+
return { columnName, path };
|
|
15
|
+
}
|
|
16
|
+
/** The column a key reaches: the first part of a JSON path, or the key itself. */
|
|
17
|
+
static columnOf(opts) {
|
|
18
|
+
return FilterKeys.isJsonPath(opts) ? FilterKeys.parseJsonPath(opts).columnName : opts.key;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../src/common/keys.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,UAAU;IACrB,kGAAkG;IAClG,MAAM,CAAC,UAAU,CAAC,IAAqB;QACrC,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC1D,CAAC;IAED,+IAA+I;IAC/I,MAAM,CAAC,aAAa,CAAC,IAAqB;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxD,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,KAAK,CAAC;QAE/C,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC;IAC9B,CAAC;IAED,kFAAkF;IAClF,MAAM,CAAC,QAAQ,CAAC,IAAqB;QACnC,OAAO,UAAU,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;IAC5F,CAAC;CACF","sourcesContent":["/**\n * How a key of a `where`, or the field of an `order` entry, names a column. One reader for the query that builds the SQL and for\n * everything that must judge the same key (a request guard), so they cannot disagree about which column a key reaches.\n */\nexport class FilterKeys {\n /** A key with a `.` or a `[` is a JSON path into a column (`meta.tags[0]`), not a column name. */\n static isJsonPath(opts: { key: string }): boolean {\n return opts.key.includes('.') || opts.key.includes('[');\n }\n\n /** Splits a JSON path on dots and brackets, dropping the empty parts: `[meta].pin`, `.meta.pin` and `meta.pin` all read `meta`, then `pin`. */\n static parseJsonPath(opts: { key: string }): { columnName: string; path: string[] } {\n const parts = opts.key.split(/[.[\\]]+/).filter(Boolean);\n const [columnName = opts.key, ...path] = parts;\n\n return { columnName, path };\n }\n\n /** The column a key reaches: the first part of a JSON path, or the key itself. */\n static columnOf(opts: { key: string }): string {\n return FilterKeys.isJsonPath(opts) ? FilterKeys.parseJsonPath(opts).columnName : opts.key;\n }\n}\n"]}
|
|
@@ -7,12 +7,16 @@ import type { TSchemaDecorator } from './common/index.js';
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class QuerySchemaFactory {
|
|
9
9
|
private static readonly identityDecorator;
|
|
10
|
+
/** `limits.whereDepth` is the deepest `and` / `or` nesting a `where` may have (default 20); a deeper one is a validation issue. */
|
|
10
11
|
static build(opts?: {
|
|
11
12
|
decorate?: TSchemaDecorator;
|
|
13
|
+
limits?: {
|
|
14
|
+
whereDepth?: number;
|
|
15
|
+
};
|
|
12
16
|
}): {
|
|
13
17
|
FieldsSchema: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodBoolean>, z.ZodArray<z.ZodString>]>>;
|
|
14
18
|
FilterSchema: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
|
|
15
|
-
where: z.ZodOptional<z.ZodUnion<readonly [z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown
|
|
19
|
+
where: z.ZodOptional<z.ZodUnion<readonly [z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>, z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>>]>>;
|
|
16
20
|
fields: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodBoolean>, z.ZodArray<z.ZodString>]>>;
|
|
17
21
|
include: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
18
22
|
relation: z.ZodString;
|
|
@@ -23,7 +27,7 @@ export declare class QuerySchemaFactory {
|
|
|
23
27
|
offset: z.ZodOptional<z.ZodNumber>;
|
|
24
28
|
skip: z.ZodOptional<z.ZodNumber>;
|
|
25
29
|
}, z.core.$strip>, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>, z.ZodObject<{
|
|
26
|
-
where: z.ZodOptional<z.ZodUnion<readonly [z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown
|
|
30
|
+
where: z.ZodOptional<z.ZodUnion<readonly [z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>, z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>>]>>;
|
|
27
31
|
fields: z.ZodOptional<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodBoolean>, z.ZodArray<z.ZodString>]>>;
|
|
28
32
|
include: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
29
33
|
relation: z.ZodString;
|
|
@@ -42,7 +46,7 @@ export declare class QuerySchemaFactory {
|
|
|
42
46
|
OffsetSchema: z.ZodOptional<z.ZodNumber>;
|
|
43
47
|
OrderBySchema: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
44
48
|
SkipSchema: z.ZodOptional<z.ZodNumber>;
|
|
45
|
-
WhereSchema: z.ZodUnion<readonly [z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown
|
|
49
|
+
WhereSchema: z.ZodUnion<readonly [z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<any, string>>, z.ZodPipe<z.ZodPipe<z.ZodAny, z.ZodTransform<any, any>>, z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>>]>;
|
|
46
50
|
};
|
|
47
51
|
}
|
|
48
52
|
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { DEFAULT_MAX_WHERE_DEPTH, WhereDepthGuard } from './where-depth.js';
|
|
2
3
|
/**
|
|
3
4
|
* The schemas are built by a factory rather than declared at module scope because documentation metadata has to be injected. `.openapi()` returns a NEW schema instead of mutating, and three of these decorations sit on inner nodes of a nested tree, so a consumer cannot annotate them after the tree is composed - the outer node would still hold the undecorated children.
|
|
4
5
|
*
|
|
@@ -6,8 +7,10 @@ import { z } from 'zod';
|
|
|
6
7
|
*/
|
|
7
8
|
export class QuerySchemaFactory {
|
|
8
9
|
static { this.identityDecorator = (schema) => schema; }
|
|
10
|
+
/** `limits.whereDepth` is the deepest `and` / `or` nesting a `where` may have (default 20); a deeper one is a validation issue. */
|
|
9
11
|
static build(opts) {
|
|
10
12
|
const decorate = opts?.decorate ?? QuerySchemaFactory.identityDecorator;
|
|
13
|
+
const maxWhereDepth = opts?.limits?.whereDepth ?? DEFAULT_MAX_WHERE_DEPTH;
|
|
11
14
|
/**
|
|
12
15
|
* `int().nonnegative()`, not a bare `number()`. A NEGATIVE limit is not a smaller page - Drizzle
|
|
13
16
|
* renders the LIMIT clause only when the value is `>= 0`, so `filter={"limit":-1}` emitted a
|
|
@@ -49,8 +52,22 @@ export class QuerySchemaFactory {
|
|
|
49
52
|
and: z.array(RecursiveWhereSchema).optional(),
|
|
50
53
|
or: z.array(RecursiveWhereSchema).optional(),
|
|
51
54
|
})));
|
|
55
|
+
/** The depth is checked on the raw value first: the recursive schema below costs the square of it. */
|
|
56
|
+
const BoundedWhereSchema = z
|
|
57
|
+
.any()
|
|
58
|
+
.transform((value, ctx) => {
|
|
59
|
+
if (WhereDepthGuard.exceeds({ value, max: maxWhereDepth })) {
|
|
60
|
+
ctx.addIssue({
|
|
61
|
+
code: 'custom',
|
|
62
|
+
message: `where is nested deeper than ${maxWhereDepth} levels`,
|
|
63
|
+
});
|
|
64
|
+
return z.NEVER;
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
})
|
|
68
|
+
.pipe(RecursiveWhereSchema);
|
|
52
69
|
const WhereSchema = decorate(z.union([
|
|
53
|
-
|
|
70
|
+
BoundedWhereSchema,
|
|
54
71
|
z
|
|
55
72
|
.string()
|
|
56
73
|
// A caught parse, never a bare `JSON.parse`. zod lets a thrown SyntaxError escape
|
|
@@ -68,7 +85,7 @@ export class QuerySchemaFactory {
|
|
|
68
85
|
return z.NEVER;
|
|
69
86
|
}
|
|
70
87
|
})
|
|
71
|
-
.pipe(
|
|
88
|
+
.pipe(BoundedWhereSchema),
|
|
72
89
|
]), { type: 'object', description: 'Query conditions for selecting data.' });
|
|
73
90
|
/** Zod schema for including related entities in queries with optional nested filtering. */
|
|
74
91
|
const InclusionSchema = decorate(z
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/schemas/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;aACL,sBAAiB,GAAqB,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC;IAEjF,MAAM,CAAC,KAAK,CAAC,IAAsC;QACjD,MA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I,CAAC,SAAS,CAAC;oBACb,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE;oBAC5D,IAAI,EAAE,EAAE;oBACR,KAAK,EAAE,EAAE;iBACV,CAAC;gBACF,IAAI,CAAC,SAAS,CAAC;oBACb,KAAK,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,YAAY,EAAE,EAAE,CAAC,EAAE;iBAC1E,CAAC;aACH;SACF,CACF,CAAC;QAEF,OAAO;YACL,YAAY;YACZ,YAAY;YACZ,eAAe;YACf,WAAW;YACX,YAAY;YACZ,aAAa;YACb,UAAU;YACV,WAAW;SACZ,CAAC;IACJ,CAAC","sourcesContent":["import { z } from 'zod';\n\nimport type { TSchemaDecorator } from './common';\n\n/**\n * The schemas are built by a factory rather than declared at module scope because documentation metadata has to be injected. `.openapi()` returns a NEW schema instead of mutating, and three of these decorations sit on inner nodes of a nested tree, so a consumer cannot annotate them after the tree is composed - the outer node would still hold the undecorated children.\n *\n * Built with plain `zod`, never `@hono/zod-openapi`: the OpenAPI layer pulls in Hono, and these schemas must stay usable in a browser. `@hono/zod-openapi` declares `zod` as a peer, so it patches the same prototype - a server that imports it can decorate these schemas even though they were built without it.\n */\nexport class QuerySchemaFactory {\n private static readonly identityDecorator: TSchemaDecorator = (schema) => schema;\n\n static build(opts?: { decorate?: TSchemaDecorator }) {\n const decorate = opts?.decorate ?? QuerySchemaFactory.identityDecorator;\n\n /**\n * `int().nonnegative()`, not a bare `number()`. A NEGATIVE limit is not a smaller page - Drizzle\n * renders the LIMIT clause only when the value is `>= 0`, so `filter={\"limit\":-1}` emitted a\n * `select` with no LIMIT at all and returned the whole table. A fractional limit reaches the\n * driver as `LIMIT 1.5` and errors there instead of here.\n */\n const LimitSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Maximum number of items to return. Defaults to 10 for top-level list queries.',\n examples: [1, 2, 3],\n });\n\n const OffsetSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Number of items to offset for pagination.',\n examples: [1, 2, 3],\n });\n\n const SkipSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Number of items to skip for pagination.',\n examples: [1, 2, 3],\n });\n\n const OrderBySchema = decorate(z.array(z.string()).optional(), {\n description:\n \"Sorting order for results. Supports regular columns ('fieldName ASC') and JSON/JSONB paths ('metadata.field DESC', 'data.nested[0].value ASC').\",\n examples: [\n 'id DESC',\n 'createdAt ASC',\n 'metadata.priority DESC',\n 'data.nested.value ASC',\n 'items[0].score DESC',\n ],\n });\n\n const FieldsSchema = decorate(\n z.record(z.string(), z.boolean()).or(z.array(z.string())).optional(),\n {\n description:\n 'Fields selection - either an array of field names to include, or an object with field names as keys and boolean values (true to include, false to exclude)',\n examples: [\n JSON.stringify(['id', 'name', 'email']),\n JSON.stringify({ id: true, name: true }),\n JSON.stringify({ id: true, name: true, email: true, fullName: false }),\n ],\n },\n );\n\n /** @internal Recursive schema for where clause validation with nested AND/OR. */\n const RecursiveWhereSchema: z.ZodType<any> = z.lazy(() =>\n z.record(z.string(), z.any()).and(\n z.object({\n and: z.array(RecursiveWhereSchema).optional(),\n or: z.array(RecursiveWhereSchema).optional(),\n }),\n ),\n );\n\n const WhereSchema = decorate(\n z.union([\n RecursiveWhereSchema,\n z\n .string()\n // A caught parse, never a bare `JSON.parse`. zod lets a thrown SyntaxError escape\n // `safeParse`, so the controller's validation hook never ran and `?where=not-json` surfaced\n // as a 500 with a stack - for what is simply a malformed query string.\n .transform((val, ctx) => {\n if (!val) {\n return undefined;\n }\n\n try {\n return JSON.parse(val);\n } catch {\n ctx.addIssue({ code: 'custom', message: 'where must be valid JSON' });\n\n return z.NEVER;\n }\n })\n .pipe(RecursiveWhereSchema),\n ]),\n { type: 'object', description: 'Query conditions for selecting data.' },\n );\n\n /** Zod schema for including related entities in queries with optional nested filtering. */\n const InclusionSchema = decorate(\n z\n .array(\n z.object({\n relation: decorate(z.string(), { description: 'Model relation name' }),\n scope: decorate(\n z\n .lazy((): z.ZodType<any> => FilterSchema) // eslint-disable-line @typescript-eslint/no-use-before-define\n .optional(),\n { description: 'Model relation filter' },\n ),\n // `shouldSkipDefaultFilter` is deliberately NOT on the wire schema. It stays on the\n // internal `TInclusion` type, so server-side callers keep it, but a client could\n // otherwise send `include[].shouldSkipDefaultFilter` and the relation would be emitted\n // with no where clause at all - erasing the `@model` `settings.query.filter` that implements\n // soft-delete and the static visibility scopes.\n }),\n )\n .optional(),\n {\n description: 'Define related models to include in the response.',\n examples: [\n JSON.stringify({ include: [{ relation: 'posts' }] }),\n JSON.stringify({ include: [{ relation: 'posts', scope: { limit: 5 } }] }),\n ],\n },\n );\n\n /** @internal Filter schema object definition. */\n const InternalFilterSchema = z.object({\n where: WhereSchema.optional(),\n fields: FieldsSchema,\n include: InclusionSchema,\n order: OrderBySchema,\n limit: LimitSchema,\n offset: OffsetSchema,\n skip: SkipSchema,\n });\n\n /** Comprehensive Zod schema for repository query filtering. Supports object and JSON string formats. */\n const FilterSchema = decorate(\n z\n .union([\n InternalFilterSchema,\n z\n .string()\n // Same reason as `where` above: a malformed filter is the caller's mistake, so it has to\n // become a validation issue rather than an uncaught throw.\n .transform((val, ctx) => {\n if (!val) {\n return {};\n }\n\n try {\n return JSON.parse(val);\n } catch {\n ctx.addIssue({ code: 'custom', message: 'filter must be valid JSON' });\n\n return z.NEVER;\n }\n })\n .pipe(InternalFilterSchema),\n ])\n .optional(),\n {\n type: 'object',\n description:\n 'A comprehensive filter object for querying data, including conditions, field selection, relations, pagination, and sorting.',\n examples: [\n JSON.stringify({ where: { name: 'John Doe' }, limit: 10 }),\n JSON.stringify({\n fields: { id: true, name: true, email: true },\n order: ['createdAt DESC'],\n }),\n JSON.stringify({ include: [{ relation: 'posts', scope: { limit: 5 } }] }),\n JSON.stringify({\n where: { or: [{ status: 'active' }, { isPublished: true }] },\n skip: 20,\n limit: 10,\n }),\n JSON.stringify({\n where: { and: [{ role: 'admin' }, { createdAt: { gte: 'YYYY-MM-DD' } }] },\n }),\n ],\n },\n );\n\n return {\n FieldsSchema,\n FilterSchema,\n InclusionSchema,\n LimitSchema,\n OffsetSchema,\n OrderBySchema,\n SkipSchema,\n WhereSchema,\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"builder.js","sourceRoot":"","sources":["../../../src/schemas/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,uBAAuB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEzE;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;aACL,sBAAiB,GAAqB,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC;IAEjF,mIAAmI;IACnI,MAAM,CAAC,KAAK,CAAC,IAAwE;QACnF,MAAM,QAAQ,GAAG,IAAI,EAAE,QAAQ,IAAI,kBAAkB,CAAC,iBAAiB,CAAC;QACxE,MAAM,aAAa,GAAG,IAAI,EAAE,MAAM,EAAE,UAAU,IAAI,uBAAuB,CAAC;QAE1E;;;;;WAKG;QACH,MAAM,WAAW,GAAG,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,EAAE;YACtE,WAAW,EAAE,+EAA+E;YAC5F,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;SACpB,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,EAAE;YACvE,WAAW,EAAE,2CAA2C;YACxD,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;SACpB,CAAC,CAAC;QAEH,MAAM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AE,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE;oBAC5D,IAAI,EAAE,EAAE;oBACR,KAAK,EAAE,EAAE;iBACV,CAAC;gBACF,IAAI,CAAC,SAAS,CAAC;oBACb,KAAK,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,YAAY,EAAE,EAAE,CAAC,EAAE;iBAC1E,CAAC;aACH;SACF,CACF,CAAC;QAEF,OAAO;YACL,YAAY;YACZ,YAAY;YACZ,eAAe;YACf,WAAW;YACX,YAAY;YACZ,aAAa;YACb,UAAU;YACV,WAAW;SACZ,CAAC;IACJ,CAAC","sourcesContent":["import { z } from 'zod';\n\nimport type { TSchemaDecorator } from './common';\nimport { DEFAULT_MAX_WHERE_DEPTH, WhereDepthGuard } from './where-depth';\n\n/**\n * The schemas are built by a factory rather than declared at module scope because documentation metadata has to be injected. `.openapi()` returns a NEW schema instead of mutating, and three of these decorations sit on inner nodes of a nested tree, so a consumer cannot annotate them after the tree is composed - the outer node would still hold the undecorated children.\n *\n * Built with plain `zod`, never `@hono/zod-openapi`: the OpenAPI layer pulls in Hono, and these schemas must stay usable in a browser. `@hono/zod-openapi` declares `zod` as a peer, so it patches the same prototype - a server that imports it can decorate these schemas even though they were built without it.\n */\nexport class QuerySchemaFactory {\n private static readonly identityDecorator: TSchemaDecorator = (schema) => schema;\n\n /** `limits.whereDepth` is the deepest `and` / `or` nesting a `where` may have (default 20); a deeper one is a validation issue. */\n static build(opts?: { decorate?: TSchemaDecorator; limits?: { whereDepth?: number } }) {\n const decorate = opts?.decorate ?? QuerySchemaFactory.identityDecorator;\n const maxWhereDepth = opts?.limits?.whereDepth ?? DEFAULT_MAX_WHERE_DEPTH;\n\n /**\n * `int().nonnegative()`, not a bare `number()`. A NEGATIVE limit is not a smaller page - Drizzle\n * renders the LIMIT clause only when the value is `>= 0`, so `filter={\"limit\":-1}` emitted a\n * `select` with no LIMIT at all and returned the whole table. A fractional limit reaches the\n * driver as `LIMIT 1.5` and errors there instead of here.\n */\n const LimitSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Maximum number of items to return. Defaults to 10 for top-level list queries.',\n examples: [1, 2, 3],\n });\n\n const OffsetSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Number of items to offset for pagination.',\n examples: [1, 2, 3],\n });\n\n const SkipSchema = decorate(z.number().int().nonnegative().optional(), {\n description: 'Number of items to skip for pagination.',\n examples: [1, 2, 3],\n });\n\n const OrderBySchema = decorate(z.array(z.string()).optional(), {\n description:\n \"Sorting order for results. Supports regular columns ('fieldName ASC') and JSON/JSONB paths ('metadata.field DESC', 'data.nested[0].value ASC').\",\n examples: [\n 'id DESC',\n 'createdAt ASC',\n 'metadata.priority DESC',\n 'data.nested.value ASC',\n 'items[0].score DESC',\n ],\n });\n\n const FieldsSchema = decorate(\n z.record(z.string(), z.boolean()).or(z.array(z.string())).optional(),\n {\n description:\n 'Fields selection - either an array of field names to include, or an object with field names as keys and boolean values (true to include, false to exclude)',\n examples: [\n JSON.stringify(['id', 'name', 'email']),\n JSON.stringify({ id: true, name: true }),\n JSON.stringify({ id: true, name: true, email: true, fullName: false }),\n ],\n },\n );\n\n /** @internal Recursive schema for where clause validation with nested AND/OR. */\n const RecursiveWhereSchema: z.ZodType<any> = z.lazy(() =>\n z.record(z.string(), z.any()).and(\n z.object({\n and: z.array(RecursiveWhereSchema).optional(),\n or: z.array(RecursiveWhereSchema).optional(),\n }),\n ),\n );\n\n /** The depth is checked on the raw value first: the recursive schema below costs the square of it. */\n const BoundedWhereSchema = z\n .any()\n .transform((value, ctx) => {\n if (WhereDepthGuard.exceeds({ value, max: maxWhereDepth })) {\n ctx.addIssue({\n code: 'custom',\n message: `where is nested deeper than ${maxWhereDepth} levels`,\n });\n\n return z.NEVER;\n }\n\n return value;\n })\n .pipe(RecursiveWhereSchema);\n\n const WhereSchema = decorate(\n z.union([\n BoundedWhereSchema,\n z\n .string()\n // A caught parse, never a bare `JSON.parse`. zod lets a thrown SyntaxError escape\n // `safeParse`, so the controller's validation hook never ran and `?where=not-json` surfaced\n // as a 500 with a stack - for what is simply a malformed query string.\n .transform((val, ctx) => {\n if (!val) {\n return undefined;\n }\n\n try {\n return JSON.parse(val);\n } catch {\n ctx.addIssue({ code: 'custom', message: 'where must be valid JSON' });\n\n return z.NEVER;\n }\n })\n .pipe(BoundedWhereSchema),\n ]),\n { type: 'object', description: 'Query conditions for selecting data.' },\n );\n\n /** Zod schema for including related entities in queries with optional nested filtering. */\n const InclusionSchema = decorate(\n z\n .array(\n z.object({\n relation: decorate(z.string(), { description: 'Model relation name' }),\n scope: decorate(\n z\n .lazy((): z.ZodType<any> => FilterSchema) // eslint-disable-line @typescript-eslint/no-use-before-define\n .optional(),\n { description: 'Model relation filter' },\n ),\n // `shouldSkipDefaultFilter` is deliberately NOT on the wire schema. It stays on the\n // internal `TInclusion` type, so server-side callers keep it, but a client could\n // otherwise send `include[].shouldSkipDefaultFilter` and the relation would be emitted\n // with no where clause at all - erasing the `@model` `settings.query.filter` that implements\n // soft-delete and the static visibility scopes.\n }),\n )\n .optional(),\n {\n description: 'Define related models to include in the response.',\n examples: [\n JSON.stringify({ include: [{ relation: 'posts' }] }),\n JSON.stringify({ include: [{ relation: 'posts', scope: { limit: 5 } }] }),\n ],\n },\n );\n\n /** @internal Filter schema object definition. */\n const InternalFilterSchema = z.object({\n where: WhereSchema.optional(),\n fields: FieldsSchema,\n include: InclusionSchema,\n order: OrderBySchema,\n limit: LimitSchema,\n offset: OffsetSchema,\n skip: SkipSchema,\n });\n\n /** Comprehensive Zod schema for repository query filtering. Supports object and JSON string formats. */\n const FilterSchema = decorate(\n z\n .union([\n InternalFilterSchema,\n z\n .string()\n // Same reason as `where` above: a malformed filter is the caller's mistake, so it has to\n // become a validation issue rather than an uncaught throw.\n .transform((val, ctx) => {\n if (!val) {\n return {};\n }\n\n try {\n return JSON.parse(val);\n } catch {\n ctx.addIssue({ code: 'custom', message: 'filter must be valid JSON' });\n\n return z.NEVER;\n }\n })\n .pipe(InternalFilterSchema),\n ])\n .optional(),\n {\n type: 'object',\n description:\n 'A comprehensive filter object for querying data, including conditions, field selection, relations, pagination, and sorting.',\n examples: [\n JSON.stringify({ where: { name: 'John Doe' }, limit: 10 }),\n JSON.stringify({\n fields: { id: true, name: true, email: true },\n order: ['createdAt DESC'],\n }),\n JSON.stringify({ include: [{ relation: 'posts', scope: { limit: 5 } }] }),\n JSON.stringify({\n where: { or: [{ status: 'active' }, { isPublished: true }] },\n skip: 20,\n limit: 10,\n }),\n JSON.stringify({\n where: { and: [{ role: 'admin' }, { createdAt: { gte: 'YYYY-MM-DD' } }] },\n }),\n ],\n },\n );\n\n return {\n FieldsSchema,\n FilterSchema,\n InclusionSchema,\n LimitSchema,\n OffsetSchema,\n OrderBySchema,\n SkipSchema,\n WhereSchema,\n };\n }\n}\n"]}
|
|
@@ -4,7 +4,7 @@ export * from './common/index.js';
|
|
|
4
4
|
export declare const FieldsSchema: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodBoolean>, import("zod").ZodArray<import("zod").ZodString>]>>;
|
|
5
5
|
/** The whole filter: `where`, `fields`, `include`, `order`, `limit`, `offset` and `skip`. Accepts an object or a JSON string; a malformed string becomes a validation issue. */
|
|
6
6
|
export declare const FilterSchema: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodObject<{
|
|
7
|
-
where: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown
|
|
7
|
+
where: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodString, import("zod").ZodTransform<any, string>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>>]>>;
|
|
8
8
|
fields: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodBoolean>, import("zod").ZodArray<import("zod").ZodString>]>>;
|
|
9
9
|
include: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodObject<{
|
|
10
10
|
relation: import("zod").ZodString;
|
|
@@ -15,7 +15,7 @@ export declare const FilterSchema: import("zod").ZodOptional<import("zod").ZodUn
|
|
|
15
15
|
offset: import("zod").ZodOptional<import("zod").ZodNumber>;
|
|
16
16
|
skip: import("zod").ZodOptional<import("zod").ZodNumber>;
|
|
17
17
|
}, import("zod/v4/core").$strip>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodString, import("zod").ZodTransform<any, string>>, import("zod").ZodObject<{
|
|
18
|
-
where: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown
|
|
18
|
+
where: import("zod").ZodOptional<import("zod").ZodUnion<readonly [import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodString, import("zod").ZodTransform<any, string>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>>]>>;
|
|
19
19
|
fields: import("zod").ZodOptional<import("zod").ZodUnion<[import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodBoolean>, import("zod").ZodArray<import("zod").ZodString>]>>;
|
|
20
20
|
include: import("zod").ZodOptional<import("zod").ZodArray<import("zod").ZodObject<{
|
|
21
21
|
relation: import("zod").ZodString;
|
|
@@ -40,5 +40,5 @@ export declare const OrderBySchema: import("zod").ZodOptional<import("zod").ZodA
|
|
|
40
40
|
/** Number of rows to skip for pagination; a non-negative integer, optional. */
|
|
41
41
|
export declare const SkipSchema: import("zod").ZodOptional<import("zod").ZodNumber>;
|
|
42
42
|
/** Query conditions with nested `and` / `or`. Accepts an object or a JSON string; a malformed string becomes a validation issue. */
|
|
43
|
-
export declare const WhereSchema: import("zod").ZodUnion<readonly [import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown
|
|
43
|
+
export declare const WhereSchema: import("zod").ZodUnion<readonly [import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodString, import("zod").ZodTransform<any, string>>, import("zod").ZodPipe<import("zod").ZodPipe<import("zod").ZodAny, import("zod").ZodTransform<any, any>>, import("zod").ZodType<any, unknown, import("zod/v4/core").$ZodTypeInternals<any, unknown>>>>]>;
|
|
44
44
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** How deep an `and` / `or` nesting may go before a `where` is refused. Real filters stay in single digits. */
|
|
2
|
+
export declare const DEFAULT_MAX_WHERE_DEPTH = 20;
|
|
3
|
+
/**
|
|
4
|
+
* Measures the nesting of a `where` BEFORE the recursive schema walks it: the schema's own cost grows with the square of the
|
|
5
|
+
* depth, so a request must be refused on its depth, not after it has been parsed. The measure is iterative, with no
|
|
6
|
+
* recursion of its own, and stops as soon as the limit is passed.
|
|
7
|
+
*/
|
|
8
|
+
export declare class WhereDepthGuard {
|
|
9
|
+
/** True when `value` nests `and` / `or` groups deeper than `max` (a flat `where` is depth 1). */
|
|
10
|
+
static exceeds(opts: {
|
|
11
|
+
value: unknown;
|
|
12
|
+
max: number;
|
|
13
|
+
}): boolean;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=where-depth.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** How deep an `and` / `or` nesting may go before a `where` is refused. Real filters stay in single digits. */
|
|
2
|
+
export const DEFAULT_MAX_WHERE_DEPTH = 20;
|
|
3
|
+
/**
|
|
4
|
+
* Measures the nesting of a `where` BEFORE the recursive schema walks it: the schema's own cost grows with the square of the
|
|
5
|
+
* depth, so a request must be refused on its depth, not after it has been parsed. The measure is iterative, with no
|
|
6
|
+
* recursion of its own, and stops as soon as the limit is passed.
|
|
7
|
+
*/
|
|
8
|
+
export class WhereDepthGuard {
|
|
9
|
+
/** True when `value` nests `and` / `or` groups deeper than `max` (a flat `where` is depth 1). */
|
|
10
|
+
static exceeds(opts) {
|
|
11
|
+
const { value, max } = opts;
|
|
12
|
+
const stack = [{ node: value, depth: 1 }];
|
|
13
|
+
while (stack.length > 0) {
|
|
14
|
+
const { node, depth } = stack.pop();
|
|
15
|
+
if (typeof node !== 'object' || node === null || Array.isArray(node)) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (depth > max) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
for (const key of ['and', 'or']) {
|
|
22
|
+
const branches = node[key];
|
|
23
|
+
if (Array.isArray(branches)) {
|
|
24
|
+
for (const branch of branches) {
|
|
25
|
+
stack.push({ node: branch, depth: depth + 1 });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=where-depth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where-depth.js","sourceRoot":"","sources":["../../../src/schemas/where-depth.ts"],"names":[],"mappings":"AAAA,+GAA+G;AAC/G,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAE1C;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAC1B,iGAAiG;IACjG,MAAM,CAAC,OAAO,CAAC,IAAqC;QAClD,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;QAC5B,MAAM,KAAK,GAA4C,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QAEnF,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,GAAG,EAAsC,CAAC;YAExE,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;gBACrE,SAAS;YACX,CAAC;YAED,IAAI,KAAK,GAAG,GAAG,EAAE,CAAC;gBAChB,OAAO,IAAI,CAAC;YACd,CAAC;YAED,KAAK,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,IAAI,CAAU,EAAE,CAAC;gBACzC,MAAM,QAAQ,GAAI,IAAgC,CAAC,GAAG,CAAC,CAAC;gBAExD,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;oBAC5B,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;wBAC9B,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC;oBACjD,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF","sourcesContent":["/** How deep an `and` / `or` nesting may go before a `where` is refused. Real filters stay in single digits. */\nexport const DEFAULT_MAX_WHERE_DEPTH = 20;\n\n/**\n * Measures the nesting of a `where` BEFORE the recursive schema walks it: the schema's own cost grows with the square of the\n * depth, so a request must be refused on its depth, not after it has been parsed. The measure is iterative, with no\n * recursion of its own, and stops as soon as the limit is passed.\n */\nexport class WhereDepthGuard {\n /** True when `value` nests `and` / `or` groups deeper than `max` (a flat `where` is depth 1). */\n static exceeds(opts: { value: unknown; max: number }): boolean {\n const { value, max } = opts;\n const stack: Array<{ node: unknown; depth: number }> = [{ node: value, depth: 1 }];\n\n while (stack.length > 0) {\n const { node, depth } = stack.pop() as { node: unknown; depth: number };\n\n if (typeof node !== 'object' || node === null || Array.isArray(node)) {\n continue;\n }\n\n if (depth > max) {\n return true;\n }\n\n for (const key of ['and', 'or'] as const) {\n const branches = (node as Record<string, unknown>)[key];\n\n if (Array.isArray(branches)) {\n for (const branch of branches) {\n stack.push({ node: branch, depth: depth + 1 });\n }\n }\n }\n }\n\n return false;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venizia/ignis-filter",
|
|
3
|
-
"version": "0.4.7-
|
|
3
|
+
"version": "0.4.7-3",
|
|
4
4
|
"description": "Engine-neutral query filter vocabulary for TypeScript: the `{ where, order, limit, offset, skip, fields, include }` shape, comparison/pattern/null/array/logical operators, and sort direction constants. Isomorphic by construction - no node builtins and no server-only peer, so the same filter language serves a server repository and a browser data layer.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"browser",
|
|
@@ -61,12 +61,12 @@
|
|
|
61
61
|
"force-update": "sh ../../scripts/build/force-update.sh"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@venizia/ignis-inversion": "0.4.7-
|
|
64
|
+
"@venizia/ignis-inversion": "0.4.7-2",
|
|
65
65
|
"zod": "^4.6.7"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/bun": "^1.4.3",
|
|
69
|
-
"@venizia/dev-configs": "0.4.8-
|
|
69
|
+
"@venizia/dev-configs": "0.4.8-2",
|
|
70
70
|
"eslint": "^10.13.0",
|
|
71
71
|
"oxlint": "1.89.0",
|
|
72
72
|
"oxlint-tsgolint": "7.0.2004",
|