codify-plugin-lib 1.0.85 → 1.0.87

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.
@@ -1,6 +1,5 @@
1
1
  import { Plan } from '../plan/plan.js';
2
2
  import { ResourceController } from '../resource/resource-controller.js';
3
- import { splitUserConfig } from '../utils/utils.js';
4
3
  export class Plugin {
5
4
  name;
6
5
  resourceControllers;
@@ -34,10 +33,9 @@ export class Plugin {
34
33
  if (!this.resourceControllers.has(config.type)) {
35
34
  throw new Error(`Resource type not found: ${config.type}`);
36
35
  }
37
- const { parameters, coreParameters } = splitUserConfig(config);
38
36
  const validation = await this.resourceControllers
39
37
  .get(config.type)
40
- .validate(parameters, coreParameters);
38
+ .validate(config);
41
39
  validationResults.push(validation);
42
40
  }
43
41
  await this.crossValidateResources(data.configs);
@@ -14,7 +14,7 @@ export declare class ResourceController<T extends StringIndexedObject> {
14
14
  protected schemaValidator?: ValidateFunction;
15
15
  constructor(resource: Resource<T>);
16
16
  initialize(): Promise<void>;
17
- validate(parameters: Partial<T>, resourceMetaData: ResourceConfig): Promise<ValidateResponseData['resourceValidations'][0]>;
17
+ validate(desiredConfig: Partial<T> & ResourceConfig): Promise<ValidateResponseData['resourceValidations'][0]>;
18
18
  plan(desiredConfig: Partial<T> & ResourceConfig | null, stateConfig?: Partial<T> & ResourceConfig | null, statefulMode?: boolean): Promise<Plan<T>>;
19
19
  apply(plan: Plan<T>): Promise<void>;
20
20
  private applyCreate;
@@ -31,14 +31,18 @@ export class ResourceController {
31
31
  async initialize() {
32
32
  return this.resource.initialize();
33
33
  }
34
- async validate(parameters, resourceMetaData) {
34
+ async validate(desiredConfig) {
35
+ const configToValidate = { ...desiredConfig };
36
+ await this.applyTransformParameters(configToValidate);
37
+ const { parameters, coreParameters } = splitUserConfig(configToValidate);
38
+ this.addDefaultValues(parameters);
35
39
  if (this.schemaValidator) {
36
40
  const isValid = this.schemaValidator(parameters);
37
41
  if (!isValid) {
38
42
  return {
39
43
  isValid: false,
40
- resourceName: resourceMetaData.name,
41
- resourceType: resourceMetaData.type,
44
+ resourceName: coreParameters.name,
45
+ resourceType: coreParameters.type,
42
46
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
43
47
  };
44
48
  }
@@ -56,15 +60,15 @@ export class ResourceController {
56
60
  return {
57
61
  customValidationErrorMessage,
58
62
  isValid: false,
59
- resourceName: resourceMetaData.name,
60
- resourceType: resourceMetaData.type,
63
+ resourceName: coreParameters.name,
64
+ resourceType: coreParameters.type,
61
65
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
62
66
  };
63
67
  }
64
68
  return {
65
69
  isValid: true,
66
- resourceName: resourceMetaData.name,
67
- resourceType: resourceMetaData.type,
70
+ resourceName: coreParameters.name,
71
+ resourceType: coreParameters.type,
68
72
  schemaValidationErrors: [],
69
73
  };
70
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.85",
3
+ "version": "1.0.87",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -11,7 +11,6 @@ import {
11
11
  import { Plan } from '../plan/plan.js';
12
12
  import { Resource } from '../resource/resource.js';
13
13
  import { ResourceController } from '../resource/resource-controller.js';
14
- import { splitUserConfig } from '../utils/utils.js';
15
14
 
16
15
  export class Plugin {
17
16
  planStorage: Map<string, Plan<any>>;
@@ -55,10 +54,9 @@ export class Plugin {
55
54
  throw new Error(`Resource type not found: ${config.type}`);
56
55
  }
57
56
 
58
- const { parameters, coreParameters } = splitUserConfig(config);
59
57
  const validation = await this.resourceControllers
60
58
  .get(config.type)!
61
- .validate(parameters, coreParameters);
59
+ .validate(config);
62
60
 
63
61
  validationResults.push(validation);
64
62
  }
@@ -7,9 +7,38 @@ import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
7
7
  import { ParameterChange } from '../plan/change-set.js';
8
8
  import { ResourceController } from './resource-controller.js';
9
9
  import { TestConfig, testPlan, TestResource, TestStatefulParameter } from '../utils/test-utils.test.js';
10
+ import { untildify } from '../utils/utils.js';
10
11
 
11
12
  describe('Resource tests', () => {
12
13
 
14
+ it('Validate applies transformations before validating', async () => {
15
+ const resource = new class extends TestResource {
16
+ getSettings(): ResourceSettings<TestConfig> {
17
+ return {
18
+ id: 'type',
19
+ dependencies: ['homebrew', 'python'],
20
+ parameterSettings: {
21
+ propA: { canModify: true, inputTransformation: (input) => untildify(input) },
22
+ },
23
+ inputTransformation: (config) => ({ propA: config.propA, propC: config.propB }),
24
+ }
25
+ }
26
+
27
+ async validate(parameters: Partial<TestConfig>): Promise<void> {
28
+ expect(parameters.propA).to.not.include('~');
29
+ expect(parameters.propB).to.not.exist;
30
+ expect(parameters.propC).to.equal(10);
31
+ }
32
+ }
33
+
34
+ const controller = new ResourceController(resource);
35
+ await controller.validate({
36
+ type: 'type',
37
+ propA: '~/.tool_versions',
38
+ propB: 10,
39
+ })
40
+ })
41
+
13
42
  it('Plans successfully', async () => {
14
43
  const resource = new class extends TestResource {
15
44
 
@@ -54,17 +54,22 @@ export class ResourceController<T extends StringIndexedObject> {
54
54
  }
55
55
 
56
56
  async validate(
57
- parameters: Partial<T>,
58
- resourceMetaData: ResourceConfig
57
+ desiredConfig: Partial<T> & ResourceConfig,
59
58
  ): Promise<ValidateResponseData['resourceValidations'][0]> {
59
+ const configToValidate = { ...desiredConfig };
60
+ await this.applyTransformParameters(configToValidate);
61
+
62
+ const { parameters, coreParameters } = splitUserConfig(configToValidate);
63
+ this.addDefaultValues(parameters);
64
+
60
65
  if (this.schemaValidator) {
61
66
  const isValid = this.schemaValidator(parameters);
62
67
 
63
68
  if (!isValid) {
64
69
  return {
65
70
  isValid: false,
66
- resourceName: resourceMetaData.name,
67
- resourceType: resourceMetaData.type,
71
+ resourceName: coreParameters.name,
72
+ resourceType: coreParameters.type,
68
73
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
69
74
  }
70
75
  }
@@ -83,16 +88,16 @@ export class ResourceController<T extends StringIndexedObject> {
83
88
  return {
84
89
  customValidationErrorMessage,
85
90
  isValid: false,
86
- resourceName: resourceMetaData.name,
87
- resourceType: resourceMetaData.type,
91
+ resourceName: coreParameters.name,
92
+ resourceType: coreParameters.type,
88
93
  schemaValidationErrors: this.schemaValidator?.errors ?? [],
89
94
  }
90
95
  }
91
96
 
92
97
  return {
93
98
  isValid: true,
94
- resourceName: resourceMetaData.name,
95
- resourceType: resourceMetaData.type,
99
+ resourceName: coreParameters.name,
100
+ resourceType: coreParameters.type,
96
101
  schemaValidationErrors: [],
97
102
  }
98
103
  }