@sanity/validation 3.2.6-client-v5.17 → 3.2.6

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.
@@ -1,22 +1,12 @@
1
1
  import {Observable} from 'rxjs'
2
2
  import {RuleClass} from '@sanity/types'
3
+ import type {SanityClient} from '@sanity/client'
3
4
  import {SanityDocument} from '@sanity/types'
4
5
  import {Schema} from '@sanity/types'
5
6
  import {SchemaType} from '@sanity/types'
6
7
  import {ValidationContext} from '@sanity/types'
7
8
  import {ValidationMarker} from '@sanity/types'
8
9
 
9
- /**
10
- * this is used make optional properties required by replacing optionals with
11
- * `T[P] | undefined`. this is used to prevent errors in `validateItem` where
12
- * an option from a previous invocation would be incorrectly passed down.
13
- *
14
- * https://medium.com/terria/typescript-transforming-optional-properties-to-required-properties-that-may-be-undefined-7482cb4e1585
15
- */
16
- declare type ExplicitUndefined<T> = {
17
- [P in keyof Required<T>]: Pick<T, P> extends Required<Pick<T, P>> ? T[P] : T[P] | undefined
18
- }
19
-
20
10
  export declare function inferFromSchema(schema: Schema): Schema
21
11
 
22
12
  export declare function inferFromSchemaType(
@@ -30,21 +20,17 @@ export declare function inferFromSchemaType(typeDef: SchemaType): SchemaType
30
20
  export declare const Rule: RuleClass
31
21
 
32
22
  export declare function validateDocument(
33
- getClient: ValidateItemOptions['getClient'],
23
+ getClient: (options: {apiVersion: string}) => SanityClient,
34
24
  doc: SanityDocument,
35
25
  schema: Schema,
36
26
  context?: Pick<ValidationContext, 'getDocumentExists'>
37
27
  ): Promise<ValidationMarker[]>
38
28
 
39
29
  export declare function validateDocumentObservable(
40
- getClient: ValidateItemOptions['getClient'],
30
+ getClient: (options: {apiVersion: string}) => SanityClient,
41
31
  doc: SanityDocument,
42
32
  schema: Schema,
43
33
  context?: Pick<ValidationContext, 'getDocumentExists'>
44
34
  ): Observable<ValidationMarker[]>
45
35
 
46
- declare type ValidateItemOptions = {
47
- value: unknown
48
- } & ExplicitUndefined<ValidationContext>
49
-
50
36
  export {}
package/lib/index.cjs.mjs CHANGED
@@ -5,5 +5,3 @@ export const inferFromSchema = cjs.inferFromSchema;
5
5
  export const inferFromSchemaType = cjs.inferFromSchemaType;
6
6
  export const validateDocument = cjs.validateDocument;
7
7
  export const validateDocumentObservable = cjs.validateDocumentObservable;
8
-
9
- export default cjs.default;
package/lib/index.esm.js CHANGED
@@ -520,7 +520,7 @@ const getFormattedDate = function () {
520
520
  function parseDate(date) {
521
521
  let throwOnError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
522
522
  if (!date) return null;
523
- if (date === "now") return /* @__PURE__ */new Date();
523
+ if (date === "now") return new Date();
524
524
  const parsed = new Date(date);
525
525
  const isInvalid = isNaN(parsed.getTime());
526
526
  if (isInvalid && throwOnError) {
@@ -1117,7 +1117,11 @@ function validateDocumentObservable(getClient, doc, schema, context) {
1117
1117
  console.warn('Schema type for object type "%s" not found, skipping validation', doc._type);
1118
1118
  return of(EMPTY_MARKERS);
1119
1119
  }
1120
+ const client = getClient({
1121
+ apiVersion: "2021-06-07"
1122
+ });
1120
1123
  const validationOptions = {
1124
+ client,
1121
1125
  getClient,
1122
1126
  schema,
1123
1127
  parent: void 0,
@@ -1127,6 +1131,16 @@ function validateDocumentObservable(getClient, doc, schema, context) {
1127
1131
  type: documentType,
1128
1132
  getDocumentExists: context == null ? void 0 : context.getDocumentExists
1129
1133
  };
1134
+ const wrappedClient = client;
1135
+ validationOptions.client = [...Object.keys(client), ...Object.keys(wrappedClient.__proto__)].reduce((acc, key) => {
1136
+ const original = Object.hasOwnProperty.call(client, key) ? wrappedClient[key] : wrappedClient.__proto__[key];
1137
+ return Object.defineProperty(acc, key, {
1138
+ get() {
1139
+ console.warn('`configContext.client` is deprecated and will be removed in the next release! Use `context.getClient({apiVersion: "2021-06-07"})` instead');
1140
+ return original;
1141
+ }
1142
+ });
1143
+ }, {});
1130
1144
  return validateItemObservable(validationOptions).pipe(catchError(err => {
1131
1145
  console.error(err);
1132
1146
  return of([{
@@ -1157,12 +1171,7 @@ function validateItemObservable(_ref) {
1157
1171
  const selfIsRequired = rules.some(rule => rule.isRequired());
1158
1172
  const shouldRunNestedObjectValidation =
1159
1173
  // run nested validation for objects
1160
- (type == null ? void 0 : type.jsonType) === "object" && (
1161
- // if the value is truthy
1162
- !!value ||
1163
- // or
1164
- // (the value is null or undefined) and the top-level value is required
1165
- (value === null || value === void 0) && selfIsRequired);
1174
+ (type == null ? void 0 : type.jsonType) === "object" && (!!value || (value === null || value === void 0) && selfIsRequired);
1166
1175
  if (shouldRunNestedObjectValidation) {
1167
1176
  const fieldTypes = type.fields.reduce((acc, field) => {
1168
1177
  acc[field.name] = field.type;