Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function starkArraySizeRange(array: any[], minSize?: number, maxSize?: number): boolean {
// by default, if the requested validator class is not already registered in the container from the 'class-validator' container
// then it will registered automatically so it will indeed be a singleton
// IMPORTANT: the StarkValidatorImpl should be used in the same way wherever needed to avoid instantiating it multiple times!
const validator = getFromContainer(Validator);
let isValid = true;
if (typeof minSize !== "undefined") {
isValid = isValid && validator.arrayMinSize(array, minSize);
}
if (typeof maxSize !== "undefined") {
isValid = isValid && validator.arrayMaxSize(array, maxSize);
}
return isValid;
}
import {
createExpressServer,
getMetadataArgsStorage
} from 'routing-controllers'
import { routingControllersToSpec } from 'routing-controllers-openapi'
import { UsersController } from './UsersController'
const routingControllersOptions = {
controllers: [UsersController],
routePrefix: '/api'
}
const app: Express = createExpressServer(routingControllersOptions)
// Parse class-validator classes into JSON Schema:
const metadatas = (getFromContainer(MetadataStorage) as any).validationMetadatas
const schemas = validationMetadatasToSchemas(metadatas, {
refPointerPrefix: '#/components/schemas/'
})
// Parse routing-controllers classes into OpenAPI spec:
const storage = getMetadataArgsStorage()
const spec = routingControllersToSpec(storage, routingControllersOptions, {
components: {
schemas,
securitySchemes: {
basicAuth: {
scheme: 'basic',
type: 'http'
}
}
},
const GetSchemaFromType = (Type: new () => any) => {
const { name } = Type;
const cached = SCHEMA_CACHE[name];
if (cached) {
return cached;
}
const metadatas = (getFromContainer(MetadataStorage) as any)
.validationMetadatas;
const allSchemas = validationMetadatasToSchemas(metadatas);
const schema = allSchemas[name];
if (!schema) {
console.error(`No schema found for ${name}`);
} else {
SCHEMA_CACHE[name] = schema;
}
return schema;
};
public validate(companyNumber: string): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
return validator.starkIsCompanyNumber(companyNumber);
}
public validate(map: Map): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
return validator.starkMapNotEmpty(map);
}
public validate(nin: string, validationArguments?: ValidationArguments): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
const constraint: string = validationArguments && validationArguments.constraints[0] ? validationArguments.constraints[0] : "";
return validator.starkIsNIN(nin, validationArguments ? validationArguments.object[constraint] : "");
}
public validate(iban: string): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
return validator.starkIsIBAN(iban);
}
static getClassValidators(
factoryModel: ClassType,
fields: {
[key: string]: any
},
validatorOptions?: ValidatorOptions
) {
const validationMetadatas: ValidationMetadata[] =
getFromContainer(MetadataStorage).getTargetValidationMetadatas(
factoryModel,
'',
validatorOptions && validatorOptions.groups ? validatorOptions.groups : undefined
);
const formGroupFields = {};
const validator = new Validator();
Object.keys(fields).filter(key => key.indexOf('__') !== 0).forEach(key => {
let formGroupField = formGroupFields[key];
if (formGroupField === undefined) {
formGroupField = Array.isArray(fields[key]) ? fields[key] : [];
}
if (!Array.isArray(fields[key])) {
if (fields[key] instanceof DynamicFormGroup) {
fields[key].object = fields[key].fields;
formGroupField.push(
fields[key]
public validate(kbo: string): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
return validator.starkIsKBO(kbo);
}