@sanity/validation 3.14.3 → 6.12.0-next.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +12 -0
  3. package/lib/_internal.d.ts +93 -0
  4. package/lib/_internal.js +39 -0
  5. package/lib/_internal.js.map +1 -0
  6. package/lib/index.d.ts +2 -0
  7. package/lib/index.js +2 -1306
  8. package/lib/validateDocument-C6wlQ7gA.d.ts +151 -0
  9. package/lib/validateDocument-il4G-TLy.js +828 -0
  10. package/lib/validateDocument-il4G-TLy.js.map +1 -0
  11. package/package.json +53 -45
  12. package/lib/dts/src/index.d.ts +0 -50
  13. package/lib/index.cjs.mjs +0 -9
  14. package/lib/index.esm.js +0 -1286
  15. package/lib/index.esm.js.map +0 -1
  16. package/lib/index.js.map +0 -1
  17. package/src/Rule.ts +0 -424
  18. package/src/ValidationError.ts +0 -32
  19. package/src/index.ts +0 -9
  20. package/src/inferFromSchema.ts +0 -19
  21. package/src/inferFromSchemaType.ts +0 -50
  22. package/src/util/convertToValidationMarker.ts +0 -84
  23. package/src/util/deepEquals.ts +0 -77
  24. package/src/util/escapeRegex.ts +0 -5
  25. package/src/util/normalizeValidationRules.test.ts +0 -170
  26. package/src/util/normalizeValidationRules.ts +0 -118
  27. package/src/util/pathToString.ts +0 -21
  28. package/src/util/requestIdleCallback.ts +0 -31
  29. package/src/util/typeString.test.ts +0 -27
  30. package/src/util/typeString.ts +0 -23
  31. package/src/validateDocument.test.ts +0 -703
  32. package/src/validateDocument.ts +0 -240
  33. package/src/validators/arrayValidator.ts +0 -100
  34. package/src/validators/booleanValidator.ts +0 -16
  35. package/src/validators/dateValidator.ts +0 -113
  36. package/src/validators/genericValidator.ts +0 -117
  37. package/src/validators/numberValidator.ts +0 -66
  38. package/src/validators/objectValidator.ts +0 -64
  39. package/src/validators/slugValidator.ts +0 -117
  40. package/src/validators/stringValidator.ts +0 -120
@@ -0,0 +1,151 @@
1
+ import { CurrentUser, SanityDocument, Schema, SchemaType, ValidationContext as ValidationContext$1, ValidationMarker } from "@sanity/types";
2
+ import { ConcurrencyLimiter } from "@sanity/util/concurrency-limiter";
3
+ import { Observable } from "rxjs";
4
+ import { TFunction } from "i18next";
5
+ import { SanityClient } from "@sanity/client";
6
+ interface LocaleSource {
7
+ currentLocale: {
8
+ id: string;
9
+ };
10
+ loadNamespaces(namespaces: string[]): Promise<void>;
11
+ t: TFunction;
12
+ }
13
+ declare module '@sanity/types' {
14
+ /**
15
+ * Extended validation context that includes internationalization
16
+ *
17
+ * Why is this not directly part of `@sanity/types`, you ask?
18
+ * Because `@sanity/types` shouldn't need to depend on the `i18next` package, which it needs
19
+ * for the `TFunction` type. The `ValidationContext` should never have been part of the types
20
+ * module in the first place, but is now unfortunately part of the public API and thus cannot
21
+ * be changed easily.
22
+ *
23
+ * This is a temporary solution until we can remove the `ValidationContext` from the types module,
24
+ * which is likely to happen at the next major version.
25
+ *
26
+ * @public
27
+ */
28
+ interface ValidationContext {
29
+ /**
30
+ * Internationalization utilities, for translation of validation messages
31
+ *
32
+ * See {@link LocaleSource} for details.
33
+ */
34
+ i18n: LocaleSource;
35
+ }
36
+ }
37
+ /**
38
+ * @internal
39
+ */
40
+ declare function resolveTypeForArrayItem(item: unknown, candidates: SchemaType[]): SchemaType | undefined;
41
+ /**
42
+ * @beta
43
+ */
44
+ interface ValidateDocumentOptions {
45
+ /**
46
+ * The document to be validated
47
+ */
48
+ document: SanityDocument;
49
+ /** The compiled schema to validate against. */
50
+ schema: Schema;
51
+ /** A configured client used for reference checks and custom validators. */
52
+ client: SanityClient;
53
+ /**
54
+ * Function used to check if referenced documents exists (and is published).
55
+ *
56
+ * If you're validating many documents in bulk, you may want to query for all
57
+ * document IDs first and provide your own implementation using those.
58
+ *
59
+ * If no function is provided a default one will be provided that will batch
60
+ * call the `doc` endpoint to check for document existence.
61
+ */
62
+ getDocumentExists?: (options: {
63
+ id: string;
64
+ }) => Promise<boolean>;
65
+ /**
66
+ * The maximum amount of custom validation functions to be running
67
+ * concurrently at once. This helps prevent custom validators from
68
+ * overwhelming backend services (e.g. called via fetch) used in async,
69
+ * user-defined validation functions. (i.e. `rule.custom(async() => {})`)
70
+ *
71
+ * Note that lowering this number may also help in cases where a custom
72
+ * validator could potentially exhaust the fetch concurrency. This is 5 by
73
+ * default.
74
+ */
75
+ maxCustomValidationConcurrency?: number;
76
+ /**
77
+ * The amount of allowed inflight fetch requests at once for this validation.
78
+ * You may need to up this value if you have complex custom validations that
79
+ * require many `client.fetch` requests at once. It's possible for a custom
80
+ * validator to stall if there are not enough concurrent fetch requests
81
+ * available to fulfill the custom validation. Must be a positive integer.
82
+ * This is 25 by default.
83
+ */
84
+ maxFetchConcurrency?: number;
85
+ /**
86
+ * The current user, when available. Used when resolving schema `hidden`
87
+ * conditionals so validation matches what the form shows. If omitted, hidden
88
+ * is resolved with no user (e.g. CLI or headless validation).
89
+ */
90
+ currentUser?: Omit<CurrentUser, 'role'> | null;
91
+ }
92
+ /**
93
+ * Validates a document against a compiled schema. Returns validation markers
94
+ * without deciding whether the document may be edited or published.
95
+ *
96
+ * @beta
97
+ */
98
+ declare function validateDocument({ client, document, schema, ...options }: ValidateDocumentOptions): Promise<ValidationMarker[]>;
99
+ /** @internal */
100
+ interface ValidateDocumentInternalOptions {
101
+ document: SanityDocument;
102
+ schema: Schema;
103
+ getClient: (clientOptions: {
104
+ apiVersion: string;
105
+ }) => SanityClient;
106
+ getDocumentExists?: (options: {
107
+ id: string;
108
+ }) => Promise<boolean>;
109
+ i18n?: LocaleSource;
110
+ environment: 'cli' | 'studio';
111
+ maxCustomValidationConcurrency?: number;
112
+ maxFetchConcurrency?: number;
113
+ currentUser?: Omit<CurrentUser, 'role'> | null;
114
+ }
115
+ /** @internal */
116
+ declare function validateDocumentInternal({ document, schema, getClient, getDocumentExists, i18n, environment, maxCustomValidationConcurrency, maxFetchConcurrency, currentUser }: ValidateDocumentInternalOptions): Promise<ValidationMarker[]>;
117
+ /**
118
+ * @internal
119
+ */
120
+ interface ValidateDocumentObservableOptions extends Pick<ValidationContext$1, 'getDocumentExists' | 'i18n'> {
121
+ getClient: (options: {
122
+ apiVersion: string;
123
+ }) => SanityClient;
124
+ document: SanityDocument;
125
+ schema: Schema;
126
+ environment: 'cli' | 'studio';
127
+ maxCustomValidationConcurrency?: number;
128
+ currentUser?: Omit<CurrentUser, 'role'> | null;
129
+ }
130
+ /**
131
+ * Validates a document against the given schema, returning an Observable
132
+ * @internal
133
+ */
134
+ declare function validateDocumentObservable({ document, getClient, i18n, schema, getDocumentExists, environment, maxCustomValidationConcurrency, currentUser }: ValidateDocumentObservableOptions): Observable<ValidationMarker[]>;
135
+ /**
136
+ * this is used make optional properties required by replacing optionals with
137
+ * `T[P] | undefined`. this is used to prevent errors in `validateItem` where
138
+ * an option from a previous invocation would be incorrectly passed down.
139
+ *
140
+ * https://medium.com/terria/typescript-transforming-optional-properties-to-required-properties-that-may-be-undefined-7482cb4e1585
141
+ */
142
+ type ExplicitUndefined<T> = { [P in keyof Required<T>]: Pick<T, P> extends Required<Pick<T, P>> ? T[P] : T[P] | undefined; };
143
+ type ValidateItemOptions = {
144
+ value: unknown;
145
+ customValidationConcurrencyLimiter?: ConcurrencyLimiter;
146
+ hidden?: boolean;
147
+ currentUser?: Omit<CurrentUser, 'role'> | null;
148
+ } & ExplicitUndefined<Omit<ValidationContext$1, 'hidden'>>;
149
+ declare function validateItem(opts: ValidateItemOptions): Promise<ValidationMarker[]>;
150
+ export { validateDocument as a, validateItem as c, resolveTypeForArrayItem as i, ValidationContext$1 as l, ValidateDocumentObservableOptions as n, validateDocumentInternal as o, ValidateDocumentOptions as r, validateDocumentObservable as s, ValidateDocumentInternalOptions as t, LocaleSource as u };
151
+ //# sourceMappingURL=validateDocument-C6wlQ7gA.d.ts.map