<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>InputValidation Archives - scmGalaxy</title>
	<atom:link href="https://www.scmgalaxy.com/tutorials/tag/inputvalidation/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.scmgalaxy.com/tutorials/tag/inputvalidation/</link>
	<description>A Platform for Software Engineers</description>
	<lastBuildDate>Sat, 21 Feb 2026 07:13:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>AspNetCore.Scalar Explained: Modern Input Validation and Binding for .NET Web Apps</title>
		<link>https://www.scmgalaxy.com/tutorials/aspnetcore-scalar-explained-modern-input-validation-and-binding-for-net-web-apps/</link>
					<comments>https://www.scmgalaxy.com/tutorials/aspnetcore-scalar-explained-modern-input-validation-and-binding-for-net-web-apps/#respond</comments>
		
		<dc:creator><![CDATA[Vijay K]]></dc:creator>
		<pubDate>Mon, 28 Apr 2025 07:19:11 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AspNetCore]]></category>
		<category><![CDATA[AspNetCoreScalar]]></category>
		<category><![CDATA[CleanArchitecture]]></category>
		<category><![CDATA[DOTNET]]></category>
		<category><![CDATA[InputValidation]]></category>
		<category><![CDATA[ModelBinding]]></category>
		<category><![CDATA[SecureCoding]]></category>
		<category><![CDATA[WebAPI]]></category>
		<category><![CDATA[WebDevelopment]]></category>
		<guid isPermaLink="false">https://www.scmgalaxy.com/tutorials/?p=12175</guid>

					<description><![CDATA[<p>📌 What is AspNetCore.Scalar? AspNetCore.Scalar is a lightweight, extensible, and highly-focused library for ASP.NET Core applications that simplifies model binding, [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/aspnetcore-scalar-explained-modern-input-validation-and-binding-for-net-web-apps/">AspNetCore.Scalar Explained: Modern Input Validation and Binding for .NET Web Apps</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="576" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/AspNetCore.Scalar-Explained-1024x576.jpg" alt="" class="wp-image-12176" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/AspNetCore.Scalar-Explained-1024x576.jpg 1024w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/AspNetCore.Scalar-Explained-300x169.jpg 300w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/AspNetCore.Scalar-Explained-768x432.jpg 768w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/AspNetCore.Scalar-Explained.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">📌 What is AspNetCore.Scalar?</h2>



<p class="wp-block-paragraph"><strong>AspNetCore.Scalar</strong> is a <strong>lightweight</strong>, <strong>extensible</strong>, and <strong>highly-focused</strong> library for <strong>ASP.NET Core</strong> applications that simplifies <strong>model binding</strong>, <strong>input validation</strong>, and <strong>data sanitization</strong> at the property level.</p>



<p class="wp-block-paragraph">It introduces the concept of <strong>&#8220;Scalars&#8221;</strong>, which are <strong>strongly-typed wrappers</strong> around primitive data types like strings, integers, booleans, etc., adding value like automatic validation, normalization, and rich error handling — all without cluttering your core domain models.</p>



<p class="wp-block-paragraph">In simple terms:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><strong>AspNetCore.Scalar helps you validate and normalize user input early, safely, and elegantly in your ASP.NET Core Web APIs and MVC apps.</strong></p>
</blockquote>



<p class="wp-block-paragraph">Instead of spreading validation logic across your controllers, services, or view models, <strong>Scalar</strong> centralizes and automates it, keeping your application <strong>clean</strong>, <strong>secure</strong>, and <strong>easy to maintain</strong>.</p>



<hr class="wp-block-separator has-alpha-channel-opacity">



<h2 class="wp-block-heading">📌 Major Use Cases of AspNetCore.Scalar</h2>



<p class="wp-block-paragraph">You can use <strong>AspNetCore.Scalar</strong> in a wide range of modern ASP.NET Core application scenarios:</p>



<ul class="wp-block-list">
<li><strong>Web API Input Validation</strong><br>Validate user-provided API request fields (e.g., email addresses, usernames, phone numbers) early during model binding.</li>



<li><strong>Sanitize User Input</strong><br>Normalize or clean up raw data inputs like trimming whitespaces, enforcing case rules (lowercase, uppercase), or formatting numbers before they hit your application logic.</li>



<li><strong>Microservices Input Contracts</strong><br>Enforce strict input contracts between services by making sure incoming data follows expected formats and constraints.</li>



<li><strong>Security Hardening</strong><br>Prevent injection attacks and other input-related vulnerabilities by validating and sanitizing at the first point of entry.</li>



<li><strong>Domain-Driven Design (DDD)</strong><br>Encapsulate validation rules inside value objects without bloating the domain entities.</li>



<li><strong>Developer Productivity</strong><br>Reduce repetitive validation code and avoid cluttering controllers and DTOs with manual <code>if-else</code> checks.</li>



<li><strong>Error Reporting</strong><br>Provide clear and structured error messages on invalid user input automatically.</li>
</ul>



<p class="wp-block-paragraph">Overall, <strong>AspNetCore.Scalar</strong> is ideal for any project where <strong>input validation</strong>, <strong>data quality</strong>, and <strong>code maintainability</strong> are top priorities.</p>



<hr class="wp-block-separator has-alpha-channel-opacity">



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="564" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/image-20-1024x564.png" alt="" class="wp-image-12177" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/image-20-1024x564.png 1024w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/image-20-300x165.png 300w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/image-20-768x423.png 768w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2025/04/image-20.png 1192w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">📌 How AspNetCore.Scalar Works (Architecture Overview)</h2>



<p class="wp-block-paragraph">AspNetCore.Scalar operates through a simple but powerful architectural flow:</p>



<h3 class="wp-block-heading">🔹 Core Components:</h3>



<ul class="wp-block-list">
<li><strong>Scalar Types</strong><br>Custom classes that wrap a primitive type (like <code>Email</code>, <code>PhoneNumber</code>, <code>Username</code>).</li>



<li><strong>Scalar Model Binders</strong><br>Intercept incoming HTTP request data during the ASP.NET Core model binding pipeline and instantiate scalar types automatically.</li>



<li><strong>Scalar Validators</strong><br>Run predefined or custom validation rules (length checks, format validation, etc.) on the bound input data.</li>



<li><strong>Scalar Normalizers</strong><br>Optionally sanitize/transform input (e.g., trimming spaces, converting to lowercase).</li>



<li><strong>Integration with ModelState</strong><br>Scalar errors are pushed automatically into <code>ModelState</code> for seamless error reporting in APIs or MVC views.</li>
</ul>



<h3 class="wp-block-heading">🛠️ Internal Workflow:</h3>



<ol class="wp-block-list">
<li>Request data (query, body, route) arrives.</li>



<li>Model binding detects scalar types.</li>



<li>Scalars validate and normalize the data.</li>



<li>If validation passes, the scalar is injected into the controller action.</li>



<li>If validation fails, errors are added to <code>ModelState</code>, and appropriate HTTP 400 responses are generated.</li>
</ol>



<p class="wp-block-paragraph">This tight integration allows input processing <strong>before</strong> your application logic sees any data — keeping your business layer pure and safe.</p>



<hr class="wp-block-separator has-alpha-channel-opacity">



<h2 class="wp-block-heading">📌 Basic Workflow of AspNetCore.Scalar</h2>



<p class="wp-block-paragraph">Here’s how a typical <strong>AspNetCore.Scalar</strong> interaction looks like:</p>



<ol class="wp-block-list">
<li><strong>Define a Scalar Class</strong><br>Create a strongly typed wrapper for a specific kind of input (like <code>EmailAddress</code>, <code>Username</code>).</li>



<li><strong>Configure Scalar Bindings</strong><br>Register scalar types into the ASP.NET Core dependency injection system.</li>



<li><strong>Bind Scalars in Controllers</strong><br>Use scalar types as parameters in API or MVC controller actions.</li>



<li><strong>Validation and Normalization Happen Automatically</strong><br>Scalars validate incoming user input at the model-binding stage.</li>



<li><strong>Handle Errors</strong><br>If input fails validation, ASP.NET Core automatically returns an error response, or you can customize the response.</li>



<li><strong>Use Validated Data</strong><br>Valid scalar values are available in controller methods for further business processing.</li>
</ol>



<hr class="wp-block-separator has-alpha-channel-opacity">



<h2 class="wp-block-heading">📌 Step-by-Step Getting Started Guide for AspNetCore.Scalar</h2>



<p class="wp-block-paragraph">Here’s how you can easily get started:</p>



<hr class="wp-block-separator has-alpha-channel-opacity">



<h3 class="wp-block-heading">1️⃣ Install AspNetCore.Scalar</h3>



<p class="wp-block-paragraph">You can install via NuGet:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
dotnet add package AspNetCore.Scalar

</pre></div>


<p class="wp-block-paragraph">Or in Visual Studio, search for <strong>AspNetCore.Scalar</strong> in the NuGet package manager.</p>



<hr class="wp-block-separator has-alpha-channel-opacity">



<h3 class="wp-block-heading">2️⃣ Create Your First Scalar</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
using Scalar;

public class Email : ScalarOf&amp;lt;string&amp;gt;
{
    public Email(string value) : base(value) {}

    protected override void Validate()
    {
        if (string.IsNullOrWhiteSpace(Value))
            throw new ScalarValidationException(&quot;Email is required.&quot;);

        if (!Value.Contains(&quot;@&quot;))
            throw new ScalarValidationException(&quot;Email must contain @ symbol.&quot;);
    }

    protected override string Normalize(string value)
    {
        return value.Trim().ToLower();
    }
}

</pre></div>


<hr class="wp-block-separator has-alpha-channel-opacity">



<h3 class="wp-block-heading">3️⃣ Register Scalars in Startup</h3>



<p class="wp-block-paragraph">In your <code>Program.cs</code> or <code>Startup.cs</code>, register scalar model binding:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
builder.Services.AddScalarModelBinder();

</pre></div>


<hr class="wp-block-separator has-alpha-channel-opacity">



<h3 class="wp-block-heading">4️⃣ Use Scalars in Controller Actions</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#x5B;ApiController]
&#x5B;Route(&quot;&#x5B;controller]&quot;)]
public class UsersController : ControllerBase
{
    &#x5B;HttpPost]
    public IActionResult Register(&#x5B;FromBody] Email email)
    {
        // If this point is reached, email is already validated
        return Ok($&quot;Registered user with email: {email.Value}&quot;);
    }
}

</pre></div>


<hr class="wp-block-separator has-alpha-channel-opacity">



<h3 class="wp-block-heading">5️⃣ Customize Error Responses (Optional)</h3>



<p class="wp-block-paragraph">Customize model state error behavior:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
builder.Services.Configure&amp;lt;ApiBehaviorOptions&amp;gt;(options =&amp;gt;
{
    options.InvalidModelStateResponseFactory = context =&amp;gt;
    {
        var errors = context.ModelState.Values
                       .SelectMany(x =&amp;gt; x.Errors)
                       .Select(x =&amp;gt; x.ErrorMessage);
        return new BadRequestObjectResult(new { Errors = errors });
    };
});

</pre></div><p>The post <a href="https://www.scmgalaxy.com/tutorials/aspnetcore-scalar-explained-modern-input-validation-and-binding-for-net-web-apps/">AspNetCore.Scalar Explained: Modern Input Validation and Binding for .NET Web Apps</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.scmgalaxy.com/tutorials/aspnetcore-scalar-explained-modern-input-validation-and-binding-for-net-web-apps/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
