Skip to content

Commit

Permalink
Add isValid field to standard model validationResult (#51310)
Browse files Browse the repository at this point in the history
This doesn't change much except aggregates the validation results into a
neat field, to help us easier access this value when we update the
standard model in the StandardEditor
  • Loading branch information
avatus authored Jan 22, 2025
1 parent 0a4e17f commit 6e23568
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('validateRoleEditorModel', () => {
expect(result.metadata.valid).toBe(true);
expect(result.resources).toEqual([]);
expect(result.rules).toEqual([]);
expect(result.isValid).toBe(true);
});

test('valid complex model', () => {
Expand Down Expand Up @@ -103,13 +104,15 @@ describe('validateRoleEditorModel', () => {
expect(result.metadata.valid).toBe(true);
expect(validity(result.resources)).toEqual([true, true, true, true, true]);
expect(validity(result.rules)).toEqual([true]);
expect(result.isValid).toBe(true);
});

test('invalid metadata', () => {
const model = minimalRoleModel();
model.metadata.name = '';
const result = validateRoleEditorModel(model, undefined, undefined);
expect(result.metadata.valid).toBe(false);
expect(result.isValid).toBe(false);
});

test('invalid resource', () => {
Expand All @@ -123,6 +126,7 @@ describe('validateRoleEditorModel', () => {
];
const result = validateRoleEditorModel(model, undefined, undefined);
expect(validity(result.resources)).toEqual([false]);
expect(result.isValid).toBe(false);
});

test('invalid access rule', () => {
Expand All @@ -136,6 +140,7 @@ describe('validateRoleEditorModel', () => {
];
const result = validateRoleEditorModel(model, undefined, undefined);
expect(validity(result.rules)).toEqual([false]);
expect(result.isValid).toBe(false);
});

it('reuses previously computed section results', () => {
Expand All @@ -145,6 +150,7 @@ describe('validateRoleEditorModel', () => {
expect(result2.metadata).toBe(result1.metadata);
expect(result2.resources).toBe(result1.resources);
expect(result2.rules).toBe(result1.rules);
expect(result2.isValid).toBe(result1.isValid);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export type RoleEditorModelValidationResult = {
metadata: MetadataValidationResult;
resources: ResourceAccessValidationResult[];
rules: AccessRuleValidationResult[];
/**
* isValid is true if all the fields in the validation result are valid.
*/
isValid: boolean;
};

/**
Expand All @@ -72,22 +76,32 @@ export function validateRoleEditorModel(
previousModel: RoleEditorModel | undefined,
previousResult: RoleEditorModelValidationResult | undefined
): RoleEditorModelValidationResult {
const metadataResult = validateMetadata(
model.metadata,
previousModel?.metadata,
previousResult?.metadata
);

const resourcesResult = validateResourceAccessList(
model.resources,
previousModel?.resources,
previousResult?.resources
);

const rulesResult = validateAccessRuleList(
model.rules,
previousModel?.rules,
previousResult?.rules
);

return {
metadata: validateMetadata(
model.metadata,
previousModel?.metadata,
previousResult?.metadata
),
resources: validateResourceAccessList(
model.resources,
previousModel?.resources,
previousResult?.resources
),
rules: validateAccessRuleList(
model.rules,
previousModel?.rules,
previousResult?.rules
),
isValid:
metadataResult.valid &&
resourcesResult.every(r => r.valid) &&
rulesResult.every(r => r.valid),
metadata: metadataResult,
resources: resourcesResult,
rules: rulesResult,
};
}

Expand Down

0 comments on commit 6e23568

Please sign in to comment.