diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9d08a1a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..03f07ee --- /dev/null +++ b/.eslintignore @@ -0,0 +1,6 @@ +it.js +it.d.ts +de.js +de.d.ts +en.js +en.d.ts diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..83c5ef6 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "parser": "@typescript-eslint/parser", + "env": { + "node": true, + "browser": true, + "es6": true, + "commonjs": true + }, + "plugins": [ + "@typescript-eslint", + "import", + "prettier" + ], + "extends": [ + "plugin:@typescript-eslint/recommended", + "plugin:prettier/recommended", + "plugin:import/recommended", + "plugin:import/typescript", + "plugin:jest/recommended" + ], + "parserOptions": { + "sourceType": "module", + "ecmaVersion": 2018 + }, + "rules": { + "@typescript-eslint/no-unused-vars": "error", + "@typescript-eslint/member-delimiter-style": "off", + "@typescript-eslint/explicit-function-return-type": "off", + "import/order": [ + "error", + { + "groups": ["builtin", "external", "unknown", "parent", "sibling", "index"] + } + ] + } +} diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..cba2354 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +set -o pipefail + +npm run lint diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d39e397 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,26 @@ +name: Create Relase & Publish Package +on: + push: + tags: + - 'v*' +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + - uses: actions/setup-node@v1 + with: + node-version: '16.x' + registry-url: 'https://registry.npmjs.org' + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ec820ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Basic +.DS_Store + +# IDE +/.idea +/.vscode + +# Project +/package-lock.json +/node_modules +/dist + +it.js +it.d.ts +de.js +de.d.ts +en.js +en.d.ts diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..fd496a8 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "singleQuote": true, + "semi": false +} diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..0959df4 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,9 @@ +module.exports = { + setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'], + transform: { + '^.+\\.tsx?$': 'babel-jest', + }, + moduleNameMapper: { + '\\.(css)$': 'identity-obj-proxy', + }, +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..0b1aead --- /dev/null +++ b/package.json @@ -0,0 +1,67 @@ +{ + "name": "@aboutbits/yup-locales", + "version": "0.0.0", + "description": "Custom yup locales", + "keywords": [ + "yup", + "internationalization", + "language", + "languages" + ], + "author": "AboutBits", + "license": "MIT", + "private": false, + "main": "en.js", + "typings": "en.d.ts", + "scripts": { + "build": "tsc", + "lint": "eslint --ext js,ts,tsx src", + "lint:fix": "npm run lint -- --fix", + "test": "jest --config jest.config.js --passWithNoTests", + "prepare": "npm run build", + "prepublishOnly": "npm run test && npm run lint", + "version": "npm run test && npm run lint && git add -A src", + "postversion": "git push && git push --tags" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/aboutbits/yup-locales.git" + }, + "files": [ + "readme.md", + "it.js", + "it.d.ts", + "de.js", + "de.d.ts", + "en.js", + "en.d.ts" + ], + "publishConfig": { + "access": "public" + }, + "bugs": { + "url": "https://github.com/aboutbits/yup-locales/issues" + }, + "homepage": "https://github.com/aboutbits/yup-locales#readme", + "engines": { + "npm": "^8", + "node": "^16" + }, + "devDependencies": { + "@types/jest": "^28.1.3", + "@types/node": "^16.0.0", + "@typescript-eslint/eslint-plugin": "^5.29.0", + "@typescript-eslint/parser": "^5.29.0", + "babel-jest": "^28.1.1", + "babel-loader": "^8.2.5", + "eslint": "^8.18.0", + "eslint-config-prettier": "^8.5.0", + "eslint-plugin-import": "^2.26.0", + "eslint-plugin-jest": "^26.5.3", + "eslint-plugin-prettier": "^4.0.0", + "jest": "^28.1.1", + "prettier": "^2.7.1", + "typescript": "^4.7.4", + "yup": "^0.32.11" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..c6b0f2a --- /dev/null +++ b/readme.md @@ -0,0 +1,56 @@ +Custom Yup Locales +==================== + +[![npm version](https://badge.fury.io/js/%40aboutbits%2Fyup-locales.svg)](https://badge.fury.io/js/%40aboutbits%2Fyup-locales) + +This package includes simplified translations for yup error messages. The main difference lies in the exclusion of the field path. + +## Table of content + +- [Usage](#usage) +- [Build & Publish](#build--publish) +- [Information](#information) + +## Usage + +First, you have to install the package: + +```bash +npm install @aboutbits/yup-locales +``` + +Second, you add the custom language packages to your project. + +```js +import { setLocale } from 'yup' +import { en, it, de } from '@aboutbits/yup-locales' + +// If you want that your app returns Italian error messages. +setLocale(it) +``` + +## Build & Publish + +To build and publish the package, simply commit all changes and push them to master. Then run one of the following commands locally: + +```bash +npm version patch +npm version minor +npm version major +``` + +## Information + +About Bits is a company based in South Tyrol, Italy. You can find more information about us on [our website](https://aboutbits.it). + +### Support + +For support, please contact [info@aboutbits.it](mailto:info@aboutbits.it). + +### Credits + +- [All Contributors](../../contributors) + +### License + +The MIT License (MIT). Please see the [license file](LICENSE.md) for more information. diff --git a/src/de.ts b/src/de.ts new file mode 100644 index 0000000..5ac2d4f --- /dev/null +++ b/src/de.ts @@ -0,0 +1,71 @@ +import { + MixedLocale, + StringLocale, + NumberLocale, + DateLocale, + BooleanLocale, + ObjectLocale, + ArrayLocale, + LocaleObject, +} from 'yup/es/locale' + +export const mixed: Required = { + default: 'Ungültiger Wert', + required: 'Pflichtfeld', + defined: 'Pflichtfeld', + oneOf: 'Gültige Werte: ${values}', + notOneOf: 'Ungültige Werte: ${values}', + notType: 'Ungültiger Typ', +} + +export const string: Required = { + length: 'Genau ${length} Zeichen', + min: 'Min ${min} Zeichen', + max: 'Max ${max} Zeichen', + matches: 'Format ungültig: "${regex}"', + email: 'Ungültige E-Mail', + url: 'Ungültige URL', + uuid: 'Ungültige UUID', + trim: 'Keine Leerzeichen am Anfang und Ende', + lowercase: 'Nur Kleinschreibung', + uppercase: 'Nur Großschreibung', +} + +export const number: Required = { + min: 'Min: ${min}', + max: 'Max: ${max}', + lessThan: 'Kleiner als ${less}', + moreThan: 'Größer als ${more}', + positive: 'Nur positive Zahlen', + negative: 'Nur negative Zahlen', + integer: 'Nur Zahlen erlaubt', +} + +export const date: Required = { + min: 'Datum nach ${min}', + max: 'Datum vor ${max}', +} + +export const boolean: BooleanLocale = { + isValue: 'Muss ${value} sein', +} + +export const object: Required = { + noUnknown: 'Unbekannter Wert', +} + +export const array: Required = { + min: 'Mindestens ${min} Einträge', + max: 'Maximal ${max} Einträge', + length: 'Genau ${length} Einträge', +} + +export default { + mixed, + string, + number, + date, + object, + array, + boolean, +} as LocaleObject diff --git a/src/en.ts b/src/en.ts new file mode 100644 index 0000000..09e26e1 --- /dev/null +++ b/src/en.ts @@ -0,0 +1,71 @@ +import { + MixedLocale, + StringLocale, + NumberLocale, + DateLocale, + BooleanLocale, + ObjectLocale, + ArrayLocale, + LocaleObject, +} from 'yup/es/locale' + +export const mixed: Required = { + default: 'Invalid value', + required: 'Required', + defined: 'Required', + oneOf: 'One of: ${values}', + notOneOf: 'Not one of: ${values}', + notType: 'Invalid type', +} + +export const string: Required = { + length: 'Must be exactly ${length} characters', + min: 'Min ${min} characters', + max: 'Max ${max} characters', + matches: 'Must match: "${regex}"', + email: 'Invalid email', + url: 'Invalid URL', + uuid: 'Invalid UUID', + trim: 'Not trimmed', + lowercase: 'Lowercase only', + uppercase: 'Uppercase only', +} + +export const number: Required = { + min: 'Min: ${min}', + max: 'Max: ${max}', + lessThan: 'Less than ${less}', + moreThan: 'Greater than ${more}', + positive: 'Positive number', + negative: 'Negative number', + integer: 'Numbers only', +} + +export const date: Required = { + min: 'Later than ${min}', + max: 'Earlier than ${max}', +} + +export const boolean: BooleanLocale = { + isValue: 'Must be ${value}', +} + +export const object: Required = { + noUnknown: 'Unknown value', +} + +export const array: Required = { + min: 'At least ${min} items', + max: 'Not more than ${max} items', + length: 'Exactly ${length} items', +} + +export default { + mixed, + string, + number, + date, + object, + array, + boolean, +} as LocaleObject diff --git a/src/it.ts b/src/it.ts new file mode 100644 index 0000000..062ba90 --- /dev/null +++ b/src/it.ts @@ -0,0 +1,71 @@ +import { + MixedLocale, + StringLocale, + NumberLocale, + DateLocale, + BooleanLocale, + ObjectLocale, + ArrayLocale, + LocaleObject, +} from 'yup/es/locale' + +export const mixed: Required = { + default: 'Valore non valido', + required: 'Campo obbligatorio', + defined: 'Campo obbligatorio', + oneOf: 'Valori validi: ${values}', + notOneOf: 'Valori non validi: ${values}', + notType: 'Tipo non valido', +} + +export const string: Required = { + length: 'Caratteri esatti: ${length}', + min: 'Min ${min} caratteri', + max: 'Max ${max} caratteri', + matches: 'Formato non valido: "${regex}"', + email: 'Email non valida', + url: 'URL non valido', + uuid: 'UUID non valido', + trim: "Senza spazi all'inizio e alla fine", + lowercase: 'Solo minuscolo', + uppercase: 'Solo maiuscole', +} + +export const number: Required = { + min: 'Min: ${min}', + max: 'Max: ${max}', + lessThan: 'Meno di ${less}', + moreThan: 'Più di ${more}', + positive: 'Solo numeri positivi', + negative: 'Solo numeri negativi', + integer: 'Solo numeri', +} + +export const date: Required = { + min: 'Data dopo ${min}', + max: 'Data precedente a ${max}', +} + +export const boolean: BooleanLocale = { + isValue: 'Deve essere ${value}', +} + +export const object: Required = { + noUnknown: 'Valore sconosciuto', +} + +export const array: Required = { + min: 'Inserimenti minimi ${min}', + max: 'Massimo di voci ${max}', + length: 'Esattamente ${length} voci', +} + +export default { + mixed, + string, + number, + date, + object, + array, + boolean, +} as LocaleObject diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2089bef --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "CommonJS", + "moduleResolution": "node", + "declaration": true, + "outDir": "./", + "strict": true, + "esModuleInterop": true + }, + "include": ["src"], + "exclude": ["node_modules", "**/__test__/*", "**/__tests__/*"] +}