Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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 default schema
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: JSON): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

JSON isn't the right type for this. This JSON is the global JSON object with methods parse and stringify. See here.

I think the right type for this is {}, an empty object with no properties. This is what's expected as the jsonSchemaValidate function's first argument.

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 Here is the eslint error from using {} (which is not intuitive at all IMO):

error  Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.
- If you want a type meaning "any value", you probably want `unknown` instead  @typescript-eslint/ban-types

Looks like Omit<Record<any, never>, keyof any> can be used for explicitly empty object or something less strict like Record<string, never> . Any thoughts @mjc1283?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm yea, this is forbidden by our lint rules. Actually I'm not sure why - I'm going to research it. Let's change it to unknown.

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,
};
}