Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -32,7 +32,7 @@ import AudienceEvaluator from '../audience_evaluator';
import errorHandler from '../../plugins/error_handler';
import eventBuilder from '../../core/event_builder/index.js';
import eventDispatcher from '../../plugins/event_dispatcher/index.node';
import jsonSchemaValidator from '../../utils/json_schema_validator';
import * as jsonSchemaValidator from '../../utils/json_schema_validator';
import {
getTestProjectConfig,
getTestProjectConfigWithFeatures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ERROR_MESSAGES, LOG_MESSAGES } from '../../utils/enums';
import testData from '../../tests/test_data';
import * as optimizelyConfig from '../optimizely_config/index';
import projectConfigManager from './project_config_manager';
import jsonSchemaValidator from '../../utils/json_schema_validator';
import * as jsonSchemaValidator from '../../utils/json_schema_validator';

describe('lib/core/project_config/project_config_manager', function() {
var globalStubErrorHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
/**
* Project Config JSON Schema file used to validate the project json datafile
*/
export var schema = {
import { JSONSchema4 } from 'json-schema';

var schemaDefinition = {
$schema: 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {
Expand Down Expand Up @@ -276,4 +278,6 @@ export var schema = {
},
};

export default schema;
const schema = schemaDefinition as JSONSchema4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to define the variable as having JSONSchema4 type in the first place, rather than coercing it here with as. Any issue doing it that way?

const schemaDefinition: JSONSchema4 = {
  // ...
};

Copy link
Contributor Author

@yavorona yavorona Jul 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjc1283 I agree, but I have tried it this way, and it complains Type '{ projectId: { type: "string"; required: true; }; accountId: ... ... }] is not assignable to type '{ [k: string]: JSONSchema4; }'. 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, I will create additional Jira tasks to fix/improve json schema validation and to document all the changes that we should do. I left const schema = schemaDefinition as JSONSchema4 so that all our unit tests can still pass without having to modify any of json schema files. FYI @mjc1283 @mikeng13


export { schema }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to keep the export default schema here since we will only have main export from this module.

Then down below instead of importing import { schema } .... you would simply import schema from ...

2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import errorHandler from '../plugins/error_handler';
import fns from '../utils/fns';
import logger from '../plugins/logger';
import decisionService from '../core/decision_service';
import jsonSchemaValidator from '../utils/json_schema_validator';
import * as jsonSchemaValidator from '../utils/json_schema_validator';
import projectConfig from '../core/project_config';
import testData from '../tests/test_data';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import { sprintf } from '@optimizely/js-sdk-utils';
import { validate as jsonSchemaValidator } from 'json-schema';

import { ERROR_MESSAGES } from '../enums';
import projectConfigSchema from '../../core/project_config/project_config_schema';
import { schema } from '../../core/project_config/project_config_schema';

var MODULE_NAME = 'JSON_SCHEMA_VALIDATOR';
const MODULE_NAME = 'JSON_SCHEMA_VALIDATOR';

/**
* Validate the given json object against the specified schema
* @param {Object} jsonObject The object to validate against the schema
* @return {Boolean} True if the given object is valid
* @param {object} jsonObject The object to validate against the schema
* @return {boolean} true if the given object is valid
*/
export var validate = function(jsonObject) {
export function validate(jsonObject: Record<string, unknown>): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of using Record... you wanna just use any so we can validate anything against this. An alternative type to use maybe would be JSON

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried any before , which gives Argument 'jsonObject' should be typed with a non-any type error. Did not think of JSON, thanks @mikeng13!

if (!jsonObject) {
throw new Error(sprintf(ERROR_MESSAGES.NO_JSON_PROVIDED, MODULE_NAME));
}

var result = jsonSchemaValidator(jsonObject, projectConfigSchema);
const result = jsonSchemaValidator(jsonObject, schema);
if (result.valid) {
return true;
} else {
Expand All @@ -42,8 +42,4 @@ export var validate = function(jsonObject) {
}
throw new Error(sprintf(ERROR_MESSAGES.INVALID_JSON, MODULE_NAME));
}
};

export default {
validate: validate,
};
}