NumberField
NumberField
NumberField describes a number field in a content type or global field schema, with data_type set to number. It adds a minimum and a maximum value to the properties of Field.
When to Use This Class: Use this class for a numeric value such as a price, a rating, or a reading time.
Conceptual Role: Acts as the Field subclass that the SDK creates for a field with data_type set to number.
Returns: NumberField. A number field of a schema.
Properties
NumberField inherits every property of Field and adds the properties below.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| Min | int? | No | null | Lowest value the field accepts. Serialized as min. |
| Max | int? | No | null | Highest value the field accepts. Serialized as max. |
Class-Level Notes
Limits.
- The SDK leaves out min and max when they are null, so the field has no limit on that side.
- Min and Max are integers. A fetched schema with a decimal limit fails to deserialize into NumberField, and System.Text.Json throws JsonException.
Method Index
NumberField has no methods. Set its properties and add it to ContentModelling.Schema.
Class-Level Snippet
The snippet below creates a content type with a reading time field that accepts values from 1 to 60.
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ContentModelling model = new ContentModelling
{
Title = "Blog Post",
Uid = "blog_post",
Schema = new List<Field>
{
new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
new NumberField { DisplayName = "Reading Time", Uid = "reading_time", DataType = "number", Min = 1, Max = 60 }
}
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Create(model);
Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}