@financial-times/dotcom-server-app-context
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -10,4 +10,7 @@ "use strict"; | ||
constructor(options = {}) { | ||
this.data = filterEmptyData_1.default({ ...options.context }); | ||
this.validate(); | ||
this.data = {}; | ||
const data = filterEmptyData_1.default({ ...options.context }); | ||
for (const [property, value] of Object.entries(data)) { | ||
this.set(property, value); | ||
} | ||
} | ||
@@ -18,8 +21,10 @@ get(property) { | ||
set(property, value) { | ||
this.data[property] = value; | ||
if (validate_1.default(property, value)) { | ||
this.data[property] = value; | ||
} | ||
} | ||
validate() { | ||
return validate_1.default(this.data); | ||
getAll() { | ||
return Object.freeze({ ...this.data }); | ||
} | ||
} | ||
exports.AppContext = AppContext; |
@@ -10,4 +10,5 @@ "use strict"; | ||
const isValid = ajv.compile(schema_json_1.default); | ||
function validate(contextData) { | ||
if (isValid(contextData)) { | ||
function validate(field, value) { | ||
const data = { [field]: value }; | ||
if (isValid(data)) { | ||
return true; | ||
@@ -14,0 +15,0 @@ } |
{ | ||
"name": "@financial-times/dotcom-server-app-context", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/node/index.js", |
@@ -23,3 +23,3 @@ # @financial-times/dotcom-server-app-context | ||
The app context instance provides methods to get, set, and validate context data: | ||
The app context instance provides methods to get and set app context data: | ||
@@ -29,8 +29,3 @@ ```js | ||
const property = appContext.get('appName') // "my-application" | ||
try { | ||
appContext.validate() | ||
} catch (error) { | ||
console.error('Application context data is invalid:', error) | ||
} | ||
const contextData = appContext.getAll() // { appName: "my-application" } | ||
``` | ||
@@ -47,7 +42,7 @@ | ||
Sets the value of the specified property. | ||
Sets the value of the specified property. The provided value will be validated against the [app context schema](#app-context-data). If the value is invalid this method will throw an error. | ||
### `validate(): boolean` | ||
### `getAll(): object` | ||
Validates the current data against the schema definition. If the data is invalid this method will throw an error with details of the first error encountered. | ||
Returns an immutable copy of the app context data. | ||
@@ -54,0 +49,0 @@ |
@@ -10,7 +10,10 @@ import { TAppContext } from './types' | ||
export class AppContext { | ||
public data: Partial<TAppContext> | ||
private data: Partial<TAppContext> = {} | ||
constructor(options: TAppContextOptions = {}) { | ||
this.data = filterEmptyData({ ...options.context }) | ||
this.validate() | ||
const data = filterEmptyData({ ...options.context }) | ||
for (const [ property, value ] of Object.entries(data)) { | ||
this.set(property, value) | ||
} | ||
} | ||
@@ -23,8 +26,10 @@ | ||
set(property: string, value: any) { | ||
this.data[property] = value | ||
if (validate(property, value)) { | ||
this.data[property] = value | ||
} | ||
} | ||
validate() { | ||
return validate(this.data) | ||
getAll(): Partial<TAppContext> { | ||
return Object.freeze({ ...this.data }) | ||
} | ||
} |
@@ -8,4 +8,6 @@ import Ajv from 'ajv' | ||
export default function validate(contextData): boolean { | ||
if (isValid(contextData)) { | ||
export default function validate(field: string, value): boolean { | ||
const data = { [field]: value } | ||
if (isValid(data)) { | ||
return true | ||
@@ -12,0 +14,0 @@ } else { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14142
282
59