How to use the class-validator.getFromContainer function in class-validator

To help you get started, we’ve selected a few class-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NationalBankBelgium / stark / packages / stark-core / src / validation / validators / array-size-range / array-size-range.validator.fn.ts View on Github external
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;
}
github epiphone / routing-controllers-openapi / sample / 01-basic / app.ts View on Github external
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'
      }
    }
  },
github skmdev / koa-decorator-ts / src / decorators / router.ts View on Github external
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;
};
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / is-company-number / is-company-number.validator.decorator.ts View on Github external
public validate(companyNumber: string): boolean {
		const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
		return validator.starkIsCompanyNumber(companyNumber);
	}
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / map-not-empty / map-not-empty.validator.decorator.ts View on Github external
public validate(map: Map): boolean {
		const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
		return validator.starkMapNotEmpty(map);
	}
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / is-nin / is-nin.validator.decorator.ts View on Github external
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] : "");
	}
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / is-iban / is-iban.validator.decorator.ts View on Github external
public validate(iban: string): boolean {
		const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
		return validator.starkIsIBAN(iban);
	}
github EndyKaufman / ngx-dynamic-form-builder / libs / ngx-dynamic-form-builder / src / shared / utils / dynamic-form-group.ts View on Github external
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]
github NationalBankBelgium / stark / packages / stark-core / src / validation / decorators / is-kbo / is-kbo.validator.decorator.ts View on Github external
public validate(kbo: string): boolean {
		const validator: StarkValidator = getFromContainer(StarkValidatorImpl);

		return validator.starkIsKBO(kbo);
	}