Repository: jquense/yup
Pull request #643 (2019-10-02 07:43:08)

Fix typo


Patch:
diff --git a/README.md b/README.md
index 9344690..69b30af 100644
--- a/README.md
+++ b/README.md
@@ -201,6 +201,35 @@ schema.validate({ name: 'jimmy', age: 11 }).catch(function(err) {
 });
 ```
 
+If you need multi-language support, Yup has got you covered. The function `useLocale` accepts functions that can be used to generate error objects with translation keys and values. Just get this output and feed it into your favorite i18n library.
+
+```js
+import { setLocale } from 'yup';
+
+setLocale({
+  // use constant translation keys for messages without values
+  mixed: {
+    default: 'field_invalid',
+  },
+  // use functions to generate an error object that includes the value from the schema
+  number: {
+    min: ({ min }) => ({ key: 'field_too_short', values: { min } }),
+    max: ({ max }) => ({ key: 'field_too_big', values: { max } }),
+  },
+});
+
+// now use Yup schemas AFTER you defined your custom dictionary
+let schema = yup.object().shape({
+  name: yup.string(),
+  age: yup.number().min(18),
+});
+
+schema.validate({ name: 'jimmy', age: 11 }).catch(function(err) {
+  err.name; // => 'ValidationError'
+  err.errors; // => [{ key: 'field_too_short', values: { min: 18 } }]
+});
+```
+
 ## API
 
 ### `yup`
@@ -284,7 +313,7 @@ schema.cast({ foo: { bar: 'boom' } }, { context: { x: 5 } });
 #### `yup.lazy((value: any) => Schema): Lazy`
 
 Creates a schema that is evaluated at validation/cast time. Useful for creating
-recursive schema like Trees, for polymophic fields and arrays.
+recursive schema like Trees, for polymorphic fields and arrays.
 
 **CAUTION!** When defining parent-child recursive object schema, you want to reset the `default()`
 to `undefined` on the child otherwise the object will infinitely nest itself when you cast it!.
diff --git a/src/array.js b/src/array.js
index c053963..e66fead 100644
--- a/src/array.js
+++ b/src/array.js
@@ -46,8 +46,11 @@ inherits(ArraySchema, MixedSchema, {
     if (!this._typeCheck(value) || !this._subType) return value;
 
     let isChanged = false;
-    const castArray = value.map(v => {
-      const castElement = this._subType.cast(v, _opts);
+    const castArray = value.map((v, idx) => {
+      const castElement = this._subType.cast(v, {
+        ..._opts,
+        path: makePath`${_opts.path}[${idx}]`,
+      });
       if (castElement !== v) {
         isChanged = true;
       }
diff --git a/src/date.js b/src/date.js
index 08c1210..661341b 100644
--- a/src/date.js
+++ b/src/date.js
@@ -21,7 +21,8 @@ function DateSchema() {
       if (this.isType(value)) return value;
 
       value = isoParse(value);
-      return value ? new Date(value) : invalidDate;
+      // 0 is a valid timestamp equivalent to 1970-01-01T00:00:00Z(unix epoch)
+      return value > -1 ? new Date(value) : invalidDate;
     });
   });
 }


Test patch:
diff --git a/test/array.js b/test/array.js
index ff303ef..5c3bec5 100644
--- a/test/array.js
+++ b/test/array.js
@@ -175,4 +175,19 @@ describe('Array types', () => {
 
     inst.cast(null).should.eql([]);
   });
+
+  it('should pass resolved path to descendants', async () => {
+    let value = ['2', '3'];
+    let expectedPaths = ['[0]', '[1]'];
+
+    let itemSchema = string().when([], function(_, context) {
+      let path = context.path || '';
+      path.should.be.oneOf(expectedPaths);
+      return string().required();
+    });
+
+    await array()
+      .of(itemSchema)
+      .validate(value);
+  });
 });