| 875 | * @since 4.0.0 |
| 876 | */ |
| 877 | export function schema<T>(codec: Schema.ConstraintCodec<T, unknown>, path?: string | ConfigProvider.Path): Config<T> { |
| 878 | const codecStringTree = Schema.toCodecStringTree(codec) |
| 879 | const encodedAst = SchemaAST.toEncoded(codecStringTree.ast) |
| 880 | const decodeCursor = SchemaParser.decodeUnknownEffect( |
| 881 | Schema.make<Schema.Codec<T, ConfigCursor>>(toConfigCursorAST(codecStringTree.ast)) |
| 882 | ) |
| 883 | const localPath = typeof path === "string" ? [path] : path ?? [] |
| 884 | return make((provider, pathPrefix) => { |
| 885 | const fullPath = [...pathPrefix, ...localPath] |
| 886 | return catchSourceError(loadCursor(provider, fullPath), false).pipe( |
| 887 | Effect.flatMapEager((cursor) => { |
| 888 | const hasInput = hasProviderInput(encodedAst, cursor.node) |
| 889 | return catchSourceError( |
| 890 | decodeCursor(cursor).pipe( |
| 891 | Effect.mapEager((value) => resolved(value, hasInput)), |
| 892 | Effect.catchEager((issue) => { |
| 893 | const error = new ConfigError( |
| 894 | new Schema.SchemaError(fullPath.length > 0 ? new SchemaIssue.Pointer(fullPath, issue) : issue) |
| 895 | ) |
| 896 | return hasInput |
| 897 | ? Effect.fail(evaluationFailure(error, true)) |
| 898 | : Effect.succeed(absent(error)) |
| 899 | }) |
| 900 | ), |
| 901 | hasInput |
| 902 | ) |
| 903 | }) |
| 904 | ) |
| 905 | }) |
| 906 | } |
| 907 | |
| 908 | /** @internal */ |
| 909 | export const TrueValues = Schema.Literals(["true", "yes", "on", "1", "y"]) |