Comparing version
{ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true, | ||
"source.organizeImports": true | ||
"source.fixAll": "explicit", | ||
"source.organizeImports": "explicit" | ||
}, | ||
@@ -6,0 +6,0 @@ "editor.tabSize": 2, |
@@ -5,2 +5,3 @@ "use strict"; | ||
const test_helpers_1 = require("./test-helpers"); | ||
const types_1 = require("./types"); | ||
describe('isBoolean', () => { | ||
@@ -25,2 +26,7 @@ it('incorrect type will cause an issue', () => { | ||
}); | ||
it('will check custom validator with custom issue returned', () => { | ||
const fut = (0, boolean_1.isBoolean)({ validator: (value) => value ? [types_1.Issue.forPath([], value, 'custom')] : true }); | ||
(0, test_helpers_1.expectSuccess)(fut, false); | ||
(0, test_helpers_1.expectIssue)(fut, true, 'custom'); | ||
}); | ||
}); | ||
@@ -27,0 +33,0 @@ describe('maybeBoolean', () => { |
@@ -1,2 +0,2 @@ | ||
import { IssueResult, Next, Path, Result, ValueProcessor } from './types'; | ||
import { Issue, IssueResult, Next, Path, Result, ValueProcessor } from './types'; | ||
export interface WithDefault<T> { | ||
@@ -25,3 +25,3 @@ default?: T | (() => T); | ||
export interface CommonValidationOptions<T> { | ||
validator?: (value: T, options?: any) => boolean; | ||
validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]; | ||
validatorOptions?: any; | ||
@@ -28,0 +28,0 @@ } |
@@ -117,9 +117,10 @@ "use strict"; | ||
var _a; | ||
const result = { issues: [] }; | ||
if (((_a = options.validator) === null || _a === void 0 ? void 0 : _a.call(options, value, options.validatorOptions)) === false) { | ||
result.issues.push(types_1.Issue.forPath(path, value, 'validator')); | ||
} | ||
return result; | ||
const customValidatorResult = (_a = options.validator) === null || _a === void 0 ? void 0 : _a.call(options, value, options.validatorOptions, path); | ||
if (customValidatorResult === undefined || customValidatorResult === true) | ||
return { issues: [] }; | ||
if (customValidatorResult === false) | ||
return { issues: [types_1.Issue.forPath(path, value, 'validator')] }; | ||
return { issues: customValidatorResult }; | ||
}; | ||
exports.basicValidation = basicValidation; | ||
//# sourceMappingURL=common.js.map |
@@ -8,3 +8,9 @@ import { TypeOf } from './../types'; | ||
export type SampleContract = TypeOf<typeof sample>; | ||
export type Sample = TypeOf<typeof sample>; | ||
interface Sample { | ||
maybeString?: string | undefined; | ||
myString: string; | ||
numericString: string; | ||
} | ||
type SameType<T, U> = T[] extends U[] ? U[] extends T[] ? true : false : false; | ||
export type ThisIsTrue = SameType<SampleContract, Sample>; | ||
export {}; |
@@ -110,2 +110,9 @@ "use strict"; | ||
}); | ||
it('will check with custom validator returning custom issues', () => { | ||
const fut = (0, object_1.isObject)({ | ||
a: (0, number_1.isNumber)({ min: 25 }), | ||
b: (0, string_1.isString)({ validator: (value, _options, path) => value === 'triggerCustom' ? [types_1.Issue.forPath(path !== null && path !== void 0 ? path : [], value, 'custom')] : true }), | ||
}); | ||
(0, test_helpers_1.expectIssue)(fut, { a: 47, b: 'triggerCustom' }, 'custom', ['b']); | ||
}); | ||
}); | ||
@@ -112,0 +119,0 @@ describe('maybeObject', () => { |
@@ -94,3 +94,3 @@ "use strict"; | ||
const fut = (0, record_1.asRecord)((0, string_1.isString)()); | ||
(0, test_helpers_1.expectValue)(fut, '{ \"fruit\": \"apple\" }', { fruit: 'apple' }); | ||
(0, test_helpers_1.expectValue)(fut, '{ "fruit": "apple" }', { fruit: 'apple' }); | ||
}); | ||
@@ -97,0 +97,0 @@ }); |
{ | ||
"name": "validata", | ||
"version": "5.0.6", | ||
"version": "5.0.7", | ||
"description": "Type safe data validation and sanitization", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -197,3 +197,3 @@ # Validata | ||
- `minLength?: number` - if there are less items than this, it's an error `min-length` | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -222,3 +222,3 @@ | ||
- `convertOptions` - options to pass to the _converter_ | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -244,3 +244,3 @@ | ||
- `maxPast?: Duration` - if the value is before this duration into the past, it's an error `max-past` | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -304,3 +304,3 @@ | ||
- `min?: number` - if the value is than this, it's an error `min` | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -324,3 +324,3 @@ | ||
- `convertOptions` - options to pass to the _converter_ | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -361,3 +361,3 @@ | ||
- `minKeys?: number` - if the number of keys in the object is more than this, it's an error `max-keys` | ||
- `validator?: (value: Record<string, V>, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: Record<string, V>, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -395,3 +395,3 @@ | ||
- `format:? StringFormatCheck` - extension point for string format checking, if check fails it's an issue `format` with `info.expectedFormat` set | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -474,3 +474,3 @@ | ||
- `validator?: (value: T, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -504,3 +504,3 @@ | ||
- `protocol?: string` - given URL must have this protocol, or it's an error `invalid-protocol` | ||
- `validator?: (value: URL, options?: any) => boolean` - custom validation function; if false is returned it's an error | ||
- `validator?: (value: URL, options?: any, path?: Path[]) => boolean | Issue[]` - custom validation function; if false or Issue[] is returned it's an error | ||
- `validatorOptions?: any` - options to pass to the _validator_ | ||
@@ -507,0 +507,0 @@ |
import { asBoolean, isBoolean, maybeAsBoolean, maybeBoolean } from './boolean'; | ||
import { expectIssue, expectSuccess, expectValue, runTests } from './test-helpers'; | ||
import { Issue } from './types'; | ||
@@ -36,2 +37,8 @@ describe('isBoolean', () => { | ||
}); | ||
it('will check custom validator with custom issue returned', () => { | ||
const fut = isBoolean({ validator: (value) => value ? [Issue.forPath([], value, 'custom')] : true }); | ||
expectSuccess(fut, false); | ||
expectIssue(fut, true, 'custom'); | ||
}); | ||
}); | ||
@@ -38,0 +45,0 @@ |
@@ -81,3 +81,3 @@ import { isIssue, Issue, IssueResult, Next, Path, Result, ValueProcessor } from './types'; | ||
export interface CommonValidationOptions<T> { | ||
validator?: (value: T, options?: any) => boolean; | ||
validator?: (value: T, options?: any, path?: Path[]) => boolean | Issue[]; | ||
validatorOptions?: any; | ||
@@ -189,7 +189,6 @@ } | ||
export const basicValidation = <T>(value: T, path: Path[], options: CommonValidationOptions<T>): IssueResult => { | ||
const result: IssueResult = { issues: [] }; | ||
if (options.validator?.(value, options.validatorOptions) === false) { | ||
result.issues.push(Issue.forPath(path, value, 'validator')); | ||
} | ||
return result; | ||
const customValidatorResult = options.validator?.(value, options.validatorOptions, path); | ||
if (customValidatorResult === undefined || customValidatorResult === true) return { issues: [] }; | ||
if (customValidatorResult === false) return { issues: [Issue.forPath(path, value, 'validator')] }; | ||
return { issues: customValidatorResult }; | ||
}; |
@@ -19,10 +19,10 @@ import { isArray } from '../array'; | ||
// both are same as | ||
export type SampleContract = TypeOf<typeof sample>; | ||
export type Sample = TypeOf<typeof sample>; | ||
// interface Sample { | ||
// myString: string; | ||
// maybeString: string | undefined; | ||
// numericString: string; | ||
// } | ||
interface Sample { | ||
maybeString?: string | undefined; | ||
myString: string; | ||
numericString: string; | ||
} | ||
type SameType<T, U> = T[] extends U[] ? U[] extends T[] ? true : false : false | ||
export type ThisIsTrue = SameType<SampleContract, Sample>; | ||
@@ -29,0 +29,0 @@ console.log(JSON.stringify(sample.process({ |
@@ -7,3 +7,3 @@ import { jsonDateParser } from 'json-date-parser'; | ||
import { expectIssue, expectSuccess, expectValue, runTests } from './test-helpers'; | ||
import { isIssue } from './types'; | ||
import { isIssue, Issue } from './types'; | ||
@@ -133,2 +133,10 @@ interface MyObject { | ||
}); | ||
it('will check with custom validator returning custom issues', () => { | ||
const fut = isObject<MyObject>({ | ||
a: isNumber({ min: 25 }), | ||
b: isString({ validator: (value, _options, path) => value === 'triggerCustom' ? [Issue.forPath(path ?? [], value, 'custom')] : true }), | ||
}); | ||
expectIssue(fut, { a: 47, b: 'triggerCustom' }, 'custom', ['b']); | ||
}); | ||
}); | ||
@@ -135,0 +143,0 @@ |
@@ -110,3 +110,3 @@ import { isAny } from './any'; | ||
const fut = asRecord(isString()); | ||
expectValue(fut, '{ \"fruit\": \"apple\" }', { fruit: 'apple' }); | ||
expectValue(fut, '{ "fruit": "apple" }', { fruit: 'apple' }); | ||
}); | ||
@@ -113,0 +113,0 @@ }); |
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
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
643357
0.68%7521
0.43%