@sanity/types
Advanced tools
Comparing version 2.15.2-fix-sparkline.9 to 2.15.3-conditional-fields.39
@@ -92,2 +92,5 @@ import React from 'react'; | ||
export interface AssetSourceComponentProps { | ||
assetType?: 'file' | 'image'; | ||
document: SanityDocument; | ||
dialogHeaderTitle?: string; | ||
selectedAssets: Asset[]; | ||
@@ -94,0 +97,0 @@ selectionType: 'single' | 'multiple'; |
@@ -12,2 +12,3 @@ export * from './slug'; | ||
export * from './images'; | ||
export * from './user'; | ||
//# sourceMappingURL=index.d.ts.map |
import React from 'react'; | ||
import { Rule } from '../validation'; | ||
import { ReferenceOptions } from '../reference'; | ||
import { AssetSource } from '../assets'; | ||
import { SlugOptions } from '../slug'; | ||
import { SanityDocument } from '../documents'; | ||
import { CurrentUser } from '../user'; | ||
export interface Schema { | ||
@@ -28,5 +31,37 @@ name: string; | ||
}; | ||
export interface HiddenOptionCallbackContext { | ||
parent?: Record<string, unknown>; | ||
document: SanityDocument; | ||
currentUser: Omit<CurrentUser, 'role'>; | ||
value: unknown; | ||
} | ||
export declare type HiddenOption = boolean | HiddenOptionCallback; | ||
export declare type HiddenOptionCallback = (context: HiddenOptionCallbackContext) => boolean; | ||
export declare type InitialValueParams = Record<string, unknown>; | ||
export declare type InitialValueResolver<T> = (params?: InitialValueParams) => Promise<T> | T; | ||
export declare type InitialValueProperty<T = unknown> = T | InitialValueResolver<T> | undefined; | ||
/** | ||
* Represents the possible values of a schema type's `validation` field. | ||
* | ||
* If the schema has not been run through `inferFromSchema` from | ||
* `@sanity/validation` then value could be a function. | ||
* | ||
* `inferFromSchema` mutates the schema converts this value to an array of | ||
* `Rule` instances. | ||
* | ||
* @privateRemarks | ||
* | ||
* Usage of the schema inside the studio will almost always be from the compiled | ||
* `createSchema` function. In this case, you can cast the value or throw to | ||
* narrow the type. E.g.: | ||
* | ||
* ```ts | ||
* if (typeof type.validation === 'function') { | ||
* throw new Error( | ||
* `Schema type "${type.name}"'s \`validation\` was not run though \`inferFromSchema\`` | ||
* ) | ||
* } | ||
* ``` | ||
*/ | ||
declare type ValidationValue = false | Rule[] | ((rule: Rule) => Rule | Rule[]); | ||
export interface BaseSchemaType { | ||
@@ -42,3 +77,2 @@ name: string; | ||
options?: unknown; | ||
hidden?: boolean; | ||
preview?: { | ||
@@ -75,2 +109,3 @@ select?: PreviewValue; | ||
initialValue?: ((arg?: any) => Promise<string> | string) | string | undefined; | ||
validation?: ValidationValue; | ||
} | ||
@@ -84,2 +119,3 @@ export interface TextSchemaType extends StringSchemaType { | ||
initialValue?: InitialValueProperty<number>; | ||
validation?: ValidationValue; | ||
} | ||
@@ -92,2 +128,3 @@ export interface BooleanSchemaType extends BaseSchemaType { | ||
initialValue?: InitialValueProperty<boolean>; | ||
validation?: ValidationValue; | ||
} | ||
@@ -107,2 +144,3 @@ export interface ArraySchemaType<V = unknown> extends BaseSchemaType { | ||
}; | ||
validation?: ValidationValue; | ||
} | ||
@@ -118,8 +156,9 @@ export interface BlockSchemaType extends ObjectSchemaType { | ||
} | ||
export declare type ObjectFieldType<T extends SchemaType = SchemaType> = T & { | ||
hidden?: HiddenOption; | ||
}; | ||
export interface ObjectField<T extends SchemaType = SchemaType> { | ||
name: string; | ||
fieldset?: string; | ||
type: T & { | ||
hidden?: boolean; | ||
}; | ||
type: ObjectFieldType<T>; | ||
} | ||
@@ -131,5 +170,7 @@ export interface ObjectSchemaType extends BaseSchemaType { | ||
initialValue?: InitialValueProperty<Record<string, unknown>>; | ||
validation?: ValidationValue; | ||
__experimental_search?: { | ||
path: string; | ||
weight: number; | ||
mapWith?: string; | ||
}[]; | ||
@@ -176,3 +217,5 @@ } | ||
export interface FileSchemaType extends ObjectSchemaType { | ||
options?: AssetSchemaTypeOptions; | ||
options?: AssetSchemaTypeOptions & { | ||
sources?: AssetSource[]; | ||
}; | ||
} | ||
@@ -179,0 +222,0 @@ export interface ImageSchemaType extends ObjectSchemaType { |
import { Path } from '../paths'; | ||
import { SchemaType } from '../schema'; | ||
import { SanityDocument } from '../documents'; | ||
import { ValidationMarker } from '../markers'; | ||
export declare type RuleTypeConstraint = 'Array' | 'Boolean' | 'Date' | 'Number' | 'Object' | 'String'; | ||
export declare type FieldRules = { | ||
[fieldKey: string]: (fieldRule: Rule) => Rule; | ||
}; | ||
export interface RuleClass { | ||
FIELD_REF: symbol; | ||
array: (def?: SchemaType) => Rule; | ||
object: (def?: SchemaType) => Rule; | ||
string: (def?: SchemaType) => Rule; | ||
number: (def?: SchemaType) => Rule; | ||
boolean: (def?: SchemaType) => Rule; | ||
dateTime: (def?: SchemaType) => Rule; | ||
valueOfField: Rule['valueOfField']; | ||
new (typeDef?: SchemaType): Rule; | ||
} | ||
export interface Rule { | ||
/** | ||
* @internal | ||
*/ | ||
_type: RuleTypeConstraint | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
_level: 'error' | 'warning' | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
_required: 'required' | 'optional' | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
_typeDef: SchemaType | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
_message: string | undefined; | ||
/** | ||
* @internal | ||
*/ | ||
_rules: RuleSpec[]; | ||
/** | ||
* @internal | ||
*/ | ||
_fieldRules: FieldRules | undefined; | ||
/** | ||
* Takes in a path and returns an object with a symbol. | ||
* | ||
* When the validation lib sees this symbol, it will use the provided path to | ||
* get a value from the current field's parent and use that value as the input | ||
* to the Rule. | ||
* | ||
* The path that's given is forwarded to `lodash/get` | ||
* | ||
* ```js | ||
* fields: [ | ||
* // ... | ||
* { | ||
* // ... | ||
* name: 'highestTemperature', | ||
* type: 'number', | ||
* validation: (Rule) => Rule.positive().min(Rule.valueOfField('lowestTemperature')), | ||
* // ... | ||
* }, | ||
* ] | ||
* ``` | ||
*/ | ||
valueOfField: (path: string | string[]) => { | ||
type: symbol; | ||
path: string | string[]; | ||
}; | ||
error(message?: string): Rule; | ||
warning(message?: string): Rule; | ||
reset(): this; | ||
isRequired(): boolean; | ||
clone(): Rule; | ||
cloneWithRules(rules: RuleSpec[]): Rule; | ||
merge(rule: Rule): Rule; | ||
type(targetType: RuleTypeConstraint | Lowercase<RuleTypeConstraint>): Rule; | ||
all(children: Rule[]): Rule; | ||
either(children: Rule[]): Rule; | ||
optional(): Rule; | ||
required(): Rule; | ||
custom<T = unknown>(fn: CustomValidator<T>): Rule; | ||
min(len: number): Rule; | ||
max(len: number): Rule; | ||
length(len: number): Rule; | ||
valid(value: unknown | unknown[]): Rule; | ||
integer(): Rule; | ||
precision(limit: number): Rule; | ||
positive(): Rule; | ||
negative(): Rule; | ||
greaterThan(num: number): Rule; | ||
lessThan(num: number): Rule; | ||
uppercase(): Rule; | ||
lowercase(): Rule; | ||
regex(pattern: RegExp, name: string, options: { | ||
name?: string; | ||
invert?: boolean; | ||
}): Rule; | ||
regex(pattern: RegExp, options: { | ||
name?: string; | ||
invert?: boolean; | ||
}): Rule; | ||
regex(pattern: RegExp, name: string): Rule; | ||
regex(pattern: RegExp): Rule; | ||
email(): Rule; | ||
uri(options?: { | ||
scheme?: (string | RegExp) | Array<string | RegExp>; | ||
allowRelative?: boolean; | ||
relativeOnly?: boolean; | ||
allowCredentials?: boolean; | ||
}): Rule; | ||
unique(): Rule; | ||
reference(): Rule; | ||
block(blockValidators: BlockValidator): Rule; | ||
fields(rules: FieldRules): Rule; | ||
assetRequired(): Rule; | ||
validate(value: unknown, options?: ValidationContext): Promise<ValidationMarker[]>; | ||
} | ||
export declare type RuleSpec = { | ||
flag: 'integer'; | ||
} | { | ||
flag: 'email'; | ||
} | { | ||
flag: 'unique'; | ||
} | { | ||
flag: 'reference'; | ||
} | { | ||
flag: 'type'; | ||
constraint: RuleTypeConstraint; | ||
} | { | ||
flag: 'all'; | ||
constraint: Rule[]; | ||
} | { | ||
flag: 'either'; | ||
constraint: Rule[]; | ||
} | { | ||
flag: 'presence'; | ||
constraint: 'optional' | 'required'; | ||
} | { | ||
flag: 'custom'; | ||
constraint: CustomValidator; | ||
} | { | ||
flag: 'min'; | ||
constraint: number; | ||
} | { | ||
flag: 'max'; | ||
constraint: number; | ||
} | { | ||
flag: 'length'; | ||
constraint: number; | ||
} | { | ||
flag: 'valid'; | ||
constraint: unknown[]; | ||
} | { | ||
flag: 'precision'; | ||
constraint: number; | ||
} | { | ||
flag: 'lessThan'; | ||
constraint: number; | ||
} | { | ||
flag: 'greaterThan'; | ||
constraint: number; | ||
} | { | ||
flag: 'stringCasing'; | ||
constraint: 'uppercase' | 'lowercase'; | ||
} | { | ||
flag: 'block'; | ||
constraint: BlockValidator; | ||
} | { | ||
flag: 'assetRequired'; | ||
constraint: { | ||
assetType: 'Asset' | 'Image' | 'File'; | ||
}; | ||
} | { | ||
flag: 'regex'; | ||
constraint: { | ||
pattern: RegExp; | ||
name?: string; | ||
invert: boolean; | ||
}; | ||
} | { | ||
flag: 'uri'; | ||
constraint: { | ||
options: { | ||
scheme: RegExp[]; | ||
allowRelative: boolean; | ||
relativeOnly: boolean; | ||
allowCredentials: boolean; | ||
}; | ||
}; | ||
}; | ||
declare type ConditionalIndexAccess<T, U> = U extends keyof T ? T[U] : undefined; | ||
export declare type RuleSpecConstraint<T extends RuleSpec['flag']> = ConditionalIndexAccess<Extract<RuleSpec, { | ||
flag: T; | ||
}>, 'constraint'>; | ||
/** | ||
* A context object passed around during validation. This includes the | ||
* `Rule.custom` context. | ||
* | ||
* e.g. | ||
* | ||
* ```js | ||
* Rule.custom((_, validationContext) => { | ||
* // ... | ||
* })` | ||
* ``` | ||
*/ | ||
export declare type ValidationContext = { | ||
parent?: unknown; | ||
type?: SchemaType; | ||
document?: SanityDocument; | ||
path?: Path; | ||
}; | ||
/** | ||
* @internal | ||
* The base type for all validators in the validation library. Takes in a | ||
* `RuleSpec`'s constraint, the value to check, an optional override message, | ||
* and the validation context. | ||
* | ||
* @see Rule.validate from `@sanity/validation/src/Rule` | ||
*/ | ||
export declare type Validator<T = any, Value = any> = (constraint: T, value: Value, message: string | undefined, context: ValidationContext) => ValidationError[] | ValidationError | string | true | Promise<ValidationError[] | ValidationError | string | true>; | ||
/** | ||
* @internal | ||
* A type helper used to define a group of validators. The type of the | ||
* `RuleSpec` constraint is inferred via the key. | ||
* | ||
* E.g. | ||
* | ||
* ```ts | ||
* const booleanValidators: Validators = { | ||
* ...genericValidator, | ||
* | ||
* presence: (v, value, message) => { | ||
* if (v === 'required' && typeof value !== 'boolean') return message || 'Required' | ||
* return true | ||
* }, | ||
* } | ||
* ``` | ||
*/ | ||
export declare type Validators = Partial<{ | ||
[P in RuleSpec['flag']]: Validator<ConditionalIndexAccess<Extract<RuleSpec, { | ||
flag: P; | ||
}>, 'constraint'>>; | ||
}>; | ||
export interface ValidationErrorOptions { | ||
paths?: Path[]; | ||
children?: ValidationMarker[]; | ||
operation?: 'AND' | 'OR'; | ||
} | ||
export interface ValidationErrorClass { | ||
new (message: string, options?: ValidationErrorOptions): ValidationError; | ||
} | ||
export interface ValidationError { | ||
message: string; | ||
children?: ValidationError[]; | ||
children?: ValidationMarker[]; | ||
operation?: 'AND' | 'OR'; | ||
@@ -9,2 +266,9 @@ paths: Path[]; | ||
} | ||
export declare type CustomValidatorResult = true | string | ValidationError; | ||
export declare type CustomValidator<T = unknown> = (value: T, context: ValidationContext) => CustomValidatorResult | Promise<CustomValidatorResult>; | ||
export declare type BlockValidator = (block: any, options: ValidationContext) => ValidationError[] | ValidationError | string | true | Promise<ValidationError[] | ValidationError | string | true>; | ||
export declare type SlugIsUniqueValidator = (slug: string, options: ValidationContext & { | ||
defaultIsUnique: SlugIsUniqueValidator; | ||
}) => Promise<boolean>; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -148,2 +148,15 @@ "use strict"; | ||
}); | ||
}); | ||
var _user = require("./user"); | ||
Object.keys(_user).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _user[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _user[key]; | ||
} | ||
}); | ||
}); |
{ | ||
"name": "@sanity/types", | ||
"version": "2.15.2-fix-sparkline.9+8754b4e07", | ||
"version": "2.15.3-conditional-fields.39+0d82c21215", | ||
"description": "Type definitions for common Sanity data structures", | ||
@@ -23,3 +23,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"@sanity/client": "2.15.2-fix-sparkline.9+8754b4e07", | ||
"@sanity/client": "2.15.3-conditional-fields.39+0d82c21215", | ||
"@sanity/color": "^2.1.4", | ||
@@ -42,3 +42,3 @@ "@types/react": "^17.0.0", | ||
"homepage": "https://www.sanity.io/", | ||
"gitHead": "8754b4e07c9a38236c53883d25216b1c1c4bf0ac" | ||
"gitHead": "0d82c21215c5c9bd7a156daede2c16a5e43ec879" | ||
} |
@@ -12,2 +12,3 @@ declare module 'part:@sanity/form-builder' | ||
declare module 'part:@sanity/form-builder/input/image/asset-sources?' | ||
declare module 'part:@sanity/form-builder/input/file/asset-sources?' | ||
declare module 'part:@sanity/form-builder/input/legacy-date/schema?' | ||
@@ -14,0 +15,0 @@ declare module 'part:@sanity/form-builder/input/number?' |
@@ -116,2 +116,5 @@ import React from 'react' | ||
export interface AssetSourceComponentProps { | ||
assetType?: 'file' | 'image' | ||
document: SanityDocument | ||
dialogHeaderTitle?: string | ||
selectedAssets: Asset[] | ||
@@ -118,0 +121,0 @@ selectionType: 'single' | 'multiple' |
@@ -12,1 +12,2 @@ export * from './slug' | ||
export * from './images' | ||
export * from './user' |
// Note: INCOMPLETE, but it's a start | ||
import React from 'react' | ||
import {Rule} from '../validation' | ||
import {ReferenceOptions} from '../reference' | ||
import {AssetSource} from '../assets' | ||
import {SlugOptions} from '../slug' | ||
import {SanityDocument} from '../documents' | ||
import {CurrentUser} from '../user' | ||
@@ -34,2 +37,12 @@ export interface Schema { | ||
export interface HiddenOptionCallbackContext { | ||
parent?: Record<string, unknown> | ||
document: SanityDocument | ||
currentUser: Omit<CurrentUser, 'role'> | ||
value: unknown | ||
} | ||
export type HiddenOption = boolean | HiddenOptionCallback | ||
export type HiddenOptionCallback = (context: HiddenOptionCallbackContext) => boolean | ||
export type InitialValueParams = Record<string, unknown> | ||
@@ -39,2 +52,27 @@ export type InitialValueResolver<T> = (params?: InitialValueParams) => Promise<T> | T | ||
/** | ||
* Represents the possible values of a schema type's `validation` field. | ||
* | ||
* If the schema has not been run through `inferFromSchema` from | ||
* `@sanity/validation` then value could be a function. | ||
* | ||
* `inferFromSchema` mutates the schema converts this value to an array of | ||
* `Rule` instances. | ||
* | ||
* @privateRemarks | ||
* | ||
* Usage of the schema inside the studio will almost always be from the compiled | ||
* `createSchema` function. In this case, you can cast the value or throw to | ||
* narrow the type. E.g.: | ||
* | ||
* ```ts | ||
* if (typeof type.validation === 'function') { | ||
* throw new Error( | ||
* `Schema type "${type.name}"'s \`validation\` was not run though \`inferFromSchema\`` | ||
* ) | ||
* } | ||
* ``` | ||
*/ | ||
type ValidationValue = false | Rule[] | ((rule: Rule) => Rule | Rule[]) | ||
export interface BaseSchemaType { | ||
@@ -50,3 +88,2 @@ name: string | ||
options?: unknown | ||
hidden?: boolean | ||
@@ -92,2 +129,3 @@ preview?: { | ||
initialValue?: ((arg?: any) => Promise<string> | string) | string | undefined | ||
validation?: ValidationValue | ||
} | ||
@@ -103,2 +141,3 @@ | ||
initialValue?: InitialValueProperty<number> | ||
validation?: ValidationValue | ||
} | ||
@@ -112,2 +151,3 @@ | ||
initialValue?: InitialValueProperty<boolean> | ||
validation?: ValidationValue | ||
} | ||
@@ -129,2 +169,3 @@ | ||
} | ||
validation?: ValidationValue | ||
} | ||
@@ -143,6 +184,8 @@ | ||
export type ObjectFieldType<T extends SchemaType = SchemaType> = T & {hidden?: HiddenOption} | ||
export interface ObjectField<T extends SchemaType = SchemaType> { | ||
name: string | ||
fieldset?: string | ||
type: T & {hidden?: boolean} | ||
type: ObjectFieldType<T> | ||
} | ||
@@ -155,6 +198,7 @@ | ||
initialValue?: InitialValueProperty<Record<string, unknown>> | ||
validation?: ValidationValue | ||
// Experimentals | ||
/* eslint-disable camelcase */ | ||
__experimental_search?: {path: string; weight: number}[] | ||
__experimental_search?: {path: string; weight: number; mapWith?: string}[] | ||
/* eslint-enable camelcase */ | ||
@@ -210,3 +254,5 @@ } | ||
export interface FileSchemaType extends ObjectSchemaType { | ||
options?: AssetSchemaTypeOptions | ||
options?: AssetSchemaTypeOptions & { | ||
sources?: AssetSource[] | ||
} | ||
} | ||
@@ -213,0 +259,0 @@ |
import {Path} from '../paths' | ||
import {SchemaType} from '../schema' | ||
import {SanityDocument} from '../documents' | ||
import {ValidationMarker} from '../markers' | ||
export type RuleTypeConstraint = 'Array' | 'Boolean' | 'Date' | 'Number' | 'Object' | 'String' | ||
export type FieldRules = {[fieldKey: string]: (fieldRule: Rule) => Rule} | ||
// Note: `RuleClass` and `Rule` are split to fit the current `@sanity/types` | ||
// setup. Classes are a bit weird in the `@sanity/types` package because classes | ||
// create an actual javascript class while simultaneously creating a type | ||
// definition. | ||
// | ||
// This implicitly creates two types: | ||
// 1. the instance type — `Rule` and | ||
// 2. the static/class type - `RuleClass` | ||
// | ||
// The `RuleClass` type contains the static methods and the `Rule` instance | ||
// contains the instance methods. Downstream in the validation package, the Rule | ||
// implementation asserts the class declaration is of this type. | ||
export interface RuleClass { | ||
FIELD_REF: symbol | ||
array: (def?: SchemaType) => Rule | ||
object: (def?: SchemaType) => Rule | ||
string: (def?: SchemaType) => Rule | ||
number: (def?: SchemaType) => Rule | ||
boolean: (def?: SchemaType) => Rule | ||
dateTime: (def?: SchemaType) => Rule | ||
valueOfField: Rule['valueOfField'] | ||
new (typeDef?: SchemaType): Rule | ||
} | ||
export interface Rule { | ||
/** | ||
* @internal | ||
*/ | ||
_type: RuleTypeConstraint | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_level: 'error' | 'warning' | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_required: 'required' | 'optional' | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_typeDef: SchemaType | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_message: string | undefined | ||
/** | ||
* @internal | ||
*/ | ||
_rules: RuleSpec[] | ||
/** | ||
* @internal | ||
*/ | ||
_fieldRules: FieldRules | undefined | ||
/** | ||
* Takes in a path and returns an object with a symbol. | ||
* | ||
* When the validation lib sees this symbol, it will use the provided path to | ||
* get a value from the current field's parent and use that value as the input | ||
* to the Rule. | ||
* | ||
* The path that's given is forwarded to `lodash/get` | ||
* | ||
* ```js | ||
* fields: [ | ||
* // ... | ||
* { | ||
* // ... | ||
* name: 'highestTemperature', | ||
* type: 'number', | ||
* validation: (Rule) => Rule.positive().min(Rule.valueOfField('lowestTemperature')), | ||
* // ... | ||
* }, | ||
* ] | ||
* ``` | ||
*/ | ||
valueOfField: (path: string | string[]) => {type: symbol; path: string | string[]} | ||
error(message?: string): Rule | ||
warning(message?: string): Rule | ||
reset(): this | ||
isRequired(): boolean | ||
clone(): Rule | ||
cloneWithRules(rules: RuleSpec[]): Rule | ||
merge(rule: Rule): Rule | ||
type(targetType: RuleTypeConstraint | Lowercase<RuleTypeConstraint>): Rule | ||
all(children: Rule[]): Rule | ||
either(children: Rule[]): Rule | ||
optional(): Rule | ||
required(): Rule | ||
custom<T = unknown>(fn: CustomValidator<T>): Rule | ||
min(len: number): Rule | ||
max(len: number): Rule | ||
length(len: number): Rule | ||
valid(value: unknown | unknown[]): Rule | ||
integer(): Rule | ||
precision(limit: number): Rule | ||
positive(): Rule | ||
negative(): Rule | ||
greaterThan(num: number): Rule | ||
lessThan(num: number): Rule | ||
uppercase(): Rule | ||
lowercase(): Rule | ||
regex(pattern: RegExp, name: string, options: {name?: string; invert?: boolean}): Rule | ||
regex(pattern: RegExp, options: {name?: string; invert?: boolean}): Rule | ||
regex(pattern: RegExp, name: string): Rule | ||
regex(pattern: RegExp): Rule | ||
email(): Rule | ||
uri(options?: { | ||
scheme?: (string | RegExp) | Array<string | RegExp> | ||
allowRelative?: boolean | ||
relativeOnly?: boolean | ||
allowCredentials?: boolean | ||
}): Rule | ||
unique(): Rule | ||
reference(): Rule | ||
block(blockValidators: BlockValidator): Rule | ||
fields(rules: FieldRules): Rule | ||
assetRequired(): Rule | ||
validate(value: unknown, options?: ValidationContext): Promise<ValidationMarker[]> | ||
} | ||
export type RuleSpec = | ||
| {flag: 'integer'} | ||
| {flag: 'email'} | ||
| {flag: 'unique'} | ||
| {flag: 'reference'} | ||
| {flag: 'type'; constraint: RuleTypeConstraint} | ||
| {flag: 'all'; constraint: Rule[]} | ||
| {flag: 'either'; constraint: Rule[]} | ||
| {flag: 'presence'; constraint: 'optional' | 'required'} | ||
| {flag: 'custom'; constraint: CustomValidator} | ||
| {flag: 'min'; constraint: number} | ||
| {flag: 'max'; constraint: number} | ||
| {flag: 'length'; constraint: number} | ||
| {flag: 'valid'; constraint: unknown[]} | ||
| {flag: 'precision'; constraint: number} | ||
| {flag: 'lessThan'; constraint: number} | ||
| {flag: 'greaterThan'; constraint: number} | ||
| {flag: 'stringCasing'; constraint: 'uppercase' | 'lowercase'} | ||
| {flag: 'block'; constraint: BlockValidator} | ||
| {flag: 'assetRequired'; constraint: {assetType: 'Asset' | 'Image' | 'File'}} | ||
| { | ||
flag: 'regex' | ||
constraint: { | ||
pattern: RegExp | ||
name?: string | ||
invert: boolean | ||
} | ||
} | ||
| { | ||
flag: 'uri' | ||
constraint: { | ||
options: { | ||
scheme: RegExp[] | ||
allowRelative: boolean | ||
relativeOnly: boolean | ||
allowCredentials: boolean | ||
} | ||
} | ||
} | ||
// this is used to get allow index access (e.g. `RuleSpec['constraint']`) to | ||
// constraint when a rule spec might not have a 'constraint` prop | ||
type ConditionalIndexAccess<T, U> = U extends keyof T ? T[U] : undefined | ||
export type RuleSpecConstraint<T extends RuleSpec['flag']> = ConditionalIndexAccess< | ||
Extract<RuleSpec, {flag: T}>, | ||
'constraint' | ||
> | ||
/** | ||
* A context object passed around during validation. This includes the | ||
* `Rule.custom` context. | ||
* | ||
* e.g. | ||
* | ||
* ```js | ||
* Rule.custom((_, validationContext) => { | ||
* // ... | ||
* })` | ||
* ``` | ||
*/ | ||
export type ValidationContext = { | ||
parent?: unknown | ||
type?: SchemaType | ||
document?: SanityDocument | ||
path?: Path | ||
} | ||
/** | ||
* @internal | ||
* The base type for all validators in the validation library. Takes in a | ||
* `RuleSpec`'s constraint, the value to check, an optional override message, | ||
* and the validation context. | ||
* | ||
* @see Rule.validate from `@sanity/validation/src/Rule` | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Validator<T = any, Value = any> = ( | ||
constraint: T, | ||
value: Value, | ||
message: string | undefined, | ||
context: ValidationContext | ||
) => | ||
| ValidationError[] | ||
| ValidationError | ||
| string | ||
| true | ||
| Promise<ValidationError[] | ValidationError | string | true> | ||
/** | ||
* @internal | ||
* A type helper used to define a group of validators. The type of the | ||
* `RuleSpec` constraint is inferred via the key. | ||
* | ||
* E.g. | ||
* | ||
* ```ts | ||
* const booleanValidators: Validators = { | ||
* ...genericValidator, | ||
* | ||
* presence: (v, value, message) => { | ||
* if (v === 'required' && typeof value !== 'boolean') return message || 'Required' | ||
* return true | ||
* }, | ||
* } | ||
* ``` | ||
*/ | ||
export type Validators = Partial< | ||
{ | ||
[P in RuleSpec['flag']]: Validator< | ||
ConditionalIndexAccess<Extract<RuleSpec, {flag: P}>, 'constraint'> | ||
> | ||
} | ||
> | ||
export interface ValidationErrorOptions { | ||
paths?: Path[] | ||
children?: ValidationMarker[] | ||
operation?: 'AND' | 'OR' | ||
} | ||
// This follows the same pattern as `RuleClass` and `Rule` above | ||
// Note: this class does not actually extend `Error` since it's never thrown | ||
// within the validation library | ||
export interface ValidationErrorClass { | ||
new (message: string, options?: ValidationErrorOptions): ValidationError | ||
} | ||
export interface ValidationError { | ||
message: string | ||
children?: ValidationError[] | ||
children?: ValidationMarker[] | ||
operation?: 'AND' | 'OR' | ||
@@ -10,1 +267,26 @@ paths: Path[] | ||
} | ||
export type CustomValidatorResult = true | string | ValidationError | ||
export type CustomValidator<T = unknown> = ( | ||
value: T, | ||
context: ValidationContext | ||
) => CustomValidatorResult | Promise<CustomValidatorResult> | ||
export type BlockValidator = ( | ||
// eslint-disable-next-line no-warning-comments | ||
// TODO (eventually): add the type of block here | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
block: any, | ||
options: ValidationContext | ||
) => | ||
| ValidationError[] | ||
| ValidationError | ||
| string | ||
| true | ||
| Promise<ValidationError[] | ValidationError | string | true> | ||
export type SlugIsUniqueValidator = ( | ||
slug: string, | ||
options: ValidationContext & {defaultIsUnique: SlugIsUniqueValidator} | ||
) => Promise<boolean> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
457273
541
15216