Skip to content

Commit

Permalink
node versions are not always ints
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Dec 27, 2024
1 parent 0d01e39 commit 479463e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ describe('ResourceLocatorRequestDto', () => {
currentNodeParameters: { param1: 'value1' },
},
},
{
name: 'request with a semver node version',
request: {
...baseValidRequest,
nodeTypeAndVersion: { name: 'TestNode', version: 1.1 },
},
},
])('should validate $name', ({ request }) => {
const result = ResourceLocatorRequestDto.safeParse(request);
expect(result.success).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import type { INodeCredentials, INodeParameters, INodeTypeNameVersion } from 'n8
import { z } from 'zod';
import { Z } from 'zod-class';

import { nodeVersionSchema } from '../../schemas/nodeVersion.schema';

export class BaseDynamicParametersRequestDto extends Z.class({
path: z.string(),
nodeTypeAndVersion: z.object({
name: z.string(),
version: z.number().int().min(1),
version: nodeVersionSchema,
}) satisfies z.ZodType<INodeTypeNameVersion>,
currentNodeParameters: z.record(z.string(), z.any()) satisfies z.ZodType<INodeParameters>,
methodName: z.string().optional(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { nodeVersionSchema } from '../nodeVersion.schema';

describe('nodeVersionSchema', () => {
describe('valid versions', () => {
test.each([
[1, 'single digit'],
[2, 'single digit'],
[1.0, 'major.minor with zero minor'],
[1.2, 'major.minor'],
[10.5, 'major.minor with double digits'],
])('should accept %s as a valid version (%s)', (version) => {
const validated = nodeVersionSchema.parse(version);
expect(validated).toBe(version);
});
});

describe('invalid versions', () => {
test.each([
['not-a-number', 'non-number input'],
['1.2.3', 'more than two parts'],
['1.a', 'non-numeric characters'],
['1.2.3', 'more than two parts as string'],
])('should reject %s as an invalid version (%s)', (version) => {
const check = () => nodeVersionSchema.parse(version);
expect(check).toThrowError();
});
});
});
17 changes: 17 additions & 0 deletions packages/@n8n/api-types/src/schemas/nodeVersion.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { z } from 'zod';

export const nodeVersionSchema = z
.number()
.min(1)
.refine(
(val) => {
const parts = String(val).split('.');
return (
(parts.length === 1 && !isNaN(Number(parts[0]))) ||
(parts.length === 2 && !isNaN(Number(parts[0])) && !isNaN(Number(parts[1])))
);
},
{
message: 'Invalid node version. Must be in format: major.minor',
},
);

0 comments on commit 479463e

Please sign in to comment.