ajv#ValidateFunction TypeScript Examples
The following examples show how to use
ajv#ValidateFunction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: actions.ts From UsTaxes with GNU Affero General Public License v3.0 | 6 votes |
makePreprocessActionCreator =
<A, AA, T extends ActionName>(
t: T,
validate: ValidateFunction<AA> | undefined,
clean: (d: A) => AA
) =>
(formData: A) =>
(year: TaxYear): Save<T, AA> => ({
type: t,
year,
formData:
validate !== undefined
? validators.checkType(clean(formData), validate)
: { ...formData, ...clean(formData) }
})
Example #2
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 6 votes |
checkType = <A>(data: A, validate: ValidateFunction<A>): A => {
validate(data)
if ((validate.errors ?? undefined) !== undefined) {
// Taken from doc example: The type cast is needed to allow user-defined keywords and errors
// You can extend this type to include your error types as needed.
const errs = validate.errors as DefinedError[]
for (const err of errs) {
log.error(err.message)
}
log.error(validate.errors)
log.error(data)
const errorMessage =
validate.errors
?.map((e) => `${e.instancePath}: ${e.message ?? ''}`)
.join('\n') ?? 'Unknown error'
validate.errors?.forEach(console.error)
throw new Error(`Validation Failed: ${errorMessage}`)
}
return data
}
Example #3
Source File: generate.ts From iuliia-js with MIT License | 6 votes |
async function jsonToTS(inputFile: string, v: ValidateFunction): Promise<string> {
const input = await readFile(inputFile, "utf8");
const data = JSON.parse(input);
if (!v(data)) {
console.error(`Validation failed for file ${inputFile}:`);
console.error(v.errors);
process.exit(1);
}
return (
"import {TransliterationSchema} from './TransliterationSchema';\n\n" +
`export default ${JSON.stringify(data)} as TransliterationSchema;`
);
}
Example #4
Source File: index.ts From integration-services with Apache License 2.0 | 6 votes |
validateUser(user: User) {
let validate: ValidateFunction;
switch (user.claim?.type) {
case UserType.Person:
validate = this.ajv.getSchema('person');
break;
case UserType.Device:
validate = this.ajv.getSchema('device');
break;
case UserType.Organization:
validate = this.ajv.getSchema('organization');
break;
case UserType.Product:
validate = this.ajv.getSchema('product');
break;
case UserType.Service:
validate = this.ajv.getSchema('service');
break;
default:
break;
}
if (!validate) {
this.logger.log(`no schema found for user type: ${user.claim?.type}`);
return;
}
if (user.claim) {
const validDetails = <boolean>validate(user.claim);
if (!validDetails) {
throw new Error('no valid identity claim');
}
}
}
Example #5
Source File: ajv.ts From backstage with Apache License 2.0 | 6 votes |
// Compiles the given schema, and makes sure to also grab any core dependencies
// that it depends on
export function compileAjvSchema(
schema: Schema,
options: { disableCache?: boolean } = {},
): ValidateFunction<unknown> {
const disableCache = options?.disableCache ?? false;
const cacheKey = disableCache ? '' : JSON.stringify(schema);
if (!disableCache) {
const cached = compiledSchemaCache.get(cacheKey);
if (cached) {
return cached;
}
}
const extraSchemas = getExtraSchemas(schema);
const ajv = new Ajv({
allowUnionTypes: true,
allErrors: true,
validateSchema: true,
});
if (extraSchemas.length) {
ajv.addSchema(extraSchemas, undefined, undefined, true);
}
const compiled = ajv.compile(schema);
if (!disableCache) {
compiledSchemaCache.set(cacheKey, compiled);
}
return compiled;
}
Example #6
Source File: ajv.ts From backstage with Apache License 2.0 | 6 votes |
export function throwAjvError(
errors: ValidateFunction<unknown>['errors'],
): never {
if (!errors?.length) {
throw new TypeError('Unknown error');
}
const error = errors[0];
throw new TypeError(
`${error.instancePath || '<root>'} ${error.message}${
error.params
? ` - ${Object.entries(error.params)
.map(([key, val]) => `${key}: ${val}`)
.join(', ')}`
: ''
}`,
);
}
Example #7
Source File: validation.middleware.ts From server-api with Apache License 2.0 | 6 votes |
async function validateRequestBody(validate: ValidateFunction, body: any) {
const valid = validate(body);
const errors = validate.errors && [...validate.errors];
if (valid === false) {
throw new ProblemException({
type: 'invalid-request-body',
title: 'Invalid Request Body',
status: 422,
validationErrors: errors as any,
});
}
}
Example #8
Source File: actions.ts From UsTaxes with GNU Affero General Public License v3.0 | 6 votes |
makeActionCreator =
<A, T extends ActionName>(t: T, validate?: ValidateFunction<A>) =>
(formData: A) =>
(year: TaxYear): Save<T, A> => ({
type: t,
year,
formData:
validate !== undefined
? validators.checkType<A>(formData, validate)
: formData
})
Example #9
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
income1099Int =
fns.Income1099Int as ValidateFunction<types.Income1099Int>
Example #10
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
refund = fns.Refund as ValidateFunction<types.Refund>
Example #11
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
estimatedTaxPayments =
fns.EstimatedTaxPayments as ValidateFunction<types.EstimatedTaxPayments>
Example #12
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
taxPayer =
fns.TaxPayer as ValidateFunction<types.TaxPayerDateString>
Example #13
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
information =
fns.Information as ValidateFunction<types.InformationDateString>
Example #14
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
property = fns.Property as ValidateFunction<types.Property>
Example #15
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
propertyType =
fns.PropertyType as ValidateFunction<types.PropertyType>
Example #16
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
f1098e = fns.F1098e as ValidateFunction<types.F1098e>
Example #17
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
itemizedDeductions =
fns.ItemizedDeductions as ValidateFunction<types.ItemizedDeductions>
Example #18
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
responses = fns.Responses as ValidateFunction<types.Responses>
Example #19
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
stateResidency =
fns.StateResidency as ValidateFunction<types.StateResidency>
Example #20
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
healthSavingsAccount =
fns.HealthSavingsAccount as ValidateFunction<types.HealthSavingsAccountDateString>
Example #21
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
ira = fns.Ira as ValidateFunction<types.Ira>
Example #22
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
assetType = fns.AssetType as ValidateFunction<types.AssetType>
Example #23
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
assetString =
fns.AssetString as ValidateFunction<types.AssetString>
Example #24
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
taxYear = fns.TaxYear as ValidateFunction<types.TaxYear>
Example #25
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
credit = fns.Credit as ValidateFunction<types.Credit>
Example #26
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
editIraAction =
fns.EditIRAAction as ValidateFunction<types.EditIraAction>
Example #27
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
editHSAAction =
fns.EditHSAAction as ValidateFunction<types.EditHSAAction>
Example #28
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
editCreditAction =
fns.EditCreditAction as ValidateFunction<types.EditCreditAction>
Example #29
Source File: validate.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
filingStatus =
fns.FilingStatus as ValidateFunction<types.FilingStatus>