Skip to content

Commit

Permalink
rename attrs to attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Feb 29, 2020
1 parent 8abbc7d commit b190402
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 79 deletions.
21 changes: 10 additions & 11 deletions packages/@aws-cdk/aws-cognito/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,20 @@ four optional attributes.
new UserPool(this, 'myuserpool', {
// ...
// ...
attributes: {
required: [ StandardAttrs.address, StandardAttrs.name ],
custom: {
'myappid': new StringAttr({ minLen: 5, maxLen: 15 }),
'callingcode': new NumberAttr({ min: 1, max: 3 }),
'isEmployee': new BooleanAttr(),
'joinedOn': new DateTimeAttr()
},
}
requiredAttributes: [ StandardAttribute.address, StandardAttribute.name ],
customAttributes: {
'myappid': new StringAttribute({ minLen: 5, maxLen: 15 }),
'callingcode': new NumberAttribute({ min: 1, max: 3 }),
'isEmployee': new BooleanAttribute(),
'joinedOn': new DateTimeAttribute()
},
});
```

As shown in the code snippet, there are data types that are available for custom attributes. The 'String' and 'Number'
data types allow for further constraints on their length and values, respectively. Custom attributes cannot be marked
as required.
data types allow for further constraints on their length and values, respectively.

Custom attributes cannot be marked as required.

### Importing User Pools

Expand Down
42 changes: 21 additions & 21 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#cognito-user-pools-standard-attributes
*/
export enum StandardAttrs {
export enum StandardAttribute {
/**
* End-User's preferred postal address.
*/
Expand Down Expand Up @@ -119,24 +119,24 @@ export enum StandardAttrs {
/**
* Represents a custom attribute type.
*/
export interface ICustomAttr {
export interface ICustomAttribute {
/**
* Bind this custom attribute type to the values as expected by CloudFormation
*/
bind(): CustomAttrConfig;
bind(): CustomAttributeConfig;
}

/**
* Configuration that will be fed into CloudFormation for any custom attribute type.
*/
export interface CustomAttrConfig {
export interface CustomAttributeConfig {
// tslint:disable:max-line-length
/**
* The data type of the custom attribute.
*
* @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_SchemaAttributeType.html#CognitoUserPools-Type-SchemaAttributeType-AttributeDataType
*/
readonly attrDataType: string;
readonly dataType: string;
// tslint:enable:max-line-length

/**
Expand All @@ -152,7 +152,7 @@ export interface CustomAttrConfig {
/**
* Props for constructing a StringAttr
*/
export interface StringAttrProps {
export interface StringAttributeProps {
/**
* Minimum length of this attribute.
* @default 0
Expand All @@ -169,11 +169,11 @@ export interface StringAttrProps {
/**
* The String custom attribute type.
*/
export class StringAttr implements ICustomAttr {
export class StringAttribute implements ICustomAttribute {
private readonly minLen?: number;
private readonly maxLen?: number;

constructor(props: StringAttrProps = {}) {
constructor(props: StringAttributeProps = {}) {
if (props.minLen && props.minLen < 0) {
throw new Error(`minLen cannot be less than 0 (value: ${props.minLen}).`);
}
Expand All @@ -184,7 +184,7 @@ export class StringAttr implements ICustomAttr {
this.maxLen = props?.maxLen;
}

public bind(): CustomAttrConfig {
public bind(): CustomAttributeConfig {
const constraints = {
stringAttributeConstraints: {
minLength: this.minLen?.toString(),
Expand All @@ -193,7 +193,7 @@ export class StringAttr implements ICustomAttr {
};

return {
attrDataType: 'String',
dataType: 'String',
constraints: (this.minLen || this.maxLen) ? constraints : undefined,
};
}
Expand All @@ -202,7 +202,7 @@ export class StringAttr implements ICustomAttr {
/**
* Props for NumberAttr
*/
export interface NumberAttrProps {
export interface NumberAttributeProps {
/**
* Minimum value of this attribute.
* @default - no minimum value
Expand All @@ -219,16 +219,16 @@ export interface NumberAttrProps {
/**
* The Number custom attribute type.
*/
export class NumberAttr implements ICustomAttr {
export class NumberAttribute implements ICustomAttribute {
private readonly min?: number;
private readonly max?: number;

constructor(props: NumberAttrProps = {}) {
constructor(props: NumberAttributeProps = {}) {
this.min = props?.min;
this.max = props?.max;
}

public bind(): CustomAttrConfig {
public bind(): CustomAttributeConfig {
const constraints = {
numberAttributeConstraints: {
minValue: this.min?.toString(),
Expand All @@ -237,7 +237,7 @@ export class NumberAttr implements ICustomAttr {
};

return {
attrDataType: 'Number',
dataType: 'Number',
constraints: (this.min || this.max) ? constraints : undefined,
};
}
Expand All @@ -246,21 +246,21 @@ export class NumberAttr implements ICustomAttr {
/**
* The Boolean custom attribute type.
*/
export class BooleanAttr implements ICustomAttr {
public bind(): CustomAttrConfig {
export class BooleanAttribute implements ICustomAttribute {
public bind(): CustomAttributeConfig {
return {
attrDataType: 'Boolean'
dataType: 'Boolean'
};
}
}

/**
* The DateTime custom attribute type.
*/
export class DateTimeAttr implements ICustomAttr {
public bind(): CustomAttrConfig {
export class DateTimeAttribute implements ICustomAttribute {
public bind(): CustomAttributeConfig {
return {
attrDataType: 'DateTime'
dataType: 'DateTime'
};
}
}
36 changes: 18 additions & 18 deletions packages/@aws-cdk/aws-cognito/lib/user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IRole, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from '
import * as lambda from '@aws-cdk/aws-lambda';
import { Construct, IResource, Lazy, Resource, Stack } from '@aws-cdk/core';
import { CfnUserPool } from './cognito.generated';
import { ICustomAttr, StandardAttrs } from './user-pool-attr';
import { ICustomAttribute, StandardAttribute } from './user-pool-attr';

/**
* The different ways in which users of this pool can sign up or sign in.
Expand Down Expand Up @@ -262,14 +262,14 @@ export interface UserPoolProps {
*
* @default - No attributes are required.
*/
readonly requiredAttrs?: StandardAttrs[];
readonly requiredAttributes?: StandardAttribute[];

/**
* Define a set of custom attributes that can be configured for each user in the user pool.
*
* @default - No custom attributes.
*/
readonly customAttrs?: { [key: string]: ICustomAttr };
readonly customAttributes?: { [key: string]: ICustomAttribute };

/**
* Lambda functions to use for supported Cognito triggers.
Expand Down Expand Up @@ -547,24 +547,24 @@ export class UserPool extends Resource implements IUserPool {

if (signIn.username) {
aliasAttrs = [];
if (signIn.email) { aliasAttrs.push(StandardAttrs.EMAIL); }
if (signIn.phone) { aliasAttrs.push(StandardAttrs.PHONE_NUMBER); }
if (signIn.preferredUsername) { aliasAttrs.push(StandardAttrs.PREFERRED_USERNAME); }
if (signIn.email) { aliasAttrs.push(StandardAttribute.EMAIL); }
if (signIn.phone) { aliasAttrs.push(StandardAttribute.PHONE_NUMBER); }
if (signIn.preferredUsername) { aliasAttrs.push(StandardAttribute.PREFERRED_USERNAME); }
if (aliasAttrs.length === 0) { aliasAttrs = undefined; }
} else {
usernameAttrs = [];
if (signIn.email) { usernameAttrs.push(StandardAttrs.EMAIL); }
if (signIn.phone) { usernameAttrs.push(StandardAttrs.PHONE_NUMBER); }
if (signIn.email) { usernameAttrs.push(StandardAttribute.EMAIL); }
if (signIn.phone) { usernameAttrs.push(StandardAttribute.PHONE_NUMBER); }
}

if (props.autoVerify) {
autoVerifyAttrs = [];
if (props.autoVerify.email) { autoVerifyAttrs.push(StandardAttrs.EMAIL); }
if (props.autoVerify.phone) { autoVerifyAttrs.push(StandardAttrs.PHONE_NUMBER); }
if (props.autoVerify.email) { autoVerifyAttrs.push(StandardAttribute.EMAIL); }
if (props.autoVerify.phone) { autoVerifyAttrs.push(StandardAttribute.PHONE_NUMBER); }
} else if (signIn.email || signIn.phone) {
autoVerifyAttrs = [];
if (signIn.email) { autoVerifyAttrs.push(StandardAttrs.EMAIL); }
if (signIn.phone) { autoVerifyAttrs.push(StandardAttrs.PHONE_NUMBER); }
if (signIn.email) { autoVerifyAttrs.push(StandardAttribute.EMAIL); }
if (signIn.phone) { autoVerifyAttrs.push(StandardAttribute.PHONE_NUMBER); }
}

return { usernameAttrs, aliasAttrs, autoVerifyAttrs };
Expand Down Expand Up @@ -610,18 +610,18 @@ export class UserPool extends Resource implements IUserPool {
private schemaConfiguration(props: UserPoolProps): CfnUserPool.SchemaAttributeProperty[] | undefined {
const schema: CfnUserPool.SchemaAttributeProperty[] = [];

if (props.requiredAttrs) {
schema.push(...props.requiredAttrs.map((attr) => {
if (props.requiredAttributes) {
schema.push(...props.requiredAttributes.map((attr) => {
return { name: attr, required: true };
}));
}

if (props.customAttrs) {
const customAttrs = Object.keys(props.customAttrs).map((attrName) => {
const attrConfig = props.customAttrs![attrName].bind();
if (props.customAttributes) {
const customAttrs = Object.keys(props.customAttributes).map((attrName) => {
const attrConfig = props.customAttributes![attrName].bind();
return {
name: attrName,
attributeDataType: attrConfig.attrDataType,
attributeDataType: attrConfig.dataType,
...attrConfig.constraints,
};
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import { BooleanAttr, DateTimeAttr, NumberAttr, StandardAttrs, StringAttr, UserPool } from '../lib';
import { BooleanAttribute, DateTimeAttribute, NumberAttribute, StandardAttribute, StringAttribute, UserPool } from '../lib';

const app = new App();
const stack = new Stack(app, 'integ-user-pool');
Expand All @@ -25,14 +25,14 @@ const userpool = new UserPool(stack, 'myuserpool', {
email: true,
phone: true,
},
requiredAttrs: [ StandardAttrs.NAME, StandardAttrs.EMAIL ],
customAttrs: {
'some-string-attr': new StringAttr(),
'another-string-attr': new StringAttr({ minLen: 4, maxLen: 100 }),
'some-number-attr': new NumberAttr(),
'another-number-attr': new NumberAttr({ min: 10, max: 50 }),
'some-boolean-attr': new BooleanAttr(),
'some-datetime-attr': new DateTimeAttr(),
requiredAttributes: [ StandardAttribute.NAME, StandardAttribute.EMAIL ],
customAttributes: {
'some-string-attr': new StringAttribute(),
'another-string-attr': new StringAttribute({ minLen: 4, maxLen: 100 }),
'some-number-attr': new NumberAttribute(),
'another-number-attr': new NumberAttribute({ min: 10, max: 50 }),
'some-boolean-attr': new BooleanAttribute(),
'some-datetime-attr': new DateTimeAttribute(),
}
});

Expand Down
50 changes: 39 additions & 11 deletions packages/@aws-cdk/aws-cognito/test/user-pool-attr.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import '@aws-cdk/assert/jest';
import { NumberAttr, StringAttr } from '../lib';
import { BooleanAttribute, DateTimeAttribute, NumberAttribute, StringAttribute } from '../lib';

describe('User Pool Attributes', () => {
describe('StringAttr', () => {
describe('StringAttribute', () => {
test('default', () => {
// GIVEN
const attr = new StringAttr();
const attr = new StringAttribute();

// WHEN
const bound = attr.bind();

// THEN
expect(bound.attrDataType).toEqual('String');
expect(bound.dataType).toEqual('String');
expect(bound.constraints).toBeUndefined();
});

test('specified constraints are recognized', () => {
// GIVEN
const attr = new StringAttr({ minLen: 10, maxLen: 60 });
const attr = new StringAttribute({ minLen: 10, maxLen: 60 });

// WHEN
const bound = attr.bind();
Expand All @@ -32,29 +32,29 @@ describe('User Pool Attributes', () => {
});

test('throws error when crossing limits', () => {
expect(() => new StringAttr({ minLen: -10 }))
expect(() => new StringAttribute({ minLen: -10 }))
.toThrow(/minLen cannot be less than/);
expect(() => new StringAttr({ maxLen: 5000 }))
expect(() => new StringAttribute({ maxLen: 5000 }))
.toThrow(/maxLen cannot be greater than/);
});
});

describe('NumberAttr', () => {
describe('NumberAttribute', () => {
test('default', () => {
// GIVEN
const attr = new NumberAttr();
const attr = new NumberAttribute();

// WHEN
const bound = attr.bind();

// THEN
expect(bound.attrDataType).toEqual('Number');
expect(bound.dataType).toEqual('Number');
expect(bound.constraints).toBeUndefined();
});

test('specified constraints are recognized', () => {
// GIVEN
const attr = new NumberAttr({ min: 5, max: 600 });
const attr = new NumberAttribute({ min: 5, max: 600 });

// WHEN
const bound = attr.bind();
Expand All @@ -68,4 +68,32 @@ describe('User Pool Attributes', () => {
});
});
});

describe('BooleanAttribute', () => {
test('default', () => {
// GIVEN
const attr = new BooleanAttribute();

// WHEN
const bound = attr.bind();

// THEN
expect(bound.dataType).toEqual('Boolean');
expect(bound.constraints).toBeUndefined();
});
});

describe('DateTimeAttribute', () => {
test('default', () => {
// GIVEN
const attr = new DateTimeAttribute();

// WHEN
const bound = attr.bind();

// THEN
expect(bound.dataType).toEqual('DateTime');
expect(bound.constraints).toBeUndefined();
});
});
});
Loading

0 comments on commit b190402

Please sign in to comment.