Fixed allowing optional params outside of the toQuery and apply

Signed-off-by: Harry Hogg <hhogg@spotify.com>

Co-authored-by: Mike Lewis <mlewis@spotify.com>
This commit is contained in:
Harry Hogg
2022-10-11 11:46:28 +01:00
parent ddf3986add
commit bbbe968e10
2 changed files with 6 additions and 29 deletions
@@ -46,7 +46,7 @@ const testIntegration = () =>
description: 'Test rule 2',
resourceType: 'test-resource',
paramsSchema: z.object({
foo: z.string(),
foo: z.string().optional(),
}),
apply: (_resource: any) => false,
toQuery: params => ({
@@ -76,12 +76,10 @@ describe('createConditionExports', () => {
},
});
expect(conditions.testRule2({ foo: 'baz' })).toEqual({
expect(conditions.testRule2({})).toEqual({
rule: 'testRule2',
resourceType: 'test-resource',
params: {
foo: 'baz',
},
params: {},
});
});
});
+3 -24
View File
@@ -21,22 +21,6 @@ import type {
import { z } from 'zod';
import { NoInfer } from './integration/util';
/**
* A ZodSchema that reflects the structure of the parameters that are passed to
* into a {@link PermissionRule}.
*
* @public
*/
export type PermissionRuleSchema<TParams> = z.ZodObject<{
// Parameters can be optional, however we we want to make sure that the
// parameters are always present in the schema, even if they are undefined.
// We remove the optional flag from the schema, and then add it back in
// with an optional zod type.
[P in keyof TParams]-?: TParams[P] extends undefined
? z.ZodOptionalType<z.ZodType<TParams[P]>>
: z.ZodType<TParams[P]>;
}>;
/**
* A conditional rule that can be provided in an
* {@link @backstage/permission-common#AuthorizeDecision} response to an authorization request.
@@ -66,24 +50,19 @@ export type PermissionRule<
/**
* A ZodSchema that reflects the structure of the parameters that are passed to
*/
paramsSchema?: PermissionRuleSchema<TParams>;
paramsSchema?: z.ZodSchema<TParams>;
/**
* Apply this rule to a resource already loaded from a backing data source. The params are
* arguments supplied for the rule; for example, a rule could be `isOwner` with entityRefs as the
* params.
*/
apply(
resource: TResource,
params: NoInfer<z.input<PermissionRuleSchema<TParams>>>,
): boolean;
apply(resource: TResource, params: NoInfer<TParams>): boolean;
/**
* Translate this rule to criteria suitable for use in querying a backing data store. The criteria
* can be used for loading a collection of resources efficiently with conditional criteria already
* applied.
*/
toQuery(
params: NoInfer<z.input<PermissionRuleSchema<TParams>>>,
): PermissionCriteria<TQuery>;
toQuery(params: NoInfer<TParams>): PermissionCriteria<TQuery>;
};