Skip to content

feat: Convert attributes_validator to TS #530

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

Merged
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 @@ -18,7 +18,7 @@ import { getLogger } from '@optimizely/js-sdk-logging';
import fns from '../../utils/fns';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributesValidator from'../../utils/attributes_validator';
import * as attributesValidator from'../../utils/attributes_validator';

var logger = getLogger('EVENT_BUILDER');

Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import fns from '../../utils/fns';
import enums from '../../utils/enums';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributeValidator from '../../utils/attributes_validator';
import * as attributeValidator from '../../utils/attributes_validator';

var ACTIVATE_EVENT_KEY = 'campaign_activated';
var CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { assert } from 'chai';
import { sprintf } from '@optimizely/js-sdk-utils';

import attributesValidator from './';
import * as attributesValidator from './';
import { ERROR_MESSAGES } from '../enums';

describe('lib/utils/attributes_validator', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import { sprintf } from '@optimizely/js-sdk-utils';
import fns from '../../utils/fns';
import { ERROR_MESSAGES } from '../enums';

var MODULE_NAME = 'ATTRIBUTES_VALIDATOR';
const MODULE_NAME = 'ATTRIBUTES_VALIDATOR';

/**
* Validates user's provided attributes
* @param {Object} attributes
* @return {boolean} True if the attributes are valid
* @param {unknown} attributes
* @return {boolean} true if the attributes are valid
* @throws If the attributes are not valid
*/
export var validate = function(attributes) {

export function validate(attributes: unknown): boolean {
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
Object.keys(attributes).forEach(function(key) {
if (typeof attributes[key] === 'undefined') {
Expand All @@ -37,21 +38,19 @@ export var validate = function(attributes) {
} else {
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ATTRIBUTES, MODULE_NAME));
}
};
}

export var isAttributeValid = function(attributeKey, attributeValue) {
/**
* Validates user's provided attribute
* @param {unknown} attributeKey
* @param {unknown} attributeValue
* @return {boolean} true if the attribute is valid
*/
export function isAttributeValid(attributeKey: unknown, attributeValue: unknown): boolean {
return (
typeof attributeKey === 'string' &&
(typeof attributeValue === 'string' ||
typeof attributeValue === 'boolean' ||
(fns.isNumber(attributeValue) && fns.isSafeInteger(attributeValue)))
);
};

/**
* Provides utility method for validating that the attributes user has provided are valid
*/
export default {
validate: validate,
isAttributeValid: isAttributeValid,
};
}