Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isValid field to standard model validationResult #51310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading