New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

aspnet-validation

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aspnet-validation

Enables ASP.NET Core MVC client-side validation, without JQuery!

  • 0.0.1-beta2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
66
decreased by-25.84%
Maintainers
1
Weekly downloads
 
Created
Source

aspnet-validation

Enables ASP.NET Core MVC client-side validation, without JQuery!

npm

Install

npm install aspnet-validation es6-promise

aspnet-validation uses Promise API, which is not supported in Internet Explorer. It is recommended to use promise-polyfill or es6-promise to resolve this issue...

Alternatively, download these:

<script src="aspnet-validation.min.js"></script>

Quick Start Guide

Via <script src="...">

let v = new window.aspnetValidation.ValidationService();
v.bootstrap();

Via CommonJS

const v = require('aspnet-validation');
let v = new ValidationService();
v.bootstrap();

Via ES6 Module

import { ValidationService } from 'aspnet-validation';
let v = new ValidationService();
v.bootstrap();

Shameless promotion: use instapack for painless TypeScript development!

Why?

jQuery + jQuery Validation + jQuery Validation Unobtrusive: 67.9 KB + 22.5 KB + 5.14 KB = 95.6 KB

aspnet-validation: 9.81 KB (10.26%)

  • promise-polyfill: +2.58 KB = 12.4 KB (12.97%)
  • es6-promise: +6.19 KB = 19 KB (19.87%)

Adding Custom Validation

Example stolen from https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation

public class ClassicMovieAttribute : ValidationAttribute, IClientModelValidator
{
    private int _year;

    public ClassicMovieAttribute(int Year)
    {
        _year = Year;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        Movie movie = (Movie)validationContext.ObjectInstance;

        if (movie.Genre == Genre.Classic && movie.ReleaseDate.Year > _year)
        {
            return new ValidationResult(GetErrorMessage());
        }

        return ValidationResult.Success;
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        MergeAttribute(context.Attributes, "data-val", "true");
        MergeAttribute(context.Attributes, "data-val-classicmovie", GetErrorMessage());

        var year = _year.ToString(CultureInfo.InvariantCulture);
        MergeAttribute(context.Attributes, "data-val-classicmovie-year", year);
    }
}
import { ValidationService } from 'aspnet-validation';
let v = new ValidationService();

// Must be done BEFORE bootstrap() is called!
v.addProvider('classicmovie', (value, element, params) => {
    if (!value) {
        // let [Required] handle validation error for empty input!
        return true;
    }

    var genre = (document.getElementById('Genre') as HTMLSelectElement).value;
    
    // data-val-classicmovie-year is bound automatically to params. Cool huh?
    let year = parseInt(params.year);
    let date = new Date(value);

    if (genre && genre.length > 0 && genre[0] === '0') {
        return date.getFullYear() <= year;
    }

    return true;
});

v.bootstrap();

Adding Custom Asynchronous Validation

Other than boolean and string, addProvider callback accepts Promise<string | boolean> as return value:

v.addProvider('io', (value, element, params) => {
    if (!value) {
        return true;
    }

    return async () => {
        let result: number = await Some_IO_Operation(value);
        return result > 0;
    };
});

FAQs

Package last updated on 21 Jul 2017

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