Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@validointi/core

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@validointi/core

Model driven validation for Angular template forms

  • 1.6.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@validointi/core

This is a library to help you validate your template driven forms and seperate validation logic from your ui logic

Installation

To install this library, run:

$ npm install @validointi/core --save

Using it in your project.

You can use this library by importing the directives and services in the components you want to use them in.

First you register your validation function to the service like this:

#vr = inject(ValidatorRegistryService);

validate = this.#vr.registerValidator('sample-data', validateSampleData);

a validation function can look like this:

async function validateSampleData(data: SampleData, field?: string): Promise<ValidationErrors> {
  /**
   * In here we use Vest to validate the data.
   * However, you can validate the data however you want.
   */
  const errors = await suite(data, field).getErrors();
  return Object.entries(errors).reduce((acc, [key, err]) => {
    acc[key] = err;
    return acc;
  }, {} as ValidationErrors);
}

Pro Tip: When you use vest, you can use the createVestAdapter(suite) function to create a validation function that can be used with this library.

validate = this.#vr.registerValidator('sample-data', createVestAdapter(suite));

will make sure the vest results are formatted correctly for this library.

The only restriction is that the function must return a Promise<ValidationErrors>.

After that you can use the directive in your template like this:

<form validationId="sample-data" (ngSubmit)="mySubmitFunction($any(data))" #form="ngForm">
  <label for="name">
    <span>Name</span>
    <input type="text" name="name" placeholder="Your name" [(ngModel)]="data.name" />
  </label>
  <input type="submit" value="Submit" [disabled]="form.invalid" />
</form>

From this point the form will be validated on formControl.valueChanges and errors coming from the function will be set on the corresponding formControl.

Keywords

FAQs

Package last updated on 20 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc