Comparing version 1.0.1 to 1.1.0
@@ -5,3 +5,3 @@ export declare class ExtendableError extends Error { | ||
export interface FefeChildError { | ||
key: string | number; | ||
key: string | number | symbol; | ||
error: FefeError; | ||
@@ -13,6 +13,6 @@ } | ||
readonly child?: FefeChildError; | ||
readonly path: (string | number)[]; | ||
readonly path: (string | number | symbol)[]; | ||
readonly originalError: FefeError; | ||
constructor(value: any, reason: string, child?: FefeChildError); | ||
createParentError(parentValue: any, key: string | number): FefeError; | ||
createParentError(parentValue: any, key: string | number | symbol): FefeError; | ||
} |
@@ -8,3 +8,3 @@ export { FefeError } from './errors'; | ||
export { number } from './number'; | ||
export { object } from './object'; | ||
export { object, optional } from './object'; | ||
export { parseBoolean } from './parse-boolean'; | ||
@@ -11,0 +11,0 @@ export { parseDate } from './parse-date'; |
@@ -17,2 +17,3 @@ "use strict"; | ||
exports.object = object_1.object; | ||
exports.optional = object_1.optional; | ||
var parse_boolean_1 = require("./parse-boolean"); | ||
@@ -19,0 +20,0 @@ exports.parseBoolean = parse_boolean_1.parseBoolean; |
@@ -15,2 +15,3 @@ import { Validator } from './validate'; | ||
}): (value: unknown) => { [k in keyof D]: ObjectReturnType<D[k]>; }; | ||
export declare function optional<R>(validator: Validator<R>): ObjectOptions<R>; | ||
export {}; |
@@ -52,2 +52,9 @@ "use strict"; | ||
exports.object = object; | ||
function optional(validator) { | ||
return { | ||
validator, | ||
optional: true | ||
}; | ||
} | ||
exports.optional = optional; | ||
//# sourceMappingURL=object.js.map |
@@ -53,2 +53,9 @@ "use strict"; | ||
}); | ||
describe('optional()', () => { | ||
it('should return an optional object options object', () => { | ||
const validator = string_1.string(); | ||
const options = object_1.optional(validator); | ||
chai_1.expect(options).to.eql({ validator, optional: true }); | ||
}); | ||
}); | ||
//# sourceMappingURL=object.test.js.map |
{ | ||
"name": "fefe", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Validate, sanitize and transform values with proper types.", | ||
@@ -41,14 +41,14 @@ "main": "dist/index.js", | ||
"@types/chai": "^4.1.7", | ||
"@types/mocha": "^5.2.5", | ||
"@types/ramda": "^0.25.45", | ||
"@types/mocha": "^5.2.7", | ||
"@types/ramda": "^0.26.10", | ||
"chai": "^4.2.0", | ||
"codecov": "^3.1.0", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0", | ||
"codecov": "^3.5.0", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"ramda": "^0.26.1", | ||
"ts-node": "^7.0.1", | ||
"tslint": "^5.12.0", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.18.0", | ||
"tslint-config-standard": "^8.0.1", | ||
"typescript": "^3.2.2" | ||
"typescript": "^3.5.2" | ||
} | ||
} |
@@ -9,6 +9,6 @@ # fefe | ||
**π Validation:** checks a value (example: check if value is string)<br/> | ||
**β Sanitization:** if a value is not valid, try to transform it (example: transform value to `Date`)<br/> | ||
**π οΈ Transformation:** transforms a value (example: parse JSON)<br/> | ||
**π Everything is a function**: functional approach makes it easy to extend β just plug in your own function anywhere! | ||
**π Validation:** checks a value (example: check if value is string)<br/> | ||
**:nut_and_bolt: Sanitization:** if a value is not valid, try to transform it (example: transform value to `Date`)<br/> | ||
**π οΈ Transformation:** transforms a value (example: parse JSON)<br/> | ||
**π Everything is a function**: functional approach makes it easy to extend β just plug in your own function anywhere! | ||
@@ -157,3 +157,3 @@ ## Installation | ||
* an object with the following properties: | ||
* `validate`: validator function `(value: unknown) => T` | ||
* `validator`: validator function `(value: unknown) => T` | ||
* `optional?`: allow undefined values (default: `false`) | ||
@@ -160,0 +160,0 @@ * `default?`: default value of type `T` or function `() => T` that returns a default value |
@@ -9,3 +9,3 @@ export class ExtendableError extends Error { | ||
export interface FefeChildError { | ||
key: string | number | ||
key: string | number | symbol | ||
error: FefeError | ||
@@ -20,3 +20,3 @@ } | ||
// derived properties | ||
public readonly path: (string | number)[] | ||
public readonly path: (string | number | symbol)[] | ||
public readonly originalError: FefeError | ||
@@ -34,3 +34,3 @@ | ||
createParentError (parentValue: any, key: string | number) { | ||
createParentError (parentValue: any, key: string | number | symbol) { | ||
const child: FefeChildError = { key, error: this } | ||
@@ -37,0 +37,0 @@ return new FefeError(parentValue, this.reason, child) |
@@ -9,3 +9,3 @@ export { FefeError } from './errors' | ||
export { number } from './number' | ||
export { object } from './object' | ||
export { object, optional } from './object' | ||
export { parseBoolean } from './parse-boolean' | ||
@@ -12,0 +12,0 @@ export { parseDate } from './parse-date' |
import { expect } from 'chai' | ||
import { FefeError } from './errors' | ||
import { object } from './object' | ||
import { object, optional, ObjectOptions } from './object' | ||
import { string } from './string' | ||
@@ -60,1 +60,9 @@ | ||
}) | ||
describe('optional()', () => { | ||
it('should return an optional object options object', () => { | ||
const validator = string() | ||
const options: ObjectOptions<string> = optional(validator) | ||
expect(options).to.eql({ validator, optional: true }) | ||
}) | ||
}) |
@@ -40,3 +40,3 @@ import { FefeError } from './errors' | ||
const validated = {} as {[k in keyof D]: ObjectReturnType<D[k]>} | ||
Object.entries(definition).forEach(([key, definitionValue]) => { | ||
Object.entries(definition).forEach(([key, definitionValue]: [keyof D, ObjectDefinitionValue<any>]) => { | ||
const options: ObjectOptions<any> = typeof definitionValue === 'object' ? | ||
@@ -71,1 +71,8 @@ definitionValue : | ||
} | ||
export function optional<R> (validator: Validator<R>): ObjectOptions<R> { | ||
return { | ||
validator, | ||
optional: true | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
97185
1480