@warlock.js/core 3.0.12 → 3.0.13
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.
- package/cjs/application.js +1 -1
- package/cjs/application.js.map +1 -1
- package/cjs/console/commander.js +1 -1
- package/cjs/console/commander.js.map +1 -1
- package/cjs/http/errors/index.d.ts +2 -0
- package/cjs/http/errors/index.d.ts.map +1 -0
- package/cjs/http/errors/resource-not-found.error.d.ts +13 -0
- package/cjs/http/errors/resource-not-found.error.d.ts.map +1 -0
- package/cjs/http/errors/resource-not-found.error.js +24 -0
- package/cjs/http/errors/resource-not-found.error.js.map +1 -0
- package/cjs/http/index.d.ts +1 -0
- package/cjs/http/index.d.ts.map +1 -1
- package/cjs/http/middleware/_inject-request-context.d.ts.map +1 -1
- package/cjs/http/middleware/cache-response-middleware.d.ts.map +1 -1
- package/cjs/http/middleware/cache-response-middleware.js +0 -1
- package/cjs/http/middleware/cache-response-middleware.js.map +1 -1
- package/cjs/http/plugins/index.d.ts +1 -1
- package/cjs/http/plugins/index.d.ts.map +1 -1
- package/cjs/http/request.d.ts +20 -5
- package/cjs/http/request.d.ts.map +1 -1
- package/cjs/http/request.js +34 -5
- package/cjs/http/request.js.map +1 -1
- package/cjs/http/response.d.ts +3 -2
- package/cjs/http/response.d.ts.map +1 -1
- package/cjs/http/response.js +15 -4
- package/cjs/http/response.js.map +1 -1
- package/cjs/http/types.d.ts +12 -0
- package/cjs/http/types.d.ts.map +1 -1
- package/cjs/index.d.ts +2 -0
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +1 -1
- package/cjs/modules/uploads/controllers/upload-files.d.ts.map +1 -1
- package/cjs/modules/uploads/controllers/upload-files.js +7 -7
- package/cjs/modules/uploads/controllers/upload-files.js.map +1 -1
- package/cjs/modules/uploads/output/upload-output.d.ts.map +1 -1
- package/cjs/modules/uploads/output/upload-output.js +2 -2
- package/cjs/modules/uploads/output/upload-output.js.map +1 -1
- package/cjs/modules/uploads/utils/get-watermark-options.js +1 -1
- package/cjs/modules/uploads/utils/get-watermark-options.js.map +1 -1
- package/cjs/output/output.js +1 -1
- package/cjs/output/output.js.map +1 -1
- package/cjs/react/index.d.ts +1 -10
- package/cjs/react/index.d.ts.map +1 -1
- package/cjs/react/index.js +3 -4
- package/cjs/react/index.js.map +1 -1
- package/cjs/repositories/repository-list-manager.js +1 -1
- package/cjs/repositories/repository-list-manager.js.map +1 -1
- package/cjs/router/router.d.ts +0 -8
- package/cjs/router/router.d.ts.map +1 -1
- package/cjs/router/router.js +3 -36
- package/cjs/router/router.js.map +1 -1
- package/cjs/router/types.d.ts +1 -1
- package/cjs/router/types.d.ts.map +1 -1
- package/cjs/starters/http-server-starter.d.ts.map +1 -1
- package/cjs/starters/http-server-starter.js +7 -2
- package/cjs/starters/http-server-starter.js.map +1 -1
- package/cjs/starters/start-cli-server.d.ts.map +1 -1
- package/cjs/starters/start-cli-server.js +10 -1
- package/cjs/starters/start-cli-server.js.map +1 -1
- package/cjs/starters/start-http-server.d.ts +3 -2
- package/cjs/starters/start-http-server.d.ts.map +1 -1
- package/cjs/starters/start-http-server.js +12 -6
- package/cjs/starters/start-http-server.js.map +1 -1
- package/cjs/utils/date-output.d.ts.map +1 -1
- package/cjs/utils/date-output.js +2 -4
- package/cjs/utils/date-output.js.map +1 -1
- package/cjs/utils/get-localized.d.ts.map +1 -1
- package/cjs/utils/get-localized.js.map +1 -1
- package/cjs/v/database/exists-except-current-id.d.ts +7 -0
- package/cjs/v/database/exists-except-current-id.d.ts.map +1 -0
- package/cjs/v/database/exists-except-current-id.js +23 -0
- package/cjs/v/database/exists-except-current-id.js.map +1 -0
- package/cjs/v/database/exists-except-current-user.d.ts +7 -0
- package/cjs/v/database/exists-except-current-user.d.ts.map +1 -0
- package/cjs/v/database/exists-except-current-user.js +25 -0
- package/cjs/v/database/exists-except-current-user.js.map +1 -0
- package/cjs/v/database/exists.d.ts +7 -0
- package/cjs/v/database/exists.d.ts.map +1 -0
- package/cjs/v/database/exists.js +21 -0
- package/cjs/v/database/exists.js.map +1 -0
- package/cjs/v/database/index.d.ts +12 -0
- package/cjs/v/database/index.d.ts.map +1 -0
- package/cjs/v/database/types.d.ts +68 -0
- package/cjs/v/database/types.d.ts.map +1 -0
- package/cjs/v/database/unique-except-current-id.d.ts +7 -0
- package/cjs/v/database/unique-except-current-id.d.ts.map +1 -0
- package/cjs/v/database/unique-except-current-id.js +23 -0
- package/cjs/v/database/unique-except-current-id.js.map +1 -0
- package/cjs/v/database/unique-except-current-user.d.ts +7 -0
- package/cjs/v/database/unique-except-current-user.d.ts.map +1 -0
- package/cjs/v/database/unique-except-current-user.js +25 -0
- package/cjs/v/database/unique-except-current-user.js.map +1 -0
- package/cjs/v/database/unique.d.ts +7 -0
- package/cjs/v/database/unique.d.ts.map +1 -0
- package/cjs/v/database/unique.js +30 -0
- package/cjs/v/database/unique.js.map +1 -0
- package/cjs/v/file/file.d.ts +22 -0
- package/cjs/v/file/file.d.ts.map +1 -0
- package/cjs/v/file/file.js +60 -0
- package/cjs/v/file/file.js.map +1 -0
- package/cjs/v/file/index.d.ts +6 -0
- package/cjs/v/file/index.d.ts.map +1 -0
- package/cjs/v/index.d.ts +17 -0
- package/cjs/v/index.d.ts.map +1 -0
- package/cjs/v/init.d.ts +7 -0
- package/cjs/v/init.d.ts.map +1 -0
- package/cjs/v/init.js +40 -0
- package/cjs/v/init.js.map +1 -0
- package/cjs/v/plugins/database-plugin.d.ts +14 -0
- package/cjs/v/plugins/database-plugin.d.ts.map +1 -0
- package/cjs/v/plugins/database-plugin.js +95 -0
- package/cjs/v/plugins/database-plugin.js.map +1 -0
- package/cjs/v/plugins/file-plugin.d.ts +11 -0
- package/cjs/v/plugins/file-plugin.d.ts.map +1 -0
- package/cjs/v/plugins/file-plugin.js +17 -0
- package/cjs/v/plugins/file-plugin.js.map +1 -0
- package/cjs/v/plugins/index.d.ts +8 -0
- package/cjs/v/plugins/index.d.ts.map +1 -0
- package/cjs/v/string/index.d.ts +6 -0
- package/cjs/v/string/index.d.ts.map +1 -0
- package/cjs/v/string/uploadable.d.ts +6 -0
- package/cjs/v/string/uploadable.d.ts.map +1 -0
- package/cjs/v/string/uploadable.js +14 -0
- package/cjs/v/string/uploadable.js.map +1 -0
- package/cjs/v/types.d.ts +53 -0
- package/cjs/v/types.d.ts.map +1 -0
- package/cjs/v/validators/file-validator.d.ts +36 -0
- package/cjs/v/validators/file-validator.d.ts.map +1 -0
- package/cjs/v/validators/file-validator.js +88 -0
- package/cjs/v/validators/file-validator.js.map +1 -0
- package/cjs/v/validators/index.d.ts +7 -0
- package/cjs/v/validators/index.d.ts.map +1 -0
- package/cjs/validator/index.d.ts +0 -1
- package/cjs/validator/index.d.ts.map +1 -1
- package/cjs/validator/locales.js +518 -10
- package/cjs/validator/locales.js.map +1 -1
- package/cjs/validator/types.d.ts +28 -0
- package/cjs/validator/types.d.ts.map +1 -1
- package/cjs/validator/validateAll.d.ts.map +1 -1
- package/cjs/validator/validateAll.js +5 -2
- package/cjs/validator/validateAll.js.map +1 -1
- package/esm/application.js +1 -1
- package/esm/application.js.map +1 -1
- package/esm/console/commander.js +1 -1
- package/esm/console/commander.js.map +1 -1
- package/esm/http/errors/index.d.ts +2 -0
- package/esm/http/errors/index.d.ts.map +1 -0
- package/esm/http/errors/resource-not-found.error.d.ts +13 -0
- package/esm/http/errors/resource-not-found.error.d.ts.map +1 -0
- package/esm/http/errors/resource-not-found.error.js +24 -0
- package/esm/http/errors/resource-not-found.error.js.map +1 -0
- package/esm/http/index.d.ts +1 -0
- package/esm/http/index.d.ts.map +1 -1
- package/esm/http/middleware/_inject-request-context.d.ts.map +1 -1
- package/esm/http/middleware/cache-response-middleware.d.ts.map +1 -1
- package/esm/http/middleware/cache-response-middleware.js +0 -1
- package/esm/http/middleware/cache-response-middleware.js.map +1 -1
- package/esm/http/plugins/index.d.ts +1 -1
- package/esm/http/plugins/index.d.ts.map +1 -1
- package/esm/http/request.d.ts +20 -5
- package/esm/http/request.d.ts.map +1 -1
- package/esm/http/request.js +34 -5
- package/esm/http/request.js.map +1 -1
- package/esm/http/response.d.ts +3 -2
- package/esm/http/response.d.ts.map +1 -1
- package/esm/http/response.js +16 -5
- package/esm/http/response.js.map +1 -1
- package/esm/http/types.d.ts +12 -0
- package/esm/http/types.d.ts.map +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/modules/uploads/controllers/upload-files.d.ts.map +1 -1
- package/esm/modules/uploads/controllers/upload-files.js +2 -2
- package/esm/modules/uploads/controllers/upload-files.js.map +1 -1
- package/esm/modules/uploads/output/upload-output.d.ts.map +1 -1
- package/esm/modules/uploads/output/upload-output.js +2 -2
- package/esm/modules/uploads/output/upload-output.js.map +1 -1
- package/esm/modules/uploads/utils/get-watermark-options.js +1 -1
- package/esm/modules/uploads/utils/get-watermark-options.js.map +1 -1
- package/esm/output/output.js +1 -1
- package/esm/output/output.js.map +1 -1
- package/esm/react/index.d.ts +1 -10
- package/esm/react/index.d.ts.map +1 -1
- package/esm/react/index.js +2 -3
- package/esm/react/index.js.map +1 -1
- package/esm/repositories/repository-list-manager.js +1 -1
- package/esm/repositories/repository-list-manager.js.map +1 -1
- package/esm/router/router.d.ts +0 -8
- package/esm/router/router.d.ts.map +1 -1
- package/esm/router/router.js +3 -36
- package/esm/router/router.js.map +1 -1
- package/esm/router/types.d.ts +1 -1
- package/esm/router/types.d.ts.map +1 -1
- package/esm/starters/http-server-starter.d.ts.map +1 -1
- package/esm/starters/http-server-starter.js +7 -2
- package/esm/starters/http-server-starter.js.map +1 -1
- package/esm/starters/start-cli-server.d.ts.map +1 -1
- package/esm/starters/start-cli-server.js +10 -1
- package/esm/starters/start-cli-server.js.map +1 -1
- package/esm/starters/start-http-server.d.ts +3 -2
- package/esm/starters/start-http-server.d.ts.map +1 -1
- package/esm/starters/start-http-server.js +10 -4
- package/esm/starters/start-http-server.js.map +1 -1
- package/esm/utils/date-output.d.ts.map +1 -1
- package/esm/utils/date-output.js +2 -4
- package/esm/utils/date-output.js.map +1 -1
- package/esm/utils/get-localized.d.ts.map +1 -1
- package/esm/utils/get-localized.js.map +1 -1
- package/esm/v/database/exists-except-current-id.d.ts +7 -0
- package/esm/v/database/exists-except-current-id.d.ts.map +1 -0
- package/esm/v/database/exists-except-current-id.js +23 -0
- package/esm/v/database/exists-except-current-id.js.map +1 -0
- package/esm/v/database/exists-except-current-user.d.ts +7 -0
- package/esm/v/database/exists-except-current-user.d.ts.map +1 -0
- package/esm/v/database/exists-except-current-user.js +25 -0
- package/esm/v/database/exists-except-current-user.js.map +1 -0
- package/esm/v/database/exists.d.ts +7 -0
- package/esm/v/database/exists.d.ts.map +1 -0
- package/esm/v/database/exists.js +21 -0
- package/esm/v/database/exists.js.map +1 -0
- package/esm/v/database/index.d.ts +12 -0
- package/esm/v/database/index.d.ts.map +1 -0
- package/esm/v/database/types.d.ts +68 -0
- package/esm/v/database/types.d.ts.map +1 -0
- package/esm/v/database/unique-except-current-id.d.ts +7 -0
- package/esm/v/database/unique-except-current-id.d.ts.map +1 -0
- package/esm/v/database/unique-except-current-id.js +23 -0
- package/esm/v/database/unique-except-current-id.js.map +1 -0
- package/esm/v/database/unique-except-current-user.d.ts +7 -0
- package/esm/v/database/unique-except-current-user.d.ts.map +1 -0
- package/esm/v/database/unique-except-current-user.js +25 -0
- package/esm/v/database/unique-except-current-user.js.map +1 -0
- package/esm/v/database/unique.d.ts +7 -0
- package/esm/v/database/unique.d.ts.map +1 -0
- package/esm/v/database/unique.js +30 -0
- package/esm/v/database/unique.js.map +1 -0
- package/esm/v/file/file.d.ts +22 -0
- package/esm/v/file/file.d.ts.map +1 -0
- package/esm/v/file/file.js +60 -0
- package/esm/v/file/file.js.map +1 -0
- package/esm/v/file/index.d.ts +6 -0
- package/esm/v/file/index.d.ts.map +1 -0
- package/esm/v/index.d.ts +17 -0
- package/esm/v/index.d.ts.map +1 -0
- package/esm/v/init.d.ts +7 -0
- package/esm/v/init.d.ts.map +1 -0
- package/esm/v/init.js +40 -0
- package/esm/v/init.js.map +1 -0
- package/esm/v/plugins/database-plugin.d.ts +14 -0
- package/esm/v/plugins/database-plugin.d.ts.map +1 -0
- package/esm/v/plugins/database-plugin.js +95 -0
- package/esm/v/plugins/database-plugin.js.map +1 -0
- package/esm/v/plugins/file-plugin.d.ts +11 -0
- package/esm/v/plugins/file-plugin.d.ts.map +1 -0
- package/esm/v/plugins/file-plugin.js +17 -0
- package/esm/v/plugins/file-plugin.js.map +1 -0
- package/esm/v/plugins/index.d.ts +8 -0
- package/esm/v/plugins/index.d.ts.map +1 -0
- package/esm/v/string/index.d.ts +6 -0
- package/esm/v/string/index.d.ts.map +1 -0
- package/esm/v/string/uploadable.d.ts +6 -0
- package/esm/v/string/uploadable.d.ts.map +1 -0
- package/esm/v/string/uploadable.js +14 -0
- package/esm/v/string/uploadable.js.map +1 -0
- package/esm/v/types.d.ts +53 -0
- package/esm/v/types.d.ts.map +1 -0
- package/esm/v/validators/file-validator.d.ts +36 -0
- package/esm/v/validators/file-validator.d.ts.map +1 -0
- package/esm/v/validators/file-validator.js +88 -0
- package/esm/v/validators/file-validator.js.map +1 -0
- package/esm/v/validators/index.d.ts +7 -0
- package/esm/v/validators/index.d.ts.map +1 -0
- package/esm/validator/index.d.ts +0 -1
- package/esm/validator/index.d.ts.map +1 -1
- package/esm/validator/locales.js +518 -10
- package/esm/validator/locales.js.map +1 -1
- package/esm/validator/types.d.ts +28 -0
- package/esm/validator/types.d.ts.map +1 -1
- package/esm/validator/validateAll.d.ts.map +1 -1
- package/esm/validator/validateAll.js +4 -1
- package/esm/validator/validateAll.js.map +1 -1
- package/package.json +3 -3
- package/cjs/react/page-renderer.d.ts +0 -18
- package/cjs/react/page-renderer.d.ts.map +0 -1
- package/cjs/react/page-renderer.js +0 -69
- package/cjs/react/page-renderer.js.map +0 -1
- package/cjs/react/plugin.d.ts +0 -15
- package/cjs/react/plugin.d.ts.map +0 -1
- package/cjs/react/plugin.js +0 -22
- package/cjs/react/plugin.js.map +0 -1
- package/cjs/react/vite-config.d.ts +0 -9
- package/cjs/react/vite-config.d.ts.map +0 -1
- package/cjs/react/vite-config.js +0 -74
- package/cjs/react/vite-config.js.map +0 -1
- package/cjs/validator/v/array-validator.d.ts +0 -21
- package/cjs/validator/v/array-validator.d.ts.map +0 -1
- package/cjs/validator/v/base-validator.d.ts +0 -117
- package/cjs/validator/v/base-validator.d.ts.map +0 -1
- package/cjs/validator/v/database.d.ts +0 -8
- package/cjs/validator/v/database.d.ts.map +0 -1
- package/cjs/validator/v/database.js +0 -129
- package/cjs/validator/v/database.js.map +0 -1
- package/cjs/validator/v/index.d.ts +0 -3
- package/cjs/validator/v/index.d.ts.map +0 -1
- package/cjs/validator/v/mutators.d.ts +0 -24
- package/cjs/validator/v/mutators.d.ts.map +0 -1
- package/cjs/validator/v/mutators.js +0 -88
- package/cjs/validator/v/mutators.js.map +0 -1
- package/cjs/validator/v/number-validator.d.ts +0 -21
- package/cjs/validator/v/number-validator.d.ts.map +0 -1
- package/cjs/validator/v/object-validator.d.ts +0 -37
- package/cjs/validator/v/object-validator.d.ts.map +0 -1
- package/cjs/validator/v/rules.d.ts +0 -265
- package/cjs/validator/v/rules.d.ts.map +0 -1
- package/cjs/validator/v/rules.js +0 -914
- package/cjs/validator/v/rules.js.map +0 -1
- package/cjs/validator/v/schema.d.ts +0 -625
- package/cjs/validator/v/schema.d.ts.map +0 -1
- package/cjs/validator/v/schema.js +0 -1212
- package/cjs/validator/v/schema.js.map +0 -1
- package/cjs/validator/v/string-validator.d.ts +0 -41
- package/cjs/validator/v/string-validator.d.ts.map +0 -1
- package/cjs/validator/v/types.d.ts +0 -180
- package/cjs/validator/v/types.d.ts.map +0 -1
- package/cjs/validator/v/utils.d.ts +0 -5
- package/cjs/validator/v/utils.d.ts.map +0 -1
- package/cjs/validator/v/utils.js +0 -29
- package/cjs/validator/v/utils.js.map +0 -1
- package/esm/react/page-renderer.d.ts +0 -18
- package/esm/react/page-renderer.d.ts.map +0 -1
- package/esm/react/page-renderer.js +0 -69
- package/esm/react/page-renderer.js.map +0 -1
- package/esm/react/plugin.d.ts +0 -15
- package/esm/react/plugin.d.ts.map +0 -1
- package/esm/react/plugin.js +0 -22
- package/esm/react/plugin.js.map +0 -1
- package/esm/react/vite-config.d.ts +0 -9
- package/esm/react/vite-config.d.ts.map +0 -1
- package/esm/react/vite-config.js +0 -74
- package/esm/react/vite-config.js.map +0 -1
- package/esm/validator/v/array-validator.d.ts +0 -21
- package/esm/validator/v/array-validator.d.ts.map +0 -1
- package/esm/validator/v/base-validator.d.ts +0 -117
- package/esm/validator/v/base-validator.d.ts.map +0 -1
- package/esm/validator/v/database.d.ts +0 -8
- package/esm/validator/v/database.d.ts.map +0 -1
- package/esm/validator/v/database.js +0 -129
- package/esm/validator/v/database.js.map +0 -1
- package/esm/validator/v/index.d.ts +0 -3
- package/esm/validator/v/index.d.ts.map +0 -1
- package/esm/validator/v/mutators.d.ts +0 -24
- package/esm/validator/v/mutators.d.ts.map +0 -1
- package/esm/validator/v/mutators.js +0 -88
- package/esm/validator/v/mutators.js.map +0 -1
- package/esm/validator/v/number-validator.d.ts +0 -21
- package/esm/validator/v/number-validator.d.ts.map +0 -1
- package/esm/validator/v/object-validator.d.ts +0 -37
- package/esm/validator/v/object-validator.d.ts.map +0 -1
- package/esm/validator/v/rules.d.ts +0 -265
- package/esm/validator/v/rules.d.ts.map +0 -1
- package/esm/validator/v/rules.js +0 -914
- package/esm/validator/v/rules.js.map +0 -1
- package/esm/validator/v/schema.d.ts +0 -625
- package/esm/validator/v/schema.d.ts.map +0 -1
- package/esm/validator/v/schema.js +0 -1212
- package/esm/validator/v/schema.js.map +0 -1
- package/esm/validator/v/string-validator.d.ts +0 -41
- package/esm/validator/v/string-validator.d.ts.map +0 -1
- package/esm/validator/v/types.d.ts +0 -180
- package/esm/validator/v/types.d.ts.map +0 -1
- package/esm/validator/v/utils.d.ts +0 -5
- package/esm/validator/v/utils.d.ts.map +0 -1
- package/esm/validator/v/utils.js +0 -29
- package/esm/validator/v/utils.js.map +0 -1
|
@@ -1,1212 +0,0 @@
|
|
|
1
|
-
import {trans}from'@mongez/localization';import {clone}from'@mongez/reinforcements';import {isPlainObject,isObject}from'@mongez/supportive-is';import {stripUnknownMutator,objectTrimMutator,flipArrayMutator,uniqueArrayMutator,sortArrayMutator,stringMutator,lowercaseMutator,uppercaseMutator,capitalizeMutator,dateMutator,numberMutator}from'./mutators.js';import {equalRule,requiredRule,requiredWithRule,requiredIfAbsentRule,requiredIfSiblingFieldIsAbsentRule,requiredIfSiblingFieldAllAbsentRule,requiredIfEmptyRule,requiredIfSiblingFieldEmptyRule,requiredIfFieldRule,requiredIfSiblingFieldRule,requiredUnlessSiblingFieldRule,forbiddenRule,whenRule,objectRule,unknownKeyRule,arrayRule,minLengthRule,maxLengthRule,lengthRule,uniqueArrayRule,stringRule,emailRule,urlRule,matchesRule,withoutWhitespaceRule,patternRule,uploadableRule,wordsRule,minWordsRule,maxWordsRule,alphaRule,alphaNumericRule,isNumericRule,startsWithRule,endsWithRule,containsRule,notContainsRule,ipRule,ip4Rule,ip6Rule,isCreditCardRule,colorRule,hexColorRule,hslColorRule,rgbColorRule,rgbaColorRule,lightColorRule,darkColorRule,dateRule,maxDateRule,minDateRule,intRule,floatRule,booleanRule,scalarRule,enumRule,inRule,allowedValuesRule,notAllowedValuesRule,fileRule,imageRule,fileExtensionRule,fileTypeRule,minFileSizeRule,maxFileSizeRule,minWidthRule,maxWidthRule,minHeightRule,maxHeightRule,numberRule,minRule,maxRule,moduloRule,positiveRule}from'./rules.js';import {invalidRule,VALID_RULE,setKeyPath}from'./utils.js';import {uniqueRule,uniqueExceptCurrentUserRule,uniqueExceptCurrentIdRule,existsRule,existsExceptCurrentUserRule,existsExceptCurrentIdRule}from'./database.js';/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
-
// TODO: Allow developer to extend the validator with custom rules and custom validators
|
|
3
|
-
class BaseValidator {
|
|
4
|
-
rules = [];
|
|
5
|
-
mutators = [];
|
|
6
|
-
defaultValue;
|
|
7
|
-
description;
|
|
8
|
-
/**
|
|
9
|
-
* Get the default value
|
|
10
|
-
*/
|
|
11
|
-
getDefaultValue() {
|
|
12
|
-
return this.defaultValue;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Add description to the validator
|
|
16
|
-
*/
|
|
17
|
-
describe(description) {
|
|
18
|
-
this.description = description;
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Value must be equal to the given value
|
|
23
|
-
*/
|
|
24
|
-
equal(value, errorMessage) {
|
|
25
|
-
const rule = this.addRule(equalRule, errorMessage);
|
|
26
|
-
rule.context.options.value = value;
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Add rule to the schema
|
|
31
|
-
*/
|
|
32
|
-
addRule(rule, errorMessage) {
|
|
33
|
-
const newRule = {
|
|
34
|
-
...clone(rule),
|
|
35
|
-
context: {
|
|
36
|
-
errorMessage,
|
|
37
|
-
options: {},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
if (errorMessage) {
|
|
41
|
-
newRule.errorMessage = errorMessage;
|
|
42
|
-
}
|
|
43
|
-
if (rule.sortOrder === undefined) {
|
|
44
|
-
newRule.sortOrder = this.rules.length + 1;
|
|
45
|
-
}
|
|
46
|
-
this.rules.push(newRule);
|
|
47
|
-
return newRule;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Define custom rule
|
|
51
|
-
*/
|
|
52
|
-
refine(callback) {
|
|
53
|
-
this.addRule({
|
|
54
|
-
name: "custom",
|
|
55
|
-
async validate(value, context) {
|
|
56
|
-
const result = await callback(value, context);
|
|
57
|
-
if (result) {
|
|
58
|
-
this.context.errorMessage = result;
|
|
59
|
-
return invalidRule(this, context);
|
|
60
|
-
}
|
|
61
|
-
return VALID_RULE;
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Add custom rule to the schema
|
|
68
|
-
* @alias refine
|
|
69
|
-
*/
|
|
70
|
-
custom(callback) {
|
|
71
|
-
return this.refine(callback);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Add mutator to the schema
|
|
75
|
-
*
|
|
76
|
-
* A mutator is a function that mutates the value of the field before validation
|
|
77
|
-
*/
|
|
78
|
-
addMutator(mutator, options = {}) {
|
|
79
|
-
this.mutators.push({
|
|
80
|
-
mutate: mutator,
|
|
81
|
-
context: {
|
|
82
|
-
options,
|
|
83
|
-
ctx: {},
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
return this;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Set default value for the field
|
|
90
|
-
*/
|
|
91
|
-
default(value) {
|
|
92
|
-
this.defaultValue = value;
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Value must be present but not necessarily has a value
|
|
97
|
-
*/
|
|
98
|
-
present(errorMessage) {
|
|
99
|
-
this.addRule(requiredRule, errorMessage);
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* This value must be present and has a value
|
|
104
|
-
*/
|
|
105
|
-
required(errorMessage) {
|
|
106
|
-
this.addRule(requiredRule, errorMessage);
|
|
107
|
-
return this;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* This value must be present if the given input is present
|
|
111
|
-
*/
|
|
112
|
-
requiredWith(input, errorMessage) {
|
|
113
|
-
const rule = this.addRule(requiredWithRule, errorMessage);
|
|
114
|
-
rule.context.options.field = input;
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Mark this field as required if the given input field is present
|
|
119
|
-
*/
|
|
120
|
-
requiredIfPresent(input, errorMessage) {
|
|
121
|
-
return this.requiredWith(input, errorMessage);
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Value is required if the given input field (global) is absent
|
|
125
|
-
*/
|
|
126
|
-
requiredIfAbsent(input, errorMessage) {
|
|
127
|
-
const rule = this.addRule(requiredIfAbsentRule, errorMessage);
|
|
128
|
-
rule.context.options.field = input;
|
|
129
|
-
return this;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Value is required if given sibling field is absent
|
|
133
|
-
*/
|
|
134
|
-
requireIfSiblingIsAbsent(field, errorMessage) {
|
|
135
|
-
const rule = this.addRule(requiredIfSiblingFieldIsAbsentRule, errorMessage);
|
|
136
|
-
rule.context.options.field = field;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Value is required if all given input fields in same parent context are absent
|
|
141
|
-
*/
|
|
142
|
-
requiredIfSiblingFieldAllAbsent(fields, errorMessage) {
|
|
143
|
-
const rule = this.addRule(requiredIfSiblingFieldAllAbsentRule, errorMessage);
|
|
144
|
-
rule.context.options.fields = fields;
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* @alias requiredIfAbsent
|
|
149
|
-
*/
|
|
150
|
-
requiredIfMissing(input, errorMessage) {
|
|
151
|
-
return this.requiredIfAbsent(input, errorMessage);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Value is required if and only if the given input field is empty
|
|
155
|
-
*/
|
|
156
|
-
requiredIfEmpty(input, errorMessage) {
|
|
157
|
-
const rule = this.addRule(requiredIfEmptyRule, errorMessage);
|
|
158
|
-
rule.context.options.field = input;
|
|
159
|
-
return this;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Value is required if and only if the given input field in same parent context is empty
|
|
163
|
-
*/
|
|
164
|
-
requiredIfSiblingFieldEmpty(input, errorMessage) {
|
|
165
|
-
const rule = this.addRule(requiredIfSiblingFieldEmptyRule, errorMessage);
|
|
166
|
-
rule.context.options.field = input;
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Value is required if and only if the given input field has the given value
|
|
171
|
-
*/
|
|
172
|
-
requiredIfField(input, value, errorMessage) {
|
|
173
|
-
const rule = this.addRule(requiredIfFieldRule, errorMessage);
|
|
174
|
-
rule.context.options.field = input;
|
|
175
|
-
rule.context.options.value = value;
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Value is required if and only if the given input field in same parent context has the given value
|
|
180
|
-
*/
|
|
181
|
-
requiredIfSiblingField(input, value, errorMessage) {
|
|
182
|
-
const rule = this.addRule(requiredIfSiblingFieldRule, errorMessage);
|
|
183
|
-
rule.context.options.field = input;
|
|
184
|
-
rule.context.options.value = value;
|
|
185
|
-
return this;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Value is required unless the given input field in same parent context has the given value
|
|
189
|
-
*/
|
|
190
|
-
requiredUnlessSiblingField(input, value, errorMessage) {
|
|
191
|
-
const rule = this.addRule(requiredUnlessSiblingFieldRule, errorMessage);
|
|
192
|
-
rule.context.options.field = input;
|
|
193
|
-
rule.context.options.value = value;
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Mutate the data
|
|
198
|
-
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
199
|
-
*/
|
|
200
|
-
async mutate(data, context) {
|
|
201
|
-
let mutatedData = data;
|
|
202
|
-
for (const mutator of this.mutators) {
|
|
203
|
-
mutator.context.ctx = context;
|
|
204
|
-
mutatedData = await mutator.mutate(mutatedData, mutator.context);
|
|
205
|
-
}
|
|
206
|
-
return mutatedData;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Value is forbidden to be present
|
|
210
|
-
*/
|
|
211
|
-
forbidden(errorMessage) {
|
|
212
|
-
this.addRule(forbiddenRule, errorMessage);
|
|
213
|
-
return this;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Apply conditional validation rules based on another field value
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* ```ts
|
|
220
|
-
* v.object({
|
|
221
|
-
* status: v.when("type", {
|
|
222
|
-
* is: {
|
|
223
|
-
* post: v.string().required().in(["active", "inactive"]),
|
|
224
|
-
* news: v.string().required().in(["published", "draft"]),
|
|
225
|
-
* },
|
|
226
|
-
* otherwise: v.forbidden(),
|
|
227
|
-
* }),
|
|
228
|
-
* })
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
when(field, options) {
|
|
232
|
-
const rule = this.addRule(whenRule);
|
|
233
|
-
rule.context.options.field = field;
|
|
234
|
-
rule.context.options.is = options.is;
|
|
235
|
-
rule.context.options.otherwise = options.otherwise;
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Validate the data
|
|
240
|
-
*/
|
|
241
|
-
async validate(data, context) {
|
|
242
|
-
const mutatedData = await this.mutate(data ?? this.defaultValue, context);
|
|
243
|
-
const errors = [];
|
|
244
|
-
let isValid = true;
|
|
245
|
-
const isFirstErrorOnly = context.configurations?.firstErrorOnly ?? true;
|
|
246
|
-
for (const rule of this.rules) {
|
|
247
|
-
if ((rule.requiresValue ?? true) && data === undefined)
|
|
248
|
-
continue;
|
|
249
|
-
const result = await rule.validate(mutatedData, context);
|
|
250
|
-
if (result.isValid === false) {
|
|
251
|
-
isValid = false;
|
|
252
|
-
errors.push({
|
|
253
|
-
type: rule.name,
|
|
254
|
-
error: result.error,
|
|
255
|
-
input: result.path ?? context.path,
|
|
256
|
-
});
|
|
257
|
-
if (isFirstErrorOnly) {
|
|
258
|
-
break;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return {
|
|
263
|
-
isValid,
|
|
264
|
-
errors,
|
|
265
|
-
data: mutatedData,
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
class AnyValidator extends BaseValidator {
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Recursively remove undefined values from an object
|
|
273
|
-
*/
|
|
274
|
-
function removeUndefinedValues(obj) {
|
|
275
|
-
if (isObject(obj) && !isPlainObject(obj)) {
|
|
276
|
-
return obj;
|
|
277
|
-
}
|
|
278
|
-
if (Array.isArray(obj)) {
|
|
279
|
-
return obj.map(item => removeUndefinedValues(item));
|
|
280
|
-
}
|
|
281
|
-
if (obj !== null && typeof obj === "object") {
|
|
282
|
-
const result = {};
|
|
283
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
284
|
-
if (value !== undefined) {
|
|
285
|
-
result[key] = removeUndefinedValues(value);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
return result;
|
|
289
|
-
}
|
|
290
|
-
return obj;
|
|
291
|
-
}
|
|
292
|
-
class ObjectValidator extends BaseValidator {
|
|
293
|
-
schema;
|
|
294
|
-
/**
|
|
295
|
-
* Whether to allow unknown properties
|
|
296
|
-
*
|
|
297
|
-
* @default false
|
|
298
|
-
*/
|
|
299
|
-
shouldAllowUnknown = false;
|
|
300
|
-
/**
|
|
301
|
-
* Allowed keys that could be in the data but not necessarily validated
|
|
302
|
-
*/
|
|
303
|
-
allowedKeys = [];
|
|
304
|
-
constructor(schema, errorMessage) {
|
|
305
|
-
super();
|
|
306
|
-
this.schema = schema;
|
|
307
|
-
this.addRule(objectRule, errorMessage);
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Strip unknown keys from the data
|
|
311
|
-
*
|
|
312
|
-
* @mutate
|
|
313
|
-
*/
|
|
314
|
-
stripUnknown() {
|
|
315
|
-
const validator = this;
|
|
316
|
-
this.addMutator(stripUnknownMutator, {
|
|
317
|
-
get allowedKeys() {
|
|
318
|
-
return validator.allowedKeys;
|
|
319
|
-
},
|
|
320
|
-
});
|
|
321
|
-
return this;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Add list of allowed keys that could be in the data but not necessarily validated
|
|
325
|
-
*/
|
|
326
|
-
allow(...keys) {
|
|
327
|
-
this.allowedKeys.push(...keys);
|
|
328
|
-
return this;
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Trim values of the object properties
|
|
332
|
-
*/
|
|
333
|
-
trim(recursive = true) {
|
|
334
|
-
this.addMutator(objectTrimMutator, { recursive });
|
|
335
|
-
return this;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Whether to allow unknown properties
|
|
339
|
-
*
|
|
340
|
-
* @default false
|
|
341
|
-
*/
|
|
342
|
-
allowUnknown(allow = true) {
|
|
343
|
-
this.shouldAllowUnknown = allow;
|
|
344
|
-
return this;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Mutate the data
|
|
348
|
-
*
|
|
349
|
-
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
350
|
-
*/
|
|
351
|
-
mutate(data, context) {
|
|
352
|
-
if (!isPlainObject(data))
|
|
353
|
-
return data;
|
|
354
|
-
return super.mutate({ ...data }, context);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Validate the data
|
|
358
|
-
*/
|
|
359
|
-
async validate(data, context) {
|
|
360
|
-
context.schema = this.schema;
|
|
361
|
-
const mutatedData = await this.mutate(data, context);
|
|
362
|
-
// now we need to check if the object has unknown properties
|
|
363
|
-
if (this.shouldAllowUnknown === false) {
|
|
364
|
-
const rule = this.addRule(unknownKeyRule);
|
|
365
|
-
rule.context.options.allowedKeys = this.allowedKeys;
|
|
366
|
-
rule.context.options.schema = this.schema;
|
|
367
|
-
}
|
|
368
|
-
const result = await super.validate(mutatedData, context);
|
|
369
|
-
if (result.isValid === false)
|
|
370
|
-
return result;
|
|
371
|
-
// if no data and the object is valid, then return it as-is, nothing to do
|
|
372
|
-
if (data === undefined)
|
|
373
|
-
return result;
|
|
374
|
-
// now we need to validate the object properties
|
|
375
|
-
const errors = [];
|
|
376
|
-
const finalData = {};
|
|
377
|
-
const validationPromises = Object.keys(this.schema).map(async (key) => {
|
|
378
|
-
const value = mutatedData?.[key];
|
|
379
|
-
const validator = this.schema[key];
|
|
380
|
-
// Only process fields that were provided in the input or have explicit defaults
|
|
381
|
-
if (key in data || validator.getDefaultValue() !== undefined) {
|
|
382
|
-
const childContext = {
|
|
383
|
-
...context,
|
|
384
|
-
parent: mutatedData,
|
|
385
|
-
value,
|
|
386
|
-
key,
|
|
387
|
-
path: setKeyPath(context.path, key),
|
|
388
|
-
};
|
|
389
|
-
const childResult = await validator.validate(value, childContext);
|
|
390
|
-
if (childResult.data !== undefined) {
|
|
391
|
-
finalData[key] = childResult.data;
|
|
392
|
-
}
|
|
393
|
-
if (childResult.isValid === false) {
|
|
394
|
-
errors.push(...childResult.errors);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
});
|
|
398
|
-
await Promise.all(validationPromises);
|
|
399
|
-
// Remove undefined values from the final data
|
|
400
|
-
const cleanedData = removeUndefinedValues(finalData);
|
|
401
|
-
return {
|
|
402
|
-
isValid: errors.length === 0,
|
|
403
|
-
errors,
|
|
404
|
-
data: cleanedData,
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
class ArrayValidator extends BaseValidator {
|
|
409
|
-
validator;
|
|
410
|
-
constructor(validator, errorMessage) {
|
|
411
|
-
super();
|
|
412
|
-
this.validator = validator;
|
|
413
|
-
this.addRule(arrayRule, errorMessage);
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Reverse array order
|
|
417
|
-
*
|
|
418
|
-
* @mutate
|
|
419
|
-
*/
|
|
420
|
-
flip() {
|
|
421
|
-
return this.addMutator(flipArrayMutator);
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Reverse array order
|
|
425
|
-
*
|
|
426
|
-
* @mutate
|
|
427
|
-
*/
|
|
428
|
-
reverse() {
|
|
429
|
-
return this.addMutator(flipArrayMutator);
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* Make it has only unique values
|
|
433
|
-
*
|
|
434
|
-
* @mutate
|
|
435
|
-
*/
|
|
436
|
-
onlyUnique() {
|
|
437
|
-
return this.addMutator(uniqueArrayMutator);
|
|
438
|
-
}
|
|
439
|
-
/**
|
|
440
|
-
* Sort array
|
|
441
|
-
*
|
|
442
|
-
* If key is passed, it will sort by the key value
|
|
443
|
-
*
|
|
444
|
-
* @mutate
|
|
445
|
-
* @supports dot notation
|
|
446
|
-
*/
|
|
447
|
-
sort(direction = "asc", key) {
|
|
448
|
-
this.addMutator(sortArrayMutator, { direction, key });
|
|
449
|
-
return this;
|
|
450
|
-
}
|
|
451
|
-
// End of mutators
|
|
452
|
-
// Start of rules
|
|
453
|
-
/**
|
|
454
|
-
* Array length must be greater than the given length
|
|
455
|
-
*/
|
|
456
|
-
minLength(length, errorMessage) {
|
|
457
|
-
const rule = this.addRule(minLengthRule, errorMessage);
|
|
458
|
-
rule.context.options.minLength = length;
|
|
459
|
-
return this;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Array length must be less than the given length
|
|
463
|
-
*/
|
|
464
|
-
maxLength(length, errorMessage) {
|
|
465
|
-
const rule = this.addRule(maxLengthRule, errorMessage);
|
|
466
|
-
rule.context.options.maxLength = length;
|
|
467
|
-
return this;
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Array length must be of the given length
|
|
471
|
-
*/
|
|
472
|
-
length(length, errorMessage) {
|
|
473
|
-
const rule = this.addRule(lengthRule, errorMessage);
|
|
474
|
-
rule.context.options.length = length;
|
|
475
|
-
return this;
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Array must have unique values
|
|
479
|
-
*/
|
|
480
|
-
unique() {
|
|
481
|
-
this.addRule(uniqueArrayRule);
|
|
482
|
-
return this;
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Mutate the data
|
|
486
|
-
*
|
|
487
|
-
* Please note this method should not be called directly, as it is used internally by the `validate` method
|
|
488
|
-
*/
|
|
489
|
-
mutate(data, context) {
|
|
490
|
-
if (!Array.isArray(data))
|
|
491
|
-
return data;
|
|
492
|
-
return super.mutate([...data], context);
|
|
493
|
-
}
|
|
494
|
-
/**
|
|
495
|
-
* Validate array
|
|
496
|
-
*/
|
|
497
|
-
async validate(data, context) {
|
|
498
|
-
const mutatedData = (await this.mutate(data, context)) || [];
|
|
499
|
-
const result = await super.validate(data, context);
|
|
500
|
-
if (result.isValid === false)
|
|
501
|
-
return result;
|
|
502
|
-
const errors = [];
|
|
503
|
-
for (let index = 0; index < mutatedData.length; index++) {
|
|
504
|
-
const value = mutatedData[index];
|
|
505
|
-
const childContext = {
|
|
506
|
-
...context,
|
|
507
|
-
parent: mutatedData,
|
|
508
|
-
value,
|
|
509
|
-
key: index.toString(),
|
|
510
|
-
path: setKeyPath(context.path, index.toString()),
|
|
511
|
-
};
|
|
512
|
-
const childResult = await this.validator.validate(value, childContext);
|
|
513
|
-
mutatedData[index] = childResult.data;
|
|
514
|
-
if (childResult.isValid === false) {
|
|
515
|
-
errors.push(...childResult.errors);
|
|
516
|
-
}
|
|
517
|
-
if (context.configurations?.firstErrorOnly && errors.length) {
|
|
518
|
-
break;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
return {
|
|
522
|
-
isValid: errors.length === 0,
|
|
523
|
-
errors,
|
|
524
|
-
data: mutatedData,
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
class StringValidator extends BaseValidator {
|
|
529
|
-
constructor(errorMessage) {
|
|
530
|
-
super();
|
|
531
|
-
this.addRule(stringRule, errorMessage);
|
|
532
|
-
this.addMutator(stringMutator);
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Convert string to lowercase
|
|
536
|
-
*
|
|
537
|
-
* @mutate
|
|
538
|
-
*/
|
|
539
|
-
lowercase() {
|
|
540
|
-
this.addMutator(lowercaseMutator);
|
|
541
|
-
return this;
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Convert string to uppercase
|
|
545
|
-
*
|
|
546
|
-
* @mutate
|
|
547
|
-
*/
|
|
548
|
-
uppercase() {
|
|
549
|
-
this.addMutator(uppercaseMutator);
|
|
550
|
-
return this;
|
|
551
|
-
}
|
|
552
|
-
/**
|
|
553
|
-
* Capitalize the first letter of the string
|
|
554
|
-
*
|
|
555
|
-
* @mutate
|
|
556
|
-
*/
|
|
557
|
-
capitalize() {
|
|
558
|
-
this.addMutator(capitalizeMutator);
|
|
559
|
-
return this;
|
|
560
|
-
}
|
|
561
|
-
/**
|
|
562
|
-
* Value must be a valid email
|
|
563
|
-
*/
|
|
564
|
-
email(errorMessage) {
|
|
565
|
-
this.addRule(emailRule, errorMessage);
|
|
566
|
-
return this;
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Value must be a valid URL
|
|
570
|
-
*/
|
|
571
|
-
url(errorMessage) {
|
|
572
|
-
this.addRule(urlRule, errorMessage);
|
|
573
|
-
return this;
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Value must match the value of the given field
|
|
577
|
-
*/
|
|
578
|
-
matches(field, errorMessage) {
|
|
579
|
-
const rule = this.addRule(matchesRule, errorMessage);
|
|
580
|
-
rule.context.options.field = field;
|
|
581
|
-
return this;
|
|
582
|
-
}
|
|
583
|
-
/**
|
|
584
|
-
* Value can not have whitespace
|
|
585
|
-
*/
|
|
586
|
-
withoutWhitespace(errorMessage) {
|
|
587
|
-
this.addRule(withoutWhitespaceRule, errorMessage);
|
|
588
|
-
return this;
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Value must match the given pattern
|
|
592
|
-
*/
|
|
593
|
-
pattern(pattern, errorMessage) {
|
|
594
|
-
const rule = this.addRule(patternRule, errorMessage);
|
|
595
|
-
rule.context.options.pattern = pattern;
|
|
596
|
-
return this;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Validate the current string as an uploadable hash id
|
|
600
|
-
*/
|
|
601
|
-
uploadable(errorMessage) {
|
|
602
|
-
this.addRule(uploadableRule, errorMessage);
|
|
603
|
-
return this;
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* Value must be exactly the given number of words
|
|
607
|
-
*/
|
|
608
|
-
words(words, errorMessage) {
|
|
609
|
-
const rule = this.addRule(wordsRule, errorMessage);
|
|
610
|
-
rule.context.options.words = words;
|
|
611
|
-
return this;
|
|
612
|
-
}
|
|
613
|
-
/**
|
|
614
|
-
* Value must be at least the given number of words
|
|
615
|
-
*/
|
|
616
|
-
minWords(words, errorMessage) {
|
|
617
|
-
const rule = this.addRule(minWordsRule, errorMessage);
|
|
618
|
-
rule.context.options.minWords = words;
|
|
619
|
-
return this;
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Value must be at most the given number of words
|
|
623
|
-
*/
|
|
624
|
-
maxWords(words, errorMessage) {
|
|
625
|
-
const rule = this.addRule(maxWordsRule, errorMessage);
|
|
626
|
-
rule.context.options.maxWords = words;
|
|
627
|
-
return this;
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Value length must be greater than the given length
|
|
631
|
-
*/
|
|
632
|
-
minLength(length, errorMessage) {
|
|
633
|
-
const rule = this.addRule(minLengthRule, errorMessage);
|
|
634
|
-
rule.context.options.minLength = length;
|
|
635
|
-
return this;
|
|
636
|
-
}
|
|
637
|
-
/**
|
|
638
|
-
* @alias minLength
|
|
639
|
-
*/
|
|
640
|
-
min(min, errorMessage) {
|
|
641
|
-
return this.minLength(min, errorMessage);
|
|
642
|
-
}
|
|
643
|
-
/**
|
|
644
|
-
* Value length must be less than the given length
|
|
645
|
-
*/
|
|
646
|
-
maxLength(length, errorMessage) {
|
|
647
|
-
const rule = this.addRule(maxLengthRule, errorMessage);
|
|
648
|
-
rule.context.options.maxLength = length;
|
|
649
|
-
return this;
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* @alias maxLength
|
|
653
|
-
*/
|
|
654
|
-
max(max, errorMessage) {
|
|
655
|
-
return this.maxLength(max, errorMessage);
|
|
656
|
-
}
|
|
657
|
-
/**
|
|
658
|
-
* Value must be of the given length
|
|
659
|
-
*/
|
|
660
|
-
length(length, errorMessage) {
|
|
661
|
-
const rule = this.addRule(lengthRule, errorMessage);
|
|
662
|
-
rule.context.options.length = length;
|
|
663
|
-
return this;
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Allow only alphabetic characters
|
|
667
|
-
*/
|
|
668
|
-
alpha(errorMessage) {
|
|
669
|
-
this.addRule(alphaRule, errorMessage);
|
|
670
|
-
return this;
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* Allow only alphanumeric characters
|
|
674
|
-
*/
|
|
675
|
-
alphanumeric(errorMessage) {
|
|
676
|
-
this.addRule(alphaNumericRule, errorMessage);
|
|
677
|
-
return this;
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
|
-
* Allow only numeric characters
|
|
681
|
-
*/
|
|
682
|
-
numeric(errorMessage) {
|
|
683
|
-
this.addRule(isNumericRule, errorMessage);
|
|
684
|
-
return this;
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Value must starts with the given string
|
|
688
|
-
*/
|
|
689
|
-
startsWith(value, errorMessage) {
|
|
690
|
-
const rule = this.addRule(startsWithRule, errorMessage);
|
|
691
|
-
rule.context.options.value = value;
|
|
692
|
-
return this;
|
|
693
|
-
}
|
|
694
|
-
/**
|
|
695
|
-
* Value must ends with the given string
|
|
696
|
-
*/
|
|
697
|
-
endsWith(value, errorMessage) {
|
|
698
|
-
const rule = this.addRule(endsWithRule, errorMessage);
|
|
699
|
-
rule.context.options.value = value;
|
|
700
|
-
return this;
|
|
701
|
-
}
|
|
702
|
-
/**
|
|
703
|
-
* Value must contain the given string
|
|
704
|
-
*/
|
|
705
|
-
contains(value, errorMessage) {
|
|
706
|
-
const rule = this.addRule(containsRule, errorMessage);
|
|
707
|
-
rule.context.options.value = value;
|
|
708
|
-
return this;
|
|
709
|
-
}
|
|
710
|
-
/**
|
|
711
|
-
* Value must not contain the given string
|
|
712
|
-
*/
|
|
713
|
-
notContains(value, errorMessage) {
|
|
714
|
-
const rule = this.addRule(notContainsRule, errorMessage);
|
|
715
|
-
rule.context.options.value = value;
|
|
716
|
-
return this;
|
|
717
|
-
}
|
|
718
|
-
/**
|
|
719
|
-
* Value must be a valid IP address
|
|
720
|
-
*/
|
|
721
|
-
ip(errorMessage) {
|
|
722
|
-
this.addRule(ipRule, errorMessage);
|
|
723
|
-
return this;
|
|
724
|
-
}
|
|
725
|
-
/**
|
|
726
|
-
* Value must be a valid IPv4 address
|
|
727
|
-
*/
|
|
728
|
-
ip4(errorMessage) {
|
|
729
|
-
this.addRule(ip4Rule, errorMessage);
|
|
730
|
-
return this;
|
|
731
|
-
}
|
|
732
|
-
/**
|
|
733
|
-
* Value must be a valid IPv6 address
|
|
734
|
-
*/
|
|
735
|
-
ip6(errorMessage) {
|
|
736
|
-
this.addRule(ip6Rule, errorMessage);
|
|
737
|
-
return this;
|
|
738
|
-
}
|
|
739
|
-
/**
|
|
740
|
-
* Check if the string matches a credit card number
|
|
741
|
-
*/
|
|
742
|
-
creditCard(errorMessage) {
|
|
743
|
-
this.addRule(isCreditCardRule, errorMessage);
|
|
744
|
-
return this;
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* Determine if the value is a valid color
|
|
748
|
-
* This validation rule will check for hex, rgb, rgba, hsl colors
|
|
749
|
-
*/
|
|
750
|
-
color(errorMessage) {
|
|
751
|
-
this.addRule(colorRule, errorMessage);
|
|
752
|
-
return this;
|
|
753
|
-
}
|
|
754
|
-
/**
|
|
755
|
-
* Determine if the value is a valid hex color
|
|
756
|
-
*/
|
|
757
|
-
hexColor(errorMessage) {
|
|
758
|
-
this.addRule(hexColorRule, errorMessage);
|
|
759
|
-
return this;
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* Determine if the value is a valid HSL color
|
|
763
|
-
*/
|
|
764
|
-
hslColor(errorMessage) {
|
|
765
|
-
this.addRule(hslColorRule, errorMessage);
|
|
766
|
-
return this;
|
|
767
|
-
}
|
|
768
|
-
/**
|
|
769
|
-
* Determine if the value is a valid RGB color
|
|
770
|
-
*/
|
|
771
|
-
rgbColor(errorMessage) {
|
|
772
|
-
this.addRule(rgbColorRule, errorMessage);
|
|
773
|
-
return this;
|
|
774
|
-
}
|
|
775
|
-
/**
|
|
776
|
-
* Determine if the value is a valid RGBA color
|
|
777
|
-
*/
|
|
778
|
-
rgbaColor(errorMessage) {
|
|
779
|
-
this.addRule(rgbaColorRule, errorMessage);
|
|
780
|
-
return this;
|
|
781
|
-
}
|
|
782
|
-
/**
|
|
783
|
-
* Determine if the value is a valid light color
|
|
784
|
-
*/
|
|
785
|
-
lightColor(errorMessage) {
|
|
786
|
-
this.addRule(lightColorRule, errorMessage);
|
|
787
|
-
return this;
|
|
788
|
-
}
|
|
789
|
-
/**
|
|
790
|
-
* Determine if the value is a valid dark color
|
|
791
|
-
*/
|
|
792
|
-
darkColor(errorMessage) {
|
|
793
|
-
this.addRule(darkColorRule, errorMessage);
|
|
794
|
-
return this;
|
|
795
|
-
}
|
|
796
|
-
/**
|
|
797
|
-
* Value must be one of the given values
|
|
798
|
-
*/
|
|
799
|
-
enum = ScalarValidator.prototype.enum;
|
|
800
|
-
/**
|
|
801
|
-
* Value must be one of the given values
|
|
802
|
-
*/
|
|
803
|
-
in = ScalarValidator.prototype.in;
|
|
804
|
-
/**
|
|
805
|
-
* @alias in
|
|
806
|
-
*/
|
|
807
|
-
oneOf = ScalarValidator.prototype.in;
|
|
808
|
-
/**
|
|
809
|
-
* Value must be unique
|
|
810
|
-
*/
|
|
811
|
-
unique = ScalarValidator.prototype.unique;
|
|
812
|
-
/**
|
|
813
|
-
* Value must be unique except current user
|
|
814
|
-
*/
|
|
815
|
-
uniqueExceptCurrentUser = ScalarValidator.prototype.uniqueExceptCurrentUser;
|
|
816
|
-
/**
|
|
817
|
-
* Value must be unique except current id
|
|
818
|
-
*/
|
|
819
|
-
uniqueExceptCurrentId = ScalarValidator.prototype.uniqueExceptCurrentId;
|
|
820
|
-
/**
|
|
821
|
-
* Value must exist
|
|
822
|
-
*/
|
|
823
|
-
exists = ScalarValidator.prototype.exists;
|
|
824
|
-
/**
|
|
825
|
-
* Value must exist except current user
|
|
826
|
-
*/
|
|
827
|
-
existsExceptCurrentUser = ScalarValidator.prototype.existsExceptCurrentUser;
|
|
828
|
-
/**
|
|
829
|
-
* Value must exist except current id
|
|
830
|
-
*/
|
|
831
|
-
existsExceptCurrentId = ScalarValidator.prototype.existsExceptCurrentId;
|
|
832
|
-
/**
|
|
833
|
-
* Add rule to check if the value is one of the allowed values
|
|
834
|
-
*/
|
|
835
|
-
allowsOnly = ScalarValidator.prototype.allowsOnly;
|
|
836
|
-
/**
|
|
837
|
-
* Add rule to forbid the value from being one of the given values
|
|
838
|
-
*/
|
|
839
|
-
forbids = ScalarValidator.prototype.forbids;
|
|
840
|
-
/**
|
|
841
|
-
* @alias forbids
|
|
842
|
-
*/
|
|
843
|
-
notIn = ScalarValidator.prototype.forbids;
|
|
844
|
-
}
|
|
845
|
-
class DateValidator extends BaseValidator {
|
|
846
|
-
constructor(format, errorMessage) {
|
|
847
|
-
super();
|
|
848
|
-
const rule = this.addRule(dateRule, errorMessage);
|
|
849
|
-
if (format) {
|
|
850
|
-
rule.context.options.format = format;
|
|
851
|
-
}
|
|
852
|
-
this.addMutator(dateMutator);
|
|
853
|
-
}
|
|
854
|
-
/**
|
|
855
|
-
* Date must be before the given date
|
|
856
|
-
*/
|
|
857
|
-
before(date, errorMessage) {
|
|
858
|
-
const rule = this.addRule(maxDateRule, errorMessage);
|
|
859
|
-
rule.context.options.maxDate = date;
|
|
860
|
-
return this;
|
|
861
|
-
}
|
|
862
|
-
/**
|
|
863
|
-
* Date must be after the given date
|
|
864
|
-
*/
|
|
865
|
-
after(date, errorMessage) {
|
|
866
|
-
const rule = this.addRule(minDateRule, errorMessage);
|
|
867
|
-
rule.context.options.minDate = date;
|
|
868
|
-
return this;
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
class NumberValidator extends BaseValidator {
|
|
872
|
-
constructor(errorMessage) {
|
|
873
|
-
super();
|
|
874
|
-
this.addRule(numberRule, errorMessage);
|
|
875
|
-
this.addMutator(numberMutator);
|
|
876
|
-
}
|
|
877
|
-
/**
|
|
878
|
-
* Value must be equal or higher than the given number
|
|
879
|
-
*/
|
|
880
|
-
min(min, errorMessage) {
|
|
881
|
-
const rule = this.addRule(minRule, errorMessage);
|
|
882
|
-
rule.context.options.min = min;
|
|
883
|
-
return this;
|
|
884
|
-
}
|
|
885
|
-
/**
|
|
886
|
-
* Value must be equal or less than the given number
|
|
887
|
-
*/
|
|
888
|
-
max(max, errorMessage) {
|
|
889
|
-
const rule = this.addRule(maxRule, errorMessage);
|
|
890
|
-
rule.context.options.max = max;
|
|
891
|
-
return this;
|
|
892
|
-
}
|
|
893
|
-
/**
|
|
894
|
-
* Value must be a modulo of the given number
|
|
895
|
-
*/
|
|
896
|
-
modulo(value, errorMessage) {
|
|
897
|
-
const rule = this.addRule(moduloRule, errorMessage);
|
|
898
|
-
rule.context.options.value = value;
|
|
899
|
-
return this;
|
|
900
|
-
}
|
|
901
|
-
/**
|
|
902
|
-
* Accept only numbers higher than 0
|
|
903
|
-
*/
|
|
904
|
-
positive(errorMessage) {
|
|
905
|
-
this.addRule(positiveRule, errorMessage);
|
|
906
|
-
return this;
|
|
907
|
-
}
|
|
908
|
-
/**
|
|
909
|
-
* Value must be unique and not exist in database
|
|
910
|
-
*/
|
|
911
|
-
unique = ScalarValidator.prototype.unique;
|
|
912
|
-
/**
|
|
913
|
-
* Value must exist in database
|
|
914
|
-
*/
|
|
915
|
-
exists = ScalarValidator.prototype.exists;
|
|
916
|
-
/**
|
|
917
|
-
* Value must be one of the given values
|
|
918
|
-
*/
|
|
919
|
-
enum = ScalarValidator.prototype.enum;
|
|
920
|
-
/**
|
|
921
|
-
* Value must be one of the given values
|
|
922
|
-
*/
|
|
923
|
-
in = ScalarValidator.prototype.in;
|
|
924
|
-
/**
|
|
925
|
-
* @alias in
|
|
926
|
-
*/
|
|
927
|
-
oneOf = ScalarValidator.prototype.in;
|
|
928
|
-
/**
|
|
929
|
-
* Add rule to check if the value is one of the allowed values
|
|
930
|
-
*/
|
|
931
|
-
allowsOnly = ScalarValidator.prototype.allowsOnly;
|
|
932
|
-
/**
|
|
933
|
-
* Add rule to forbid the value from being one of the given values
|
|
934
|
-
*/
|
|
935
|
-
forbids = ScalarValidator.prototype.forbids;
|
|
936
|
-
/**
|
|
937
|
-
* @alias forbids
|
|
938
|
-
*/
|
|
939
|
-
notIn = ScalarValidator.prototype.forbids;
|
|
940
|
-
/**
|
|
941
|
-
* Validate number length
|
|
942
|
-
*/
|
|
943
|
-
length = StringValidator.prototype.length;
|
|
944
|
-
/**
|
|
945
|
-
* Validate number min length
|
|
946
|
-
*/
|
|
947
|
-
minLength = StringValidator.prototype.minLength;
|
|
948
|
-
/**
|
|
949
|
-
* Validate number max length
|
|
950
|
-
*/
|
|
951
|
-
maxLength = StringValidator.prototype.maxLength;
|
|
952
|
-
}
|
|
953
|
-
class IntValidator extends NumberValidator {
|
|
954
|
-
constructor(errorMessage) {
|
|
955
|
-
super();
|
|
956
|
-
this.addRule(intRule, errorMessage);
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
class FloatValidator extends NumberValidator {
|
|
960
|
-
constructor(errorMessage) {
|
|
961
|
-
super();
|
|
962
|
-
this.addRule(floatRule, errorMessage);
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
class BooleanValidator extends BaseValidator {
|
|
966
|
-
constructor(errorMessage) {
|
|
967
|
-
super();
|
|
968
|
-
this.addRule(booleanRule, errorMessage);
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
class ScalarValidator extends BaseValidator {
|
|
972
|
-
constructor(errorMessage) {
|
|
973
|
-
super();
|
|
974
|
-
this.addRule(scalarRule, errorMessage);
|
|
975
|
-
}
|
|
976
|
-
/**
|
|
977
|
-
* Value must be unique in database
|
|
978
|
-
*/
|
|
979
|
-
unique(model, optionsList) {
|
|
980
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
981
|
-
const rule = this.addRule(uniqueRule, errorMessage);
|
|
982
|
-
rule.context.options = {
|
|
983
|
-
...options,
|
|
984
|
-
Model: model,
|
|
985
|
-
};
|
|
986
|
-
return this;
|
|
987
|
-
}
|
|
988
|
-
/**
|
|
989
|
-
* Value must be unique in database except current user
|
|
990
|
-
*/
|
|
991
|
-
uniqueExceptCurrentUser(model, optionsList) {
|
|
992
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
993
|
-
const rule = this.addRule(uniqueExceptCurrentUserRule, errorMessage);
|
|
994
|
-
rule.context.options = {
|
|
995
|
-
...options,
|
|
996
|
-
Model: model,
|
|
997
|
-
};
|
|
998
|
-
return this;
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* Value must be unique in database except current id
|
|
1002
|
-
*/
|
|
1003
|
-
uniqueExceptCurrentId(model, optionsList) {
|
|
1004
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
1005
|
-
const rule = this.addRule(uniqueExceptCurrentIdRule, errorMessage);
|
|
1006
|
-
rule.context.options = {
|
|
1007
|
-
...options,
|
|
1008
|
-
Model: model,
|
|
1009
|
-
};
|
|
1010
|
-
return this;
|
|
1011
|
-
}
|
|
1012
|
-
/**
|
|
1013
|
-
* Value must exist in database
|
|
1014
|
-
*/
|
|
1015
|
-
exists(model, optionsList) {
|
|
1016
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
1017
|
-
const rule = this.addRule(existsRule, errorMessage);
|
|
1018
|
-
rule.context.options = {
|
|
1019
|
-
...options,
|
|
1020
|
-
Model: model,
|
|
1021
|
-
};
|
|
1022
|
-
return this;
|
|
1023
|
-
}
|
|
1024
|
-
/**
|
|
1025
|
-
* Value must exist in database except current user
|
|
1026
|
-
*/
|
|
1027
|
-
existsExceptCurrentUser(model, optionsList) {
|
|
1028
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
1029
|
-
const rule = this.addRule(existsExceptCurrentUserRule, errorMessage);
|
|
1030
|
-
rule.context.options = {
|
|
1031
|
-
...options,
|
|
1032
|
-
Model: model,
|
|
1033
|
-
};
|
|
1034
|
-
return this;
|
|
1035
|
-
}
|
|
1036
|
-
/**
|
|
1037
|
-
* Value must exists in database except current id
|
|
1038
|
-
*/
|
|
1039
|
-
existsExceptCurrentId(model, optionsList) {
|
|
1040
|
-
const { errorMessage, ...options } = optionsList || {};
|
|
1041
|
-
const rule = this.addRule(existsExceptCurrentIdRule, errorMessage);
|
|
1042
|
-
rule.context.options = {
|
|
1043
|
-
...options,
|
|
1044
|
-
Model: model,
|
|
1045
|
-
};
|
|
1046
|
-
return this;
|
|
1047
|
-
}
|
|
1048
|
-
/**
|
|
1049
|
-
* Value must be one of the given values
|
|
1050
|
-
*/
|
|
1051
|
-
enum(values, errorMessage) {
|
|
1052
|
-
const rule = this.addRule(enumRule, errorMessage);
|
|
1053
|
-
rule.context.options.enum = values;
|
|
1054
|
-
return this;
|
|
1055
|
-
}
|
|
1056
|
-
/**
|
|
1057
|
-
* Value must be one of the given values
|
|
1058
|
-
*/
|
|
1059
|
-
in(values, errorMessage) {
|
|
1060
|
-
const rule = this.addRule(inRule, errorMessage);
|
|
1061
|
-
rule.context.options.values = values;
|
|
1062
|
-
return this;
|
|
1063
|
-
}
|
|
1064
|
-
/**
|
|
1065
|
-
* @alias in
|
|
1066
|
-
*/
|
|
1067
|
-
oneOf = ScalarValidator.prototype.in;
|
|
1068
|
-
/**
|
|
1069
|
-
* Add rule to check if the value is one of the allowed values
|
|
1070
|
-
*/
|
|
1071
|
-
allowsOnly(values, errorMessage) {
|
|
1072
|
-
const rule = this.addRule(allowedValuesRule, errorMessage);
|
|
1073
|
-
rule.context.options.allowedValues = values;
|
|
1074
|
-
return this;
|
|
1075
|
-
}
|
|
1076
|
-
/**
|
|
1077
|
-
* Forbid the value from being one of the given values
|
|
1078
|
-
*/
|
|
1079
|
-
forbids(values, errorMessage) {
|
|
1080
|
-
const rule = this.addRule(notAllowedValuesRule, errorMessage);
|
|
1081
|
-
rule.context.options.notAllowedValues = values;
|
|
1082
|
-
return this;
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
class FileValidator extends BaseValidator {
|
|
1086
|
-
constructor(errorMessage) {
|
|
1087
|
-
super();
|
|
1088
|
-
this.addRule(fileRule, errorMessage);
|
|
1089
|
-
}
|
|
1090
|
-
image(errorMessage) {
|
|
1091
|
-
this.addRule(imageRule, errorMessage);
|
|
1092
|
-
return this;
|
|
1093
|
-
}
|
|
1094
|
-
accept(extensions, errorMessage) {
|
|
1095
|
-
const rule = this.addRule(fileExtensionRule, errorMessage);
|
|
1096
|
-
rule.context.options.extensions = extensions;
|
|
1097
|
-
return this;
|
|
1098
|
-
}
|
|
1099
|
-
mimeType(mimeTypes, errorMessage) {
|
|
1100
|
-
const rule = this.addRule(fileTypeRule, errorMessage);
|
|
1101
|
-
rule.context.options.mimeTypes = mimeTypes;
|
|
1102
|
-
return this;
|
|
1103
|
-
}
|
|
1104
|
-
/**
|
|
1105
|
-
* Allow only pdf files
|
|
1106
|
-
*/
|
|
1107
|
-
pdf(errorMessage) {
|
|
1108
|
-
return this.mimeType("application/pdf", errorMessage);
|
|
1109
|
-
}
|
|
1110
|
-
/**
|
|
1111
|
-
* Allow only excel files
|
|
1112
|
-
*/
|
|
1113
|
-
excel(errorMessage) {
|
|
1114
|
-
return this.mimeType([
|
|
1115
|
-
"application/vnd.ms-excel",
|
|
1116
|
-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1117
|
-
], errorMessage);
|
|
1118
|
-
}
|
|
1119
|
-
/**
|
|
1120
|
-
* Allow only word files
|
|
1121
|
-
*/
|
|
1122
|
-
word(errorMessage) {
|
|
1123
|
-
return this.mimeType([
|
|
1124
|
-
"application/msword",
|
|
1125
|
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
1126
|
-
], errorMessage);
|
|
1127
|
-
}
|
|
1128
|
-
minSize(size, errorMessage) {
|
|
1129
|
-
const rule = this.addRule(minFileSizeRule, errorMessage);
|
|
1130
|
-
rule.context.options.minFileSize = size;
|
|
1131
|
-
return this;
|
|
1132
|
-
}
|
|
1133
|
-
min(size, errorMessage) {
|
|
1134
|
-
return this.minSize(size, errorMessage);
|
|
1135
|
-
}
|
|
1136
|
-
maxSize(size, errorMessage) {
|
|
1137
|
-
const rule = this.addRule(maxFileSizeRule, errorMessage);
|
|
1138
|
-
rule.context.options.maxFileSize = size;
|
|
1139
|
-
return this;
|
|
1140
|
-
}
|
|
1141
|
-
max(size, errorMessage) {
|
|
1142
|
-
return this.maxSize(size, errorMessage);
|
|
1143
|
-
}
|
|
1144
|
-
minWidth(width, errorMessage) {
|
|
1145
|
-
const rule = this.addRule(minWidthRule, errorMessage);
|
|
1146
|
-
rule.context.options.minWidth = width;
|
|
1147
|
-
return this;
|
|
1148
|
-
}
|
|
1149
|
-
maxWidth(width, errorMessage) {
|
|
1150
|
-
const rule = this.addRule(maxWidthRule, errorMessage);
|
|
1151
|
-
rule.context.options.maxWidth = width;
|
|
1152
|
-
return this;
|
|
1153
|
-
}
|
|
1154
|
-
minHeight(height, errorMessage) {
|
|
1155
|
-
const rule = this.addRule(minHeightRule, errorMessage);
|
|
1156
|
-
rule.context.options.minHeight = height;
|
|
1157
|
-
return this;
|
|
1158
|
-
}
|
|
1159
|
-
maxHeight(height, errorMessage) {
|
|
1160
|
-
const rule = this.addRule(maxHeightRule, errorMessage);
|
|
1161
|
-
rule.context.options.maxHeight = height;
|
|
1162
|
-
return this;
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
const validate = async (schema, data) => {
|
|
1166
|
-
const context = {
|
|
1167
|
-
allValues: data,
|
|
1168
|
-
parent: null,
|
|
1169
|
-
value: data,
|
|
1170
|
-
key: "",
|
|
1171
|
-
path: "",
|
|
1172
|
-
translator(rule, attributes) {
|
|
1173
|
-
return trans(`validation.${rule}`, attributes);
|
|
1174
|
-
},
|
|
1175
|
-
};
|
|
1176
|
-
return await schema.validate(data, context);
|
|
1177
|
-
};
|
|
1178
|
-
const v = {
|
|
1179
|
-
object: (schema, errorMessage) => new ObjectValidator(schema, errorMessage),
|
|
1180
|
-
any: () => new AnyValidator(),
|
|
1181
|
-
forbidden: () => v.any().forbidden(),
|
|
1182
|
-
array: (validator, errorMessage) => new ArrayValidator(validator, errorMessage),
|
|
1183
|
-
date: (format, errorMessage) => new DateValidator(format, errorMessage),
|
|
1184
|
-
string: (errorMessage) => new StringValidator(errorMessage),
|
|
1185
|
-
enum: (values, errorMessage) => new StringValidator().enum(values, errorMessage),
|
|
1186
|
-
number: (errorMessage) => new NumberValidator(errorMessage),
|
|
1187
|
-
int: (errorMessage) => new IntValidator(errorMessage),
|
|
1188
|
-
float: (errorMessage) => new FloatValidator(errorMessage),
|
|
1189
|
-
boolean: (errorMessage) => new BooleanValidator(errorMessage),
|
|
1190
|
-
scalar: (errorMessage) => new ScalarValidator(errorMessage),
|
|
1191
|
-
file: (errorMessage) => new FileValidator(errorMessage),
|
|
1192
|
-
localized: (valueValidator, errorMessage) => v.array(v.object({
|
|
1193
|
-
localeCode: v.string().required(),
|
|
1194
|
-
value: (valueValidator || v.string()).required(),
|
|
1195
|
-
}), errorMessage),
|
|
1196
|
-
/**
|
|
1197
|
-
* TODO: Added validateCallback to declare a schema and the second argument is a callback that will be only executed if schema validation passes
|
|
1198
|
-
* This will return a new callback that will be executed with the validated data
|
|
1199
|
-
*
|
|
1200
|
-
* @example
|
|
1201
|
-
* ```ts
|
|
1202
|
-
* export const createNewUser = v.validateCallback(schema, (data) => {
|
|
1203
|
-
* // Do something with the validated data
|
|
1204
|
-
* })
|
|
1205
|
-
* ```
|
|
1206
|
-
*/
|
|
1207
|
-
// validateCallback: (
|
|
1208
|
-
// schema: BaseValidator,
|
|
1209
|
-
// callback: (data: InferType<BaseValidator>) => Promise<any> | any,
|
|
1210
|
-
// ) => v.validate(schema, callback),
|
|
1211
|
-
validate,
|
|
1212
|
-
};export{AnyValidator,ArrayValidator,BaseValidator,BooleanValidator,DateValidator,FileValidator,FloatValidator,IntValidator,ObjectValidator,ScalarValidator,StringValidator,v,validate};//# sourceMappingURL=schema.js.map
|