Skip to content

Commit 9996a10

Browse files
Merge branch 'master' into handle-dependencies-ts
2 parents ed406ba + 44a2cc9 commit 9996a10

15 files changed

+288
-254
lines changed

src/registry/domain/validators/component-parameters.js

Lines changed: 0 additions & 106 deletions
This file was deleted.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import strings from '../../../resources';
2+
import { OcParameter } from '../../../types';
3+
4+
const validateParameter = function(
5+
parameter: string | number | boolean,
6+
expectedType: string
7+
): boolean {
8+
const expected = expectedType.toLowerCase();
9+
10+
if (expected === 'boolean') {
11+
return typeof parameter === 'boolean';
12+
} else if (expected === 'number') {
13+
return typeof parameter === 'number';
14+
} else if (expected === 'string') {
15+
return typeof parameter === 'string';
16+
}
17+
18+
return false;
19+
};
20+
21+
interface ValidationResult {
22+
isValid: boolean;
23+
errors: {
24+
mandatory?: Dictionary<string>;
25+
types?: Dictionary<string>;
26+
message?: string;
27+
};
28+
}
29+
30+
export default function componentParameters(
31+
requestParameters: Dictionary<string | number | boolean>,
32+
expectedParameters: Dictionary<OcParameter> = {}
33+
): ValidationResult {
34+
const result: ValidationResult = { isValid: true, errors: {} };
35+
const mandatoryParameters: string[] = [];
36+
37+
for (const [expectedParameterName, expectedParameter] of Object.entries(
38+
expectedParameters
39+
)) {
40+
if (expectedParameter.mandatory) {
41+
mandatoryParameters.push(expectedParameterName);
42+
}
43+
}
44+
45+
for (const mandatoryParameterName of mandatoryParameters) {
46+
if (
47+
typeof requestParameters === 'object' &&
48+
// eslint-disable-next-line no-prototype-builtins
49+
!requestParameters.hasOwnProperty(mandatoryParameterName)
50+
) {
51+
if (!result.errors.mandatory) {
52+
result.errors.mandatory = {};
53+
result.isValid = false;
54+
}
55+
56+
result.errors.mandatory[mandatoryParameterName] =
57+
strings.errors.registry.MANDATORY_PARAMETER_MISSING_CODE;
58+
}
59+
}
60+
61+
for (const [requestParameterName, requestParameter] of Object.entries(
62+
requestParameters
63+
)) {
64+
if (
65+
typeof expectedParameters === 'object' &&
66+
// eslint-disable-next-line no-prototype-builtins
67+
expectedParameters.hasOwnProperty(requestParameterName)
68+
) {
69+
const expectedType = expectedParameters[requestParameterName].type;
70+
71+
if (!validateParameter(requestParameter, expectedType)) {
72+
if (!result.errors.types) {
73+
result.errors.types = {};
74+
result.isValid = false;
75+
}
76+
77+
result.errors.types[requestParameterName] =
78+
strings.errors.registry.PARAMETER_WRONG_FORMAT_CODE;
79+
}
80+
}
81+
}
82+
83+
result.errors.message = (function() {
84+
let errorString = '';
85+
86+
if (Object.keys(result.errors.mandatory || {}).length > 0) {
87+
const missingParams = Object.keys(result.errors.mandatory || {})
88+
.map(mandatoryParameterName => mandatoryParameterName + ', ')
89+
.join('')
90+
.slice(0, -2);
91+
92+
errorString += strings.errors.registry.MANDATORY_PARAMETER_MISSING(
93+
missingParams
94+
);
95+
}
96+
97+
if (Object.keys(result.errors.types || {}).length > 0) {
98+
if (errorString.length > 0) {
99+
errorString += '; ';
100+
}
101+
102+
const badParams = Object.keys(result.errors.types || {})
103+
.map(parameterName => parameterName + ', ')
104+
.join('')
105+
.slice(0, -2);
106+
107+
errorString += strings.errors.registry.PARAMETER_WRONG_FORMAT(badParams);
108+
}
109+
return errorString;
110+
})();
111+
112+
return result;
113+
}

src/registry/domain/validators/index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import semver from 'semver';
2+
3+
import ocCliVersionValidator from './oc-cli-version';
4+
import componentParametersValidator from './component-parameters';
5+
import packageJsonValidator from './package-json-validator';
6+
import pluginsRequirementsValidator from './plugins-requirements';
7+
import registryConfigurationValidator from './registry-configuration';
8+
9+
import uploadedPackageValidator from './uploaded-package';
10+
import nodeVersionValidator from './node-version';
11+
12+
export function validateComponentName(componentName: string): boolean {
13+
return !/[^a-zA-Z0-9\-_]/.test(componentName) && componentName !== '_package';
14+
}
15+
export const validateComponentParameters = componentParametersValidator;
16+
export const validateNodeVersion = nodeVersionValidator;
17+
export const validateOcCliVersion = ocCliVersionValidator;
18+
export const validatePackage = uploadedPackageValidator;
19+
export const validatePackageJson = packageJsonValidator;
20+
export const validatePluginsRequirements = pluginsRequirementsValidator;
21+
export const validateRegistryConfiguration = registryConfigurationValidator;
22+
export function validateVersion(version: string): boolean {
23+
return !!semver.valid(version);
24+
}

src/registry/domain/validators/node-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import semver from 'semver';
22
import fs from 'fs-extra';
33
import path from 'path';
44

5-
const packageInfo = fs.readJSONSync(
5+
const packageInfo = fs.readJsonSync(
66
path.join(__dirname, '..', '..', '..', '..', 'package.json')
77
);
88

@@ -19,7 +19,7 @@ type ErrorResult = {
1919
type Result = OkResult | ErrorResult;
2020

2121
export default function nodeVersion(
22-
userAgent: string,
22+
userAgent: string | undefined,
2323
nodeVersion: string
2424
): Result {
2525
const baseError = (

src/registry/domain/validators/oc-cli-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import semver from 'semver';
22
import fs from 'fs-extra';
33
import path from 'path';
44

5-
const packageInfo = fs.readJSONSync(
5+
const packageInfo = fs.readJsonSync(
66
path.join(__dirname, '..', '..', '..', '..', 'package.json')
77
);
88

@@ -18,7 +18,7 @@ type ErrorResult = {
1818
};
1919
type Result = OkResult | ErrorResult;
2020

21-
export default function ocCliVersion(userAgent: string): Result {
21+
export default function ocCliVersion(userAgent?: string): Result {
2222
const baseError = (
2323
opts: Partial<ErrorResult['error']> = {}
2424
): ErrorResult => ({

src/registry/domain/validators/package-json-validator.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import strings from '../../../resources';
2+
3+
interface ValidationResult {
4+
isValid: boolean;
5+
error?: string;
6+
}
7+
8+
interface PkgDetails {
9+
componentName: string;
10+
packageJson: { name: string };
11+
customValidator: (data: unknown) => ValidationResult | boolean;
12+
}
13+
14+
export default function packageJsonValidator(
15+
pkgDetails: PkgDetails
16+
): ValidationResult {
17+
if (pkgDetails.packageJson.name !== pkgDetails.componentName) {
18+
return {
19+
isValid: false,
20+
error: strings.errors.registry.COMPONENT_PUBLISHNAME_CONFLICT
21+
};
22+
}
23+
24+
let result = pkgDetails.customValidator(pkgDetails.packageJson);
25+
26+
if (typeof result === 'boolean') {
27+
result = { isValid: result };
28+
29+
if (!result.isValid) {
30+
result.error = 'unknown';
31+
}
32+
}
33+
34+
return result;
35+
}

0 commit comments

Comments
 (0)